Amazon Phone Screen SDE 2

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-04-30Added By Anonymous
Source: Link

Company: Amazon
Role: SDE2
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

A fixed size circular array buffer is used to store integer timestamps of events being triggered from
another system so that the timestamps of the most recent N events are always stored in the buffer
where N is the size of the buffer.

Each timestamp written will always be greater [in scalar value] than all the timestamps written before
it.

When the buffer is initialized, timestamps are written sequentially into the buffer array until the
end of the array is reached and then it begins sequentially writing from the first index again, hence
the circular nature of it.

Write a function that, given such a buffer that has already been written to, will scan the buffer and
return the index of the array in the buffer where the next event timestamp should be written based on
the values that are already stored.

The buffer can be any size and your solution should be optimized for large buffers.

Examples:
Input: [ 47, 82, 100, 8, 13]
Output: 3
The index with the scalar value of 8 is where the next timestamp should go, as '100' would be the most
recently written timestamp and the next timestamp should be written directly after it.

Input: [ 3, 7, 15, 78, 103 ]
Output: 0
'103' is the most recently written timestamp, but it's at the end of the array, therefore the next
value should be written at the start of the array as per the circular buffer behavior.

Comments