Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making Code Faster: The Interview Question

DZone's Guide to

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.

· Agile Zone
Free Resource

Reduce testing time & get feedback faster through automation. Read the Benefits of Parallel Testing, brought to you in partnership with Sauce Labs.

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.

The Agile Zone is brought to you in partnership with Sauce Labs. Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure.

Topics:
career ,agile ,interviews ,coding

Published at DZone with permission of Ayende Rahien, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}