Reported September 2026
Mercorgraph

Park Train Cars by Preference

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

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

The edge case that kills the greedy answer in Mercor's "Park Train Cars by Preference" is the one where an early car grabs a spot a later car can't live without. This showed up as a reported OA in September 2026. It's bipartite matching in disguise: cars on one side, spots on the other, and you need every car matched. If you've seen it, it's quick. If you haven't, it's a trap. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea is simple enough to carry in your head.

The problem

A rail yard has parking spots numbered from 0 through spotCount - 1. Train car i may be parked in any spot listed in preferences[i].
Each spot may hold at most one car. Return whether every train car can be assigned to an acceptable spot. Cars may be reassigned from an earlier choice while searching for a complete placement.

Function
canParkTrain(spotCount: int, preferences: int[][]) → boolean

Examples
Example 1
spotCount = 3
preferences = [[0,1],[1],[1,2]]
return = true
The cars can use spots 0, 1, and 2 respectively.
Example 2
spotCount = 2
preferences = [[0],[0]]
return = false
Both cars require the same single spot, so one remains unparked.
Example 3
spotCount = 4
preferences = [[0,1],[0,2],[1,3],[2,3]]
return = true
Reassigning an earlier car exposes a complete placement.

Constraints
1 <= preferences.length, spotCount <= 200
Each preference list is nonempty and contains distinct valid spot indices.
A car occupies exactly one spot and a spot holds at most one car.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is augmenting paths, also called Kuhn's algorithm. For each car, run a DFS over its preferred spots. If a spot is free, take it. If it's taken, try to move the current occupant to one of its other preferences by recursing. If that works, you take the spot. Keep a visited set per car so you don't loop. Count successful matches, and return true only if every car is placed. The pitfall is plain greedy, which fails Example 3 where reassignment is required. Another one is forgetting to reset the visited array between cars. Also check early: if cars outnumber spots, return false immediately. With sizes up to 200, O(V*E) is fine. Keep matchSpot[] initialized to -1. If you freeze during the live OA, StealthCoder is the hedge that hands you the working matching code.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Park Train Cars by Preference 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Mercor reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Park Train Cars by Preference FAQ

What's the trick in Park Train Cars by Preference?+

Treat it as bipartite matching. Use DFS with augmenting paths: when a preferred spot is taken, try to relocate its current car to another option. If every car gets matched, return true. Plain greedy assignment fails because it never undoes earlier choices.

Is this problem really hard?+

Medium-hard if you haven't seen matching. The code is about 20 lines once you know Kuhn's algorithm. The difficulty is recognizing that reassigning cars is the whole point, which the problem hints at with its wording about earlier choices.

Why does greedy fail here?+

Example 3 shows it. Car 0 takes spot 0, car 1 then takes spot 2, and the rest can still work, but other orderings strand a car. Greedy locks decisions in. You need the ability to push an earlier car to another spot to free one up.

What's the time complexity and will it pass?+

Kuhn's runs in O(cars * edges). With up to 200 cars and 200 spots, that's tiny, roughly a few million operations at worst. No need for Hopcroft-Karp. Just reset the visited array for each car's DFS.

How do I prepare for this in 48 hours?+

Write Kuhn's algorithm from memory twice, then test it on the three examples. Learn the shape: matchSpot array, visited per attempt, recursive try function. Also practice the quick rejection when cars exceed spots. That covers this variant and similar assignment problems.

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

OA at Mercor?
Invisible during screen share
Get it