Skip to content
Sharp

Collections interview questions

List, Dictionary, HashSet, concurrent collections, complexity.

3 questions with answers and explanations · 2 flashcards

Key ideas

  • List<T>: dynamic array. O(1) index and amortized O(1) Add, O(n) Contains/insert.
  • Dictionary<TKey,TValue> and HashSet<T>: O(1) average lookup; keys need consistent Equals/GetHashCode.
  • SortedDictionary: balanced tree, O(log n).
  • Don't modify a List<T> while foreach-ing it. Use ConcurrentDictionary etc. for multi-threaded access.

Questions

  1. Choose the typical (average) time complexity of each operation.Matrix / gridMedium
  2. Several threads add and read cached values by key at the same time. Which collection fits best?Multiple choiceEasy
  3. What happens when this code runs?What does this code output?Medium