r/chalkinterview • u/Uahakan • 11d ago
Library question Microsoft coding question: First value seen twice
Scan left to right and return the first value whose earlier copy was already seen.
You help maintain an order feed that shows orders in the order they arrive. Spotting the first repeated order ID helps the team catch duplicate entries quickly.
You get:
order_ids: integer order IDs in arrival order, e.g.[8, 3, 8]
Return the ID whose second appearance comes earliest in the feed:
- Scan from left to right, since that is the order the feed received the IDs.
- If several IDs repeat, return the one with the earliest second appearance; two IDs cannot have the same second-appearance position.
- If the feed is empty or no ID repeats, return
-1, since there is no repeated order to flag.
For example, with order_ids = [8, 3, 5, 2, 3, 8]:
- The first
8and first3are new IDs. - The next
3has appeared before, so it is the first repeat. Return
3.def first_repeated_order_id(order_ids: list[int]) -> int
Source: reported by a candidate in a public write-up. The wording above is ours.
Practise it out loud with follow-ups: https://chalkinterview.com/questions/microsoft-first-value-seen-twice/?utm_source=reddit&utm_campaign=sub
How would you answer it? Drop your approach below.