easyArrays & HashingGoogleAmazonMetaAppleMicrosoft
Concert Gate Scanner
A box-office scanner beeps each guest's ticket number at the gate. Raise a duplicate-use alert if any ticket beeps twice.
You run the entrance scanner for a concert. Guests arrive one after another, and the scanner reads each ticket's integer code into a list in the order it is scanned. If any ticket code appears twice during the night, flag it as a duplicate use and return `true`. If every scan is unique, return `false`.
Choose approach
37
042
191
242
366
415
5Setseen
empty
1/9
Narrationstep 1 of 9
Open an empty log of seen tickets. We'll walk the night in one pass.
Variables
i=—
ticket=—
result=scanning…
How to think about it
- What exactly is being asked? Any repeat means immediate alert — so we don't need counts, just a yes/no.
- The naive move: compare each ticket to every later one. Works but gets slow fast.
- Can we remember what we've seen? A set gives O(1) membership check in exchange for O(n) memory.
- Walk once. On each scan: have we seen this ticket? If yes → alert. If no → log it, keep going.
- Return `true` the moment a repeat shows up. Return `false` only after a full clean pass.
Edge cases to catch in interview
- Empty list of scans — no tickets, no duplicates. Return `false`.
- Single scan — nothing to compare against. Return `false`.
- Duplicate at the very first opportunity (positions 0 and 1).
- Duplicate right at the end — algorithm still has to look at every ticket first.
- All identical tickets — the alert fires on the second scan.
Examples
Example 1
true{"tickets":[37,42,91,42,66,15]}
Ticket 42 is scanned at position 1 and again at position 3.
Example 2
false{"tickets":[11,22,33,44,55]}
Every scan is a different number — a clean night.
Example 3
true{"tickets":[8,8,8,8]}
The second scan is already a duplicate.
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed