FastPrepPark Train Cars by Preference
Problems/Park Train Cars by Preference

Park Train Cars by Preference

Mercor logoMercor● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report establishes the exercise identity and central capability. This practice contract supplies only the smallest portable interface, deterministic boundary rules, and output representation needed for consistent execution.

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 = 3preferences = [[0,1],[1],[1,2]]return = true

The cars can use spots 0, 1, and 2 respectively.

Example 2

spotCount = 2preferences = [[0],[0]]return = false

Both cars require the same single spot, so one remains unparked.

Example 3

spotCount = 4preferences = [[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.

More Mercor problems

  • Expected Fake-Name CollisionsPHONE SCREEN · Seen Oct 2026
  • Length of a Document Reached Through a Random TokenPHONE SCREEN · Seen Oct 2026
  • Precision of Stacked PII Detector FlagsPHONE SCREEN · Seen Oct 2026
All Mercor interview questionsSee Mercor hiring insights
Saved
class Solution {
    public boolean canParkTrain(int spotCount, int[][] preferences) {
      // write your code here
    }
}
Checking account…