Last 4 In Order
Create a public class called Last4Ordered
.
You should expose two public methods:
add
: adds a value, does not return a valuelast
: returns an array containing the last 4 values that were added in the order they were added.
You do not need a constructor, but you can add an empty one if you need. Until 4 values have been added you should return 0s in their place.
For example, here's how an instance of Last4Ordered
should work:
Do not create a huge array to save the values. Submissions that do will be marked incorrect.
This problem is harder than it looks! A suggestion is to handle the cases until the array is initially filled separately. Once four values have been added, you will need to shift values around before adding a new one.