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?

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

importjava.util.Date;

/**

* This class represents a fixed hourly schedule.

*

* It can tell you the next start time for an event after a given point in

* time.

*

* You first define the minutes past each hour that an event can

* occur (for example HH:00, HH:15, HH:30, and HH:45). Then you call

* {@link #getNextTime(long)} to find the next occurrence from any point

* in time. In the above scenario, passing in 4:23 PM today should return

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.

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.

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?

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

packagedev.blake.dele;

import java.util.Date;

/**

* This class represents a fixed hourly schedule.

*

* It can tell you the next start time for an event after a given point in time.

*

* You first define the minutes past each hour that an event can occur (for

* example HH:00, HH:15, HH:30, and HH:45). Then you call

* {@link #getNextTime(long)} to find the next occurrence from any point in

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.