Busy Intersection
Updated
Problem statement
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
1crosses. - 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^5direction.length == arrival.length0 <= arrival[i] <= 10^9arrivalis sorted in nondecreasing order.direction[i]is either0or1.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Realized PnL with Oldest Inventory FirstBelvedere Trading · OA
- Buffet Day GainsUpstart · OAUpstart interview questions
- Bank Requests with Delayed CashbackRamp · OARamp interview questions
- Capital Gains Tax from TradesArcesium · OAArcesium interview questions
- Paginate Search Results by Unique HostAirbnb · OAAirbnb interview questions
- Incident MonitorStripe · OAStripe interview questions
- Chess TournamentJP Morgan Chase · OAJP Morgan Chase interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions