THN Interview Prep

Data Structure Operations — Go ↔ JS

Average / amortized unless labeled worst. Interview answers usually cite average unless skew/adversarial input is the point.

StructureTypical operationsAmortized / average complexityGo stdlib anchorJavaScript equivalent
Dynamic arraypush / pop at end, index read/writeO(1) end; worst O(n) if growth copiesslice built-inArray (push/pop)
Singly linked listinsert-after, delete-next with predecessorO(1) with pointer; search O(n)container/list (also doubly)hand-rolled { value, next }
Doubly linked listinsert, delete, spliceO(1) with node ref; index O(n)container/listhand-rolled or Map as adjacency (rare)
Stackpush, pop, peekO(1)slice as stack; list.ListArray as stack (push/pop)
Queue / dequeenqueue, dequeue, sometimes both endsO(1)container/list; slice ring buffer (custom)Array (shift is O(n)—use index head or deque pattern)
BST (unbalanced)search, insert, deleteavg O(log n); worst O(n)no std tree; sort + slice or customno std; hand-rolled or npm (not in interview)
Balanced BST (concept)same ops, height boundworst O(log n)no built-in; red-black in Tree not stdno std; use SortedMap patterns sparingly
Hash mapget, set, deleteavg O(1); worst O(n)map built-inMap / plain object (string keys)
Binary heappush, pop min/maxO(log n); build O(n)container/heaparray + heapify (see JavaScript idioms)
Trieinsert, prefix queryO(m) per string length mno std; custom struct childrennested Map or object children
Union-findfind, unionamortized α(n)no std; custom DSUcustom DSU
Segment tree (conceptual)range query / point updateO(log n) per opno std; customcustom (or sparse patterns)

Notes on “stdlib anchor”

  • Go favors slices + maps + container/list + container/heap; trees and segment trees are almost always custom for interviews.
  • JavaScript has no native heap, trie, or DSU—express with Array, Map/Set, and classes.

When to pick which (10 bullets)

  1. Need order + frequent middle inserts? Doubly linked list (or container/list); dynamic array shifts are expensive.
  2. Only stack discipline (LIFO)? Slice / array end push/pop; simplest and cache-friendly.
  3. FIFO queue and O(1) both ends? Deque pattern (Go list or ring buffer); avoid JS shift() in hot loops.
  4. Keyed lookups dominate? Hash map (map / Map); tree only if you need sorted keys.
  5. Sorted keys / order statistics? Balanced BST (often simulated by sort + binary search on arrays in interviews).
  6. Top-k / streaming extremes? Heap ( container/heap / array heap).
  7. Prefix / autocomplete strings? Trie (children maps or fixed arrays for small alphabet).
  8. Components, cycles, Kruskal? Union-find (path compression + union by rank).
  9. Range sums on static array? Prefix sums; dynamic ranges with updates → segment tree / Fenwick (BIT) conceptually.
  10. Graph unweighted shortest path? BFS; non-neg weighted → Dijkstra + heap; negative edges → Bellman-Ford (see Big-O complexity).

See also: Big-O complexity, Go idioms, JavaScript idioms.

Last updated on

Spotted something unclear or wrong on this page?

On this page