CodeArchon

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
0
42
1
91
2
42
3
66
4
15
5
Setseen
empty
1/9
Narrationstep 1 of 9

Open an empty log of seen tickets. We'll walk the night in one pass.

1def has_duplicate_scan(tickets: list[int]) -> bool:
2 seen: set[int] = set()
3 for ticket in tickets:
4 if ticket in seen:
5 return True
6 seen.add(ticket)
7 return False
Variables
i=—
ticket=—
result=scanning…

How to think about it

  1. What exactly is being asked? Any repeat means immediate alert — so we don't need counts, just a yes/no.
  2. The naive move: compare each ticket to every later one. Works but gets slow fast.
  3. Can we remember what we've seen? A set gives O(1) membership check in exchange for O(n) memory.
  4. Walk once. On each scan: have we seen this ticket? If yes → alert. If no → log it, keep going.
  5. 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 1true
{"tickets":[37,42,91,42,66,15]}

Ticket 42 is scanned at position 1 and again at position 3.

Example 2false
{"tickets":[11,22,33,44,55]}

Every scan is a different number — a clean night.

Example 3true
{"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