← Questions

Keyboard and mouse input system

0
AmazonOO designLast asked Sep 2025

An object-oriented design interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests object-oriented design and events.

Design an input system that records what a user does with the keyboard and the mouse and can tell, at any time, exactly what text the user entered. It must handle several input devices, keep all their events in one order, store an event log, and replay that log to rebuild the final text.

Requirements

  • Functional: capture key down, key up and auto-repeat, modifier keys, Backspace and Delete; mouse clicks, double-clicks (word selection), drags (text selection), movement and scrolling; copy, cut and paste; undo and redo; text from the operating system's text-input pipeline, including IME composition (start, update, commit) and dead keys, never guessed from raw key codes; record_event(event), current_text(), selection_range(), undo(), redo(), start_session(), end_session(), replay(log).
  • Non-functional: low latency per event; one total order of events across devices; deterministic replay; recovery after a crash without losing text; correct handling of Unicode grapheme clusters (emoji, combining marks).

Clarifying questions

  • One application window, or input across the whole system?
  • Which platforms? IME behaviour differs between them.
  • Is the text a single field or a whole document?
  • Should mouse movement be stored at full rate, or only what affects text?

What a strong answer covers

  • Event models in layers: raw device events, normalised events (device id, sequence number, monotonic timestamp), and high-level edit operations (insert text, delete range, move cursor, select, paste), plus a session.
  • Classes: InputDevice sources, an EventQueue that assigns the total order, a TextModel (text, cursor, selection), an EditCommand per operation with apply and undo, an UndoStack that groups typing into transactions, a Clipboard, and an EventLog. New devices or event types plug in as new sources and commands.
  • Text from committed text-input and composition events, so IME and dead keys are right; cursor movement and deletion by grapheme cluster, not by byte or code unit.
  • Robustness: coalescing mouse moves, double-click detection by time and distance, button bounce, and dropping duplicated events by sequence number.
  • Persistence: an append-only log plus periodic snapshots, so replay from the last snapshot rebuilds the text after a crash.
  • Concurrency: one thread applies events in order; device threads only enqueue.
  • Tests: replaying recorded sessions and comparing with the expected text, including IME and emoji cases.

Follow-ups

  • Two devices send events with the same timestamp. How do you order them, and does it matter?
  • Support collaborative editing by two users at once.
  • The log is huge after a long session. How do you keep replay fast?

Where it's asked

  • Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

object-oriented design · events

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing