Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Build an insertion-ordered O(1) set

Hardware interview practice

Build an insertion-ordered O(1) set

HardData StructuresPYTHON

Implement a set that preserves insertion order while supporting average O(1) insert, remove, and contains. Iteration must return surviving elements in their original insertion order.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Reviewed example

Work through one case

Input
insert(A), insert(B), insert(C), remove(B), insert(B)
Expected output
iteration order = [A, C, B]

Removing B unlinks its original node, and reinserting it creates a new most-recent insertion at the tail.

What to cover

Requirements

  1. Use a hash map for membership and direct node lookup.
  2. Use a doubly linked list so removing a middle element does not reorder the survivors.
  3. Ignore duplicate insertion and define reinsertion after removal as a new insertion at the end.
  4. Keep the map keys and list nodes in one-to-one correspondence.
Continue practicing

Related questions

Data StructuresImplement an O(1) LRU cache→
asic.fyi · Learn silicon end to end.info@asic.fyi