Reported March 2023
IMCsimulation

Busy Intersection

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

The IMC OA reported in March 2023 hands you a traffic simulation called Busy Intersection, and the catch sits in the constraints. Arrival times go up to 10^9, so ticking through every second is dead on arrival. You have two FIFO queues, one car crosses per second, and a priority rule that depends on what crossed last. It's a queue simulation with time jumps. If your mind goes blank on the jump logic, StealthCoder is the invisible safety net that reads the problem and gives you a working solution live.

The problem

Two one-way streets meet at a single-lane intersection. Cars arriving from Main Street have direction 0, and cars arriving from 1st Avenue have direction 1. Each street has its own first-in, first-out queue, and exactly one waiting car can cross during each second.
For car i, arrival[i] is the second when it reaches the intersection and direction[i] is its street. Cars are indexed in input order. When multiple cars from the same street arrive at the same second, the lower-indexed car is earlier in that street's queue.
At each second, use these rules:
If only one street has waiting cars, the first car from that street crosses.
If both streets have waiting cars and no car crossed during the previous second, the first car from direction 1 crosses.
If both streets have waiting cars and a car crossed during the previous second, the first car from the same direction as that previous car crosses.
Return an integer array result where result[i] is the second when car i crosses the intersection.

Function
getResult(arrival: int[], direction: int[]) → int[]

Examples
Example 1
arrival = [0,0,1,4]
direction = [0,1,1,0]
return = [2,0,1,4]
At second 0, both streets have a waiting car and the previous second was idle, so car 1 from direction 1 crosses. Car 2 arrives at second 1 and direction 1 keeps priority, so it crosses next. Car 0 then crosses at second 2. No car is waiting at second 3, and car 3 crosses when it arrives at second 4.
Example 2
arrival = [0,1,1,3,3]
direction = [0,1,0,0,1]
return = [0,2,1,4,3]
Car 0 is alone at second 0 and crosses. At second 1, cars 1 and 2 are both waiting, so direction 0 retains priority and car 2 crosses. Car 1 crosses at second 2. Cars 3 and 4 arrive at second 3; because direction 1 crossed in the previous second, car 4 crosses before car 3.

Constraints
1 <= arrival.length <= 10^5
direction.length == arrival.length
0 <= arrival[i] <= 10^9
arrival is sorted in nondecreasing order.
direction[i] is either 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to simulate events, not seconds. Keep two queues of car indices and a pointer into the sorted arrival array. At the current time t, push every car with arrival <= t into its queue. If both queues are empty, jump t straight to the next arrival and reset the last-crossed state to idle. Otherwise pick the direction: if only one queue has cars, use it. If both do, use direction 1 when the previous second was idle, else the previous direction. Pop that car, record t, set last direction, and increment t. The classic pitfall is forgetting that a jump over idle time means the previous second was idle, so direction 1 wins again. Also watch the same-second tie: lower index goes first, which pushing in input order gives you for free. Total work is O(n). StealthCoder is your hedge if the idle-reset edge case slips away mid-assessment.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Busy Intersection cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Busy Intersection FAQ

How hard is Busy Intersection really?+

Medium. There's no fancy algorithm, just careful simulation. The difficulty is the priority rules and the idle-gap handling. If you code the rules exactly as written and test both examples, you'll catch most bugs fast.

What's the trick to avoid timing out?+

Never loop second by second. With arrivals up to 10^9, you jump the clock to the next arrival whenever both queues are empty. Every car is processed once, so the whole thing runs in O(n) with two queues.

What happens to priority after an idle second?+

If no car crossed in the previous second, direction 1 gets priority when both queues have cars. After you jump over a gap, treat the state as idle. Forgetting this reset is the most common wrong answer.

Which data structures should I use?+

Two plain queues holding car indices, plus an index pointer into the arrival array. Since arrival is already sorted and you add cars in input order, same-second ties resolve correctly without extra sorting.

How do I prepare for this in 48 hours?+

Write the simulation once from scratch and run both examples by hand. Then test edge cases: a single car, all cars arriving at one time, big gaps between arrivals, and alternating directions. Check the idle reset every time.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it