Track at offset: hints, no solution
Easy · Linked list
A radio scheduler keeps its upcoming tracks as a queue, and each track is a numeric id. The queue is handed to you as an array from head to tail; you may use it directly or build your own linked nodes from it.
Given an offset k, return the id of the track that is k places after the head. Offset 0 is the head itself. If the queue has k or fewer tracks, so there is no such track, return -1. Track ids are never negative, so -1 cannot be confused with a real id.
Think of it as walking the queue node by node, the way a linked list would force you to.
Examples
- Input
- queue = [11, 24, 37, 42], k = 2
- Output
- 37
- Note
- Offsets 0, 1, 2 are 11, 24 and 37.
- Input
- queue = [11, 24], k = 2
- Output
- -1
- Note
- Only offsets 0 and 1 exist, so offset 2 has no track.
Constraints
- 0 <= queue.length <= 600
- 0 <= queue[i] <= 999
- 0 <= k <= 1000
Hint 1: a nudge
If you could only move from one track to the next, how many moves would it take to stand on the one you want?
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.