Passing Cars
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip the road and the cars away and this Bloomberg OA question, reported in November 2021, is a counting problem in disguise. You're tallying pairs where a 1 appears before a 0 in an array. That's it. The trap is writing a nested loop on an input that can hit 200,000 elements, then watching it time out. If you've got the OA coming in a day or two, this is a one-pass problem once you see it. And if you blank mid-assessment, StealthCoder runs invisibly on your screen and hands you the approach in real time.
The problem
Each value in directions describes a car's travel direction along a two-way road: 1 travels east and 0 travels west. Return the number of pairs (i,j) with i < j, directions[i] == 1, and directions[j] == 0. Function countPassingCars(directions: int[]) → long Examples Example 1 directions = [1,0,1,0,0,1] return = 5 The first eastbound car passes three westbound cars and the second passes two. Constraints 0 <= directions.length <= 2 * 10^5. Every value is 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a running counter. Walk the array left to right and keep a count of eastbound cars (1s) seen so far. Every time you hit a 0, that westbound car is passed by every eastbound car before it, so add the counter to your answer. One pass, O(n) time, O(1) space. The common pitfall is overflow. With 2 * 10^5 elements, the pair count can reach about 10^10, which blows past a 32-bit int. The function returns a long for a reason, so declare your accumulator as long in Java or C++. Python won't care. Also handle the empty array, since length can be 0. The example [1,0,1,0,0,1] gives 3 + 2 = 5, which is a good sanity check. If your head goes blank on the live OA, StealthCoder is the safety net that surfaces this prefix-count pattern while the proctor sees nothing.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Passing Cars 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Passing Cars FAQ
What's the trick to Passing Cars?+
Keep a running count of 1s as you scan left to right. Each time you see a 0, add that count to the total. Every earlier eastbound car forms a valid pair with that westbound car. No nested loop needed, just one pass.
How hard is this Bloomberg question really?+
Easy once you spot the reduction. The difficulty is recognizing it's a counting problem, not a simulation of cars. Most candidates who start with a double loop realize it's too slow at 200,000 elements and then find the counter approach quickly.
Why does the function return a long?+
The max number of pairs is roughly (n/2)^2, which is about 10^10 for n = 2 * 10^5. That exceeds the 32-bit int limit of about 2.1 billion. Use a 64-bit accumulator in Java, C++, or similar languages to avoid silent overflow.
What edge cases should I test?+
Test an empty array (answer 0), all 1s (0), all 0s (0), and 1s only after all 0s (0). Also test a large alternating input to confirm no overflow. The sample [1,0,1,0,0,1] should return 5.
How do I prepare for this in 48 hours?+
Practice the running-counter pattern on a few array problems where order matters, like counting pairs with a condition on i < j. Write this one from memory in your language, with a long accumulator. It should take under ten lines.