Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
A feed delivers each event at least once, and some events are delivered again after a retry. An engineer wants to find the first event that was never repeated, to see where the first clean delivery was.
Write first_unique(event_ids). event_ids is a list of ids. Return the first id that appears exactly once in the whole list, or None if there is none.
Function to write
first_unique(event_ids: list) -> object | NoneThe first id that appears exactly once in the whole list, or None.
How to approach it
Count every id first. Then walk the list in its original order and return the first id whose count is 1.
Sample cases
+ 3 held back until you submit
a retry in the middle
`e1` and `e2` each appear twice. The first id that appears once is `e3`.
Input
Argument 1
[
'e1',
'e2',
'e1',
'e3',
'e2',
'e4'
]Returns
'e3'the first one is unique
`a` appears once and comes first, so it is the answer.
Input
Argument 1
[
'a',
'b',
'b'
]Returns
'a'every id repeats
No id appears exactly once, so the answer is None.
Input
Argument 1
[
'x',
'y',
'x',
'y'
]Returns
None
empty list
Nothing to look at, so None.
Input
Argument 1
[] (empty list)
Returns
None
Constraints
None if no id is unique, or if the list is empty.1 and "1" are different ids.list.count inside a loop.Worked example
Take ['e1', 'e2', 'e1', 'e3', 'e2', 'e4']. First count every id: e1 2, e2 2, e3 1, e4 1. Now scan the list in order. e1 has a count of 2, so skip. e2 has 2, so skip. e1 again, skip. e3 has a count of 1, so it is the answer.
The trap: you cannot decide while you read, because an id that looks unique now may repeat later. That is why you count first and scan second.
What this tests
Count first, then scan, the idea behind 'First Unique Character in a String'. A Counter gives each count in O(1). The second pass keeps the original order, which a Counter alone does not guarantee as an answer. Using list.count inside the loop gives O(n²), which is the version that times out on a large feed.
first_unique(event_ids: list) -> object | NoneSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.