9. Find the Missing IDs
Difficulty: Easy · Topic: Core Python & data structures
A source system says which order ids it sent; the warehouse lists the ids it received. Write missing_ids(expected, received): return the ids that are in expected but not in received, sorted in ascending order, each id once.
- Either list may contain duplicates, and either may be empty.
- Ids that were received but never expected are not missing: ignore them.
Example: missing_ids([1, 2, 3, 4], [2, 4]) gives [1, 3].
Your code runs in CPython on our servers against 7 tests, including hidden ones.
Sample test
missing_ids([1, 2, 3, 4], [2, 4])Expected: [1, 3]
Hints
Hint 1
A set answers "is this id in there?" in O(1), and duplicates disappear.Hint 2
The- operator on two sets keeps what is only in the first one. Sets have no order, so sort the result.Learn the concepts
- Dict, set and list methods · 7 min read. get, setdefault, views, set operations, append versus extend, remove and slicing, with the traps in each.
More Python problems
- Flatten a Nested List · Easy
- Squares of the Even Numbers · Easy
- Top K Frequent Words · Easy
- Invert a Dictionary · Easy
- Merge Dicts and Sum Shared Keys · Easy
- Group Records by Key · Easy
- Count Words in Reviews · Easy
- Zip Columns into Records · Easy
- Safe Min and Max · Easy
- Sort by Several Keys · Medium
- Fix the Mutable Default Argument · Medium
- Override a Config Without Changing It · Medium
- Keep the Last N Events · Medium