Embercode
✕
Dicts and SetsQuick quiz

O(1) random set

7 questions. Covers Insert, delete and random pick in O(1) in Python.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``python values = [5, 6, 7, 8] positions = {} for i, v in enumerate(values): positions[v] = i index = positions[6] last_index = len(values) - 1 last_value = values[last_index] values[index] = last_value positions[last_value] = index values.pop() del positions[6] print(values, positions[8]) ``
  2. 2. What does this program print? ``python rs = RandomizedSet() rs.insert(10) rs.insert(20) rs.insert(30) rs.remove(10) print(rs.values, rs.positions[30]) ` The RandomizedSet class uses the swap-with-last removal from the lesson, with values as the list and positions` as the dict.
  3. 3. The remove method swaps the removed value with the last item. Which statements about that swap are true? Pick all that apply.

    Pick every answer that is true.

  4. 4. Which operations take constant time on average? Pick all that apply.

    Pick every answer that is true.

  5. 5. Why does get_random() pick a random index into the list instead of a random key from the dict?

5 question(s) left on this page.