DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Making Code Faster: The Interview Question

Making Code Faster: The Interview Question

The author offers a question to ask in coding interviews to illuminate several aspects about a particular candidate's thinking.

Oren Eini user avatar by
Oren Eini
·
Nov. 13, 16 · Opinion
Like (4)
Save
Tweet
Share
4.94K Views

Join the DZone community and get the full member experience.

Join For Free

Interview questions are always tough to design. On one hand, you need to create something that will not be trivial to do, and on the other hand, you have a pretty much hard time limit to a reasonable solution. For example, while implementing a linked list is something that I would expect anyone to be able to do in an interview, implementing a binary tree (including the balancing), is probably not going to be feasible.

Interview tasks (that candidate can do at home) are somewhat easier because you don’t have the same time constraints, but at the same time, if you ask for something that takes a week to write, candidates will skip the question and the position entirely. Another issue here is that if you ask a candidate to send a binary tree as an interview task, they are going to google, copy and paste, and send, and you learn absolutely nothing. (Oh, sometimes you learn quite a lot, if a candidate cannot do that, they are pretty much disqualified themselves, but we could do that more easily with Fizz Buzz, after all.)

So I came up with the following question, we have the following file (the full data set is 276 MB), that contains the entry  exit log to a parking lot.

image

The first value is the entry time, the second is the exit time, and the third is the car ID.

Details about this file: This is UTF8 text file with space separated values using Windows line ending.

What we need to do is to find out how much time a car spent in the lot based on this file. I started out by writing the following code:

// code

var summary = from line in File.ReadAllLines(args[0])
     let record = new Record(line)
     group record by record.Id
     into g
     select new
     {
         Id = g.Key,
         Duration = TimeSpan.FromTicks(g.Sum(r => r.Duration.Ticks))
     };

 using (var output = File.CreateText("summary.txt"))
 {
     foreach (var entry in summary)
     {
         output.WriteLine($"{entry.Id:D10} {entry.Duration:c}");
     }
 }

// data class

  public class Record
  {
      public DateTime Start => DateTime.Parse(_line.Split(' ')[0]);

      public DateTime End => DateTime.Parse(_line.Split(' ')[1]);
      public long Id => long.Parse(_line.Split(' ')[2]);

      public TimeSpan Duration => End - Start;

      private readonly string _line;

      public Record(string line)
      {
          _line = line;
      }
  }

You can find the full file here. The only additional stuff is that we measure just how much this cost us.

This code processes the 276MB file in 30 seconds, using a peak working set of 850 MB and allocating a total of 7.6 GB of memory. I’m pretty sure that we can do better. That is the task we give to candidates.

This has the nice advantage of being a pretty small and compact problem, but to improve upon it, you actually need to understand what is going on under the covers.

Interview (journalism)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Data Leakage Nightmare in AI
  • Deploying Java Serverless Functions as AWS Lambda
  • GPT-3 Playground: The AI That Can Write for You
  • Java Development Trends 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: