Collections · interview question
Choose the typical (average) time complexity of each operation.
Matrix / gridMedium
Choose for each row.
| Item | O(1) | O(log n) | O(n) |
|---|---|---|---|
| `List<T>.Add` (amortized) | |||
| `Dictionary<TKey,TValue>` lookup by key | |||
| `List<T>.Contains` | |||
| `HashSet<T>.Contains` | |||
| `SortedDictionary<TKey,TValue>` lookup by key |
Answer and explanation
Correct answer
List<T>.Add(amortized): O(1)Dictionary<TKey,TValue>lookup by key: O(1)List<T>.Contains: O(n)HashSet<T>.Contains: O(1)SortedDictionary<TKey,TValue>lookup by key: O(log n)
Why
Picking the right collection is often the whole interview answer: use a HashSet for membership tests, a Dictionary for keyed lookup, and a sorted structure only when you need ordering.
Test yourself on Collections
A free test with instant, explained results. No account needed.