Skip to the solution
MediumFrontend11 min read

Build a Flight Booker

736 views

The problem

Question presented to candidate: "Build a small flight booker. The user picks one-way or return, types a departure date and, for a return trip, a return date. Invalid dates should be flagged, the return date only matters for return trips and can never be before departure, and Book should only work when everything is valid. On booking, show a confirmation message."

Requirements:

  • A select with "One-way flight" and "Return flight"; two text inputs in YYYY-MM-DD format, both starting on the same date.
  • The return input is disabled for one-way trips, and its value is kept if the user switches back.
  • Each input shows its own error: empty, wrong format, or a date that does not exist (for example 2026-02-30).
  • For return trips, the return date must be on or after the departure date; show the trip length in nights when valid.
  • Book is disabled while anything is invalid; booking shows "You have booked a one-way flight on ..." or "... a return flight departing ... and returning ...". Editing anything clears the old confirmation.

What a strong answer should cover:

  • Minimal state (trip type, two strings, the confirmation) with everything else derived: errors, whether Book is enabled, nights.
  • Strict date parsing: why new Date("2026-02-30") is not a validator (it rolls over), and why parsing date-only strings can shift a day across time zones.
  • Cross-field validation (return vs departure) that only runs when both fields are individually valid.
  • Constraints between widgets: disabling the return input, keeping its value, and ignoring its errors for one-way trips.
  • Accessibility: labels, aria-invalid, aria-describedby, and a role="status" confirmation.

Clarifying questions expected:

  • "Should I use a native date picker or text inputs with a fixed format?"
  • "Is a departure date in the past allowed, and whose time zone defines today?"
  • "Is a same-day return allowed?"

Code / implementation expected: Yes. The booker component with a strict date parser, derived validation and enable rules, and a confirmation message.

  • Forms
  • Validation
  • Dates

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read ยท 10 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import FlightBooker from "./src/components/FlightBooker";
import "./styles.css";

export default function App() {
  return (
    <main className="page">
      <FlightBooker />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 45 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a File Explorer (tree view)

Continue