Programming Interview Question

Problem is a moderately easy to solve, you should be able to determine the right answer well within a half-hour if you think about it. I think it makes a good interview question, reveals more about the approach to the problem than the brilliance of the person solving it.

Problem:
Your required to write an interface, the spec is:
public List getItemsFromServer(int beginIndex, int endIndex)
Valid values for beginIndex and endIndexes are anywhere on the scale of integers.

beginIndex = 0
endIndex = -1
fetch all

beginIndex = 0
endIndex = 99
Fetch the latest 100

beginIndex = -1
endIndex = -1
fetch all you got

beginIndex = 9
endIndex = 88
Should make sense to you now 🙂

The method fetches an ordered list of items from a server. However, the request to the server looks like this:
http://server/requestItems?count=x&start=y

count is the number of items you want.
start is the starting offset and is only valid from 1 – total (# of items on the server). You may assume that you are aware of the total count of items on the server. start is the oldest and total is the newest.

1. Write the algorithm to compose the request.
2. Test the algorithm.
3. endIndex may exceed the range specified, now test the algorithm