DZone
Agile Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Agile Zone > 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 · Agile Zone · Opinion
Like (4)
Save
Tweet
4.80K 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

  • Classification of Neural Networks in TensorFlow
  • Model Quantization for Edge AI
  • Practice on Pushing Messages to Devices of Different Manufacturers
  • Blind SQL Injection — Threat or Child's Play?

Comments

Agile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo