Validate a Rider Trip Lifecycle Event Sequence

Last updated: November 29, 2026

Quick Overview

A trip emits events such as requested, accepted, arrived, started, completed and cancelled. Given the ordered events for one trip, return the index of the first illegal transition, len(events) if the trip never reaches a terminal state, or -1 if the sequence is valid.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A trip emits events such as requested, accepted, arrived, started, completed and cancelled. Given the ordered events for one trip, return the index of the first illegal transition, len(events) if the trip never reaches a terminal state, or -1 if the sequence is valid.

A state machine warm-up in the style of Uber's phone screens, built on the real trip lifecycle that every rider and driver app depends on. The interviewer looks for an explicit transition table instead of a tangle of if statements.

What the Interviewer Expects
  • Writes the allowed transitions as data, not nested conditionals
  • Rejects any event after a terminal state
  • Distinguishes an illegal step from an unfinished trip
  • Handles an empty sequence
  • Keeps the function easy to extend with a new state
Key Topics to Cover
State machines
Hash maps
Validation
Simulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add a driver_cancelled state with a different fee rule?
  • How would you validate millions of trips streaming from Kafka where events can arrive out of order?
  • How would you report every illegal step instead of the first one?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

States: requested -> accepted -> arrived -> started -> completed, plus cancelled allowed from requested, accepted and arrived (not after the trip has started). The first event must be requested....

High-Level Design

Model the machine as a dictionary from current state to the set of allowed next states, with a virtual start state whose only successor is requested. Walk the events once. If an event is not in `a...


Submit Your Answer
Markdown supported