How would you implement this Java class?

We’re hiring a new Java developer and decided to start by asking them to write code instead of the usual Q&A.

Recently we needed to add an hourly scheduler to our sliding window data aggregator and decided this would be a good test to see how people think and code.

We gave our candidates the following class skeleton to complete.  As you can see, it has two parts.  A constructor that takes in a variable number of minutes-past-the-hour arguments.   And a method that returns the next occurrence given a fixed point in time.

We asked our candidates to compete the code and make all the tests in the main method pass.

Here’s the full listing.  How would you implement it?

 

Happy coding!

About Dele Taylor

We make Data Pipeline — a lightweight ETL framework for Java. Use it to filter, transform, and aggregate data on-the-fly in your web, mobile, and desktop apps. Learn more about it at northconcepts.com.

25 thoughts on “How would you implement this Java class?

  • By Tiejun - Reply

    • By Dele Taylor - Reply

      Very close to our implementation 🙂 You can reduce several lines of code by replacing the break statement with a return.

  • By Philippe FERY - Reply

    I would implement it as follow:

    • By Dele Taylor - Reply

      Nice approach Philippe and your line count is almost on par with ours.

      If I had included a real set of unit tests you would have caught an assumption in your code — that all schedules start on the hour. You can see what I mean by changing the input to be 15 and 45 minutes past the hour.

      Thanks for being a good sport and sharing your quick answer.

  • By Philippe FERY - Reply

    You’re right: i missed it! … Shame on me 🙂

    This time, it shoul be ok….

  • By Ervin Nemesszeghy - Reply

    • By Dele Taylor - Reply

      +1 for showing recursion instead of iteration.

      I probably wouldn’t do this in prod code because of the overhead in calling methods. I’d also suggest making minutesList a field and moving its initialization to the constructor to save time and memory.

  • By Tomm Carr - Reply

    Just found your site. Here is my first ever entry.

    • By Tomm Carr - Reply

      OK, so I don’t know how to post code and there are not any obvious instructions.

      • By Dele Taylor - Reply

        No worries. I surrounded your code with the PRE tag and one of the WordPress plugins figured it out.

        Thanks for sharing the fewest lines of code so far Tomm 🙂

  • By Philippe FERY - Reply

    Just for fun: less lines of code. But I agree, it’s not really readable 🙂

    • By Dele Taylor - Reply

      nice 😉

  • By Blake Rogers - Reply

  • By Blake Rogers - Reply

    A couple minor tweaks, removed extraneous objects, etc. It doesn’t perform well due to Calendar class calls though.

  • By Blake Rogers - Reply

    Okay, no Calendar. This one performs < 300MS for 1M.

    • By Dele Taylor - Reply

      Blake – you get points for testing against the 15 and 45 minute cases 🙂

      You get more points for removing the Calendar class, our implementation doesn’t use it (or Date) either. That may make our implementations vulnerable to daylight savings time and leap year cases, but I haven’t tested them yet.

      BTW, your test takes about 170 milliseconds for 1 million calls on my i7, but I know for a fact you can cut it in half 😉 Hint: do you need to call Arrays.sort(startTargetsMillisArr); inside getNextTime?

  • By Blake Rogers - Reply

    Thanks for the feedback, Dele. Here I do a basic sort during the insert. Results in twice the (relative) speed. My metrics are from an i5. The metric is actually like 8 million calls, since each “run” tests the algorithm 8 times, and each algorithm searching either shorter (2) or longer (4) array.

    • By Dele Taylor - Reply

      Blake – it looks like you’re modifying one of the member arrays inside getNextTime. If you can move this to the constructor, you’d have an thread-safe implementation.

      I used to ask developers to give advantages of immutable types like String and BigDecimal. Thread-safety was always a good answer 🙂

  • By Anish K George - Reply

    What about a bit of Java 8 APIs?

    • By Dele Taylor - Reply

      Thanks for sharing. I’m a big fan of fluent interfaces, but this one seems hard to read…

  • By Razvan - Reply

    • By Dele Taylor - Reply

      Razvan – this is a very sweet use of fluent APIs 🙂 You’d get points for the most readable so far — including against our own — code is for people after all.

      BTW, your minutesToMillis method should probably return a long instead of int. I’d probably also typecast intValue() to a long instead of creating a java.lang.Long instance, but it’d be very hard to measure the slight performance difference.

      Thanks for sharing. Cheers.

  • By Nadine - Reply

    • By Nadine - Reply

      I’m not sure what happened, but some lines are missing :-P. Here is the complete implementation:

      • By Dele Taylor - Reply

        Very tight implementation 🙂

        The main difference from ours is we used a loop instead of a binary search.

        Thanks for sharing.

Leave a Reply to Nadine Cancel reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">