Circling the Garage
A camera at a parking garage entrance logs the plate of every car that drives in, in order. A driver who finds no free spot leaves by the exit ramp and often comes straight back in.
The manager wants to detect this, which means finding the same plate entering twice within a few log entries. The same car returning hours later is a normal visit and does not count as circling.
Your task
Write circlingCar(plates, k). Return true if there are two positions i < j with plates[i] === plates[j] and j - i ≤ k. Otherwise return false.
An empty log returns false. With k = 0 no pair can qualify, so the answer is false.
circlingCar(plates: int[], k: int) → bool
Examples
plates = [512, 77, 903, 77, 640] k = 2
true
Plate 77 entered at positions 1 and 3. The gap is 3 - 1 = 2, which is within k = 2, so the answer is true.
plates = [512, 77, 903, 640, 512] k = 3
false
Plate 512 entered at 0 and 4. 4 - 0 = 4 is more than k = 3, so that was a normal second visit. No other plate repeats, so the answer is false.
plates = [8, 1, 2, 3, 8, 4, 8] k = 2
true
Plate 8 entered at positions 0, 4 and 6. The gaps from the first visit, 4 and 6, are too big, but the visits at positions 4 and 6 are only 2 apart, so the answer is true.
Constraints
- 0 ≤ plates.length ≤ 100,000
- 0 ≤ plates[i] ≤ 1,000,000,000
- 0 ≤ k ≤ 100,000
Start with brute force
Compare each entry with the next k entries and return true on a match.
That is about n × k comparisons: billions with 100,000 entries and k = 100,000. Each entry rescans cars that earlier entries already looked at.
Hints
0 of 4 shown- Hint 1
In
[3, 9, 3]withk = 2, the 3s are at positions 0 and 2. At position 2, what single fact about plate 3 do you need from the past? - Hint 2
Only the most recent earlier visit matters. If it is too far back, every older visit is further still.
- Hint 3
Keep one number per plate: the position where you last saw it. Which structure gives you that in one step?
- Hint 4
Walk the log with a map from plate to last position. At each position
j, if the plate is in the map andj - last ≤ k, returntrue. Either way, update the map toj. Returnfalseat the end.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
This is the duplicate check from Double-Scanned Tickets with a distance limit. A set is no longer enough: you need where you last saw a plate, so store the position.
Trace [8, 1, 2, 3, 8, 4, 8] with k = 2:
- At positions 0 to 3 the plates 8, 1, 2 and 3 are all new, so the map becomes 8 → 0, 1 → 1, 2 → 2, 3 → 3.
- At position 4 the plate is 8, last seen at position 0. The gap is 4, which is greater than 2, so this is not circling, and the map entry for 8 is updated to 4.
- At position 5 the plate is 4, which is new, so the map gets 4 → 5.
- At position 6 the plate is 8, last seen at position 4. The gap is 2, which is within k, so the function returns
true.
The update in step 2 is the important line. If the map kept the first position, 8 → 0, the last visit would see a gap of 6 and the function would wrongly return false.
function circlingCar(plates, k) {
const last = new Map(); // plate -> position of its latest entry
for (let j = 0; j < plates.length; j++) {
if (last.has(plates[j]) && j - last.get(plates[j]) <= k) return true;
last.set(plates[j], j); // this is now the latest visit
}
return false;
}def circling_car(plates, k):
last = {} # plate -> position of its latest entry
for j, plate in enumerate(plates):
if plate in last and j - last[plate] <= k:
return True
last[plate] = j # this is now the latest visit
return FalseRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.