InterviewDB Experience

Stop Sequence: Validate and Reorder Transit Stops Along a Route

Interview Experience

Problem

You are given a list of bus stops with their positions along a route (in km from route start) and a list of stop names in a claimed order. Validate whether the claimed order is consistent with the physical positions (i.e., monotonically increasing distance). If not,

return the corrected order sorted by distance.

python
from dataclasses import dataclass

@dataclass
class Stop:
    name: str
    distance_km: float

def validate_stop_sequence(
    stops: list[Stop],
    claimed_order: list[str]  # stop names
) -> tuple[bool, list[str]]:

**Returns** (is_valid, corrected_order)
    pass

Example:

stops = [Stop("A",0), Stop("B",5), Stop("C",10), Stop("D",15)]
claimed_order = ["A", "C", "B", "D"]
-> (False, ["A", "B", "C", "D"])

claimed_order = ["A", "B", "C", "D"]
-> (True, ["A", "B", "C", "D"])

Follow-ups

  1. How do you handle stops that are not in the provided stops list -- raise an error or skip?
  2. What if two stops are at the same distance (e.g., two doors of the same station)? How do you break ties in the corrected order?
  3. For a circular route (last stop connects back to first), how does the validation logic change?
  4. How would you compute the minimum number of swaps needed to transform the claimed order into the correct order?

Full Details

Problem

You are given a list of bus stops with their positions along a route (in km from route start) and a list of stop names in a claimed order. Validate whether the claimed order is consistent with the physical positions (i.e., monotonically increasing distance). If not,

return the corrected order sorted by distance.

python
from dataclasses import dataclass

@dataclass
class Stop:
    name: str
    distance_km: float

def validate_stop_sequence(
    stops: list[Stop],
    claimed_order: list[str]  # stop names
) -> tuple[bool, list[str]]:

**Returns** (is_valid, corrected_order)
    pass

Example:

stops = [Stop("A",0), Stop("B",5), Stop("C",10), Stop("D",15)]
claimed_order = ["A", "C", "B", "D"]
-> (False, ["A", "B", "C", "D"])

claimed_order = ["A", "B", "C", "D"]
-> (True, ["A", "B", "C", "D"])

Follow-ups

  1. How do you handle stops that are not in the provided stops list -- raise an error or skip?
  2. What if two stops are at the same distance (e.g., two doors of the same station)? How do you break ties in the corrected order?
  3. For a circular route (last stop connects back to first), how does the validation logic change?
  4. How would you compute the minimum number of swaps needed to transform the claimed order into the correct order?

About This Question

This is a candidate experience report from a nuro interview during the phone round.

It covers the following topics: Coding, Onsite, Phone .