Unit 1 · Algorithms · Lesson 2 of 24
Coding fluency and standard-library costs
Step 1: Learn the ideas
- Dynamic arrays: amortized append, O(n) front operations, slicing copies
- Deques and FIFO queues in the standard library
- Hash maps and sets: average costs, iteration order, hashable keys
- Ordered maps and sets (or their absence) and binary-search helpers
- Priority queues: default order, tie-breaking, no decrease-key
- Sorting: stability, key functions versus comparators, default comparison
- Strings: immutability, builders and joins, character versus byte indexing
- Integers: overflow, division and modulo with negative numbers
- Recursion limits and explicit stacks
- Aliasing and copying: shallow copies and shared backing arrays
Why it matters
A coding interview leaves little time, so the language has to disappear: you should not be looking up how to build a max-heap or whether a sort is stable. Fluency also means knowing what library calls cost. A BFS that uses a plain array as its queue, or a loop that rebuilds a string on every step, turns an O(n) idea into O(n²) code, and interviewers notice. Pick one interview language and know its standard library precisely; the rest of this note lists the facts that matter most for algorithmic code in five common choices.
Core ideas
For each abstract structure (stack, FIFO queue, deque, hash map and set, ordered map, priority queue, sort with a custom order) know the concrete type you will use, its costs, and the traps.
Python. list is a dynamic array: append and pop() are amortized O(1), but pop(0) and insert(0, x) shift every element (O(n)), x in lst scans the list, and slicing copies. Use collections.deque for queues. Strings are immutable, so build them with ''.join(parts); the documentation warns that repeated concatenation can be quadratic. dict preserves insertion order (guaranteed since 3.7). heapq is a min-heap over a list: negate numeric keys for max-heap behavior (Python 3.14 added _max variants), and put a counter in tuples so ties never compare payloads. There is no balanced BST in the standard library: bisect searches a sorted list in O(log n) but inserts in O(n). Sorts are stable. CPython's recursion limit is typically 1000 frames. [[0] * m] * n creates n references to one row.
C++. vector::push_back is amortized O(1), but a reallocation invalidates every iterator, pointer and reference into the vector; call reserve when the size is known. unordered_map is average O(1), a rehash invalidates its iterators, and operator[] inserts missing keys, so test membership with find, count or contains (C++20). map and set are ordered (usually red-black trees) with O(log n) member lower_bound; the free std::lower_bound on their iterators takes linear steps. priority_queue is a max-heap; use std::greater<T> for a min-heap. std::sort is O(n log n), not stable, and requires a strict weak ordering: a <= comparator is undefined behavior. Signed overflow is undefined, so use long long and lo + (hi - lo) / 2.
Java. PriorityQueue is a min-heap by natural order; for a max-heap pass Comparator.reverseOrder(), compare with Integer.compare rather than subtraction (which can overflow), and remember that remove(Object) and contains are linear and iteration is not in priority order. Collections hold boxed values: == on two Integer objects compares references and may pass for small cached values, then fail. HashMap iteration order is unspecified; LinkedHashMap keeps insertion order and TreeMap offers floorKey and ceilingKey in O(log n). Use ArrayDeque for stacks and queues. Object sorts are stable; primitive sorts use a dual-pivot quicksort. int overflow wraps silently.
Go. A slice is a view (pointer, length, capacity) onto a backing array. append writes into spare capacity, so slices cut from the same array can overwrite each other: copy a slice before storing it, for example a backtracking path. Map iteration order is unspecified and can differ between loops; sort the keys when order matters. Sets are map[T]struct{}. Heaps come from container/heap, which you drive through an interface and which offers Fix for priority changes. sort.Search is binary search over a monotone predicate; sort.Slice is not stable, sort.SliceStable is.
JavaScript and TypeScript. sort() without a comparator compares elements as strings, so [10, 9, 1].sort() yields [1, 10, 9]; pass (a, b) => a - b for numbers. Sorting has been stable since ES2019. shift and unshift re-index the whole array, so BFS queues need a head index. Map keeps insertion order, accepts any key type and has size; plain objects coerce keys to strings. Map and Set compare object keys by identity, so encode a coordinate as a string or a number. Numbers are doubles, exact up to 2⁵³ − 1 (BigInt beyond), and bitwise operators work on 32-bit integers. There is no built-in heap, deque or ordered map.
Common misconceptions
- "An array is a fine queue." Removing from the front moves every remaining element in Python lists and Java
ArrayList, and JavaScript'sshift()is specified the same way; use a deque or a moving head index. - "The default sort does what I mean." JavaScript compares numbers as strings, C++
std::sortis unstable, and a comparator that is not a consistent ordering breaks sorting in every language. - "Heaps are min-heaps everywhere." Python and Java default to min-heaps, C++ to a max-heap, and JavaScript has none.
- "Copies happen when I expect them." Python slices and C++ pass-by-value copy in O(n), while Go re-slicing, Python
[[0] * m] * nand JavaScriptArray(n).fill([])share storage and cause aliasing bugs. - "Hash map iteration order is stable." Python
dict, JavaScriptMapand JavaLinkedHashMappromise insertion order; Go maps and JavaHashMapdo not.
How to approach it
Write a one-page cheat sheet for your language from its official documentation: each structure, its costs and three traps. Drill the idioms you use most (frequency count, BFS queue, heap with tuple priorities, two-key sort, iterative DFS) until they take seconds. In the interview, mention costs as you call library functions ("a deque, so popleft is O(1)"). Among the languages a role allows, interviewers care less about which one you pick than about code that fights it.
Suggested order
- 1Choose one interview language and read its official cost references: Python’s time-complexity page plus the collections, heapq and bisect docs; cppreference’s container pages and iterator-invalidation tables; the Java PriorityQueue, HashMap, ArrayDeque and TreeMap docs; the Go slices blog post, sort and container/heap; or MDN’s pages for sort, shift and Map.
- 2Write a one-page cheat sheet listing, for stack, queue, deque, hash map, hash set, ordered map, heap and sort, the type you will use, its operation costs and one trap.
- 3Type the core idioms from memory until each takes under a minute: frequency count, BFS with a proper queue, heap with tuple priorities and a tie-breaker, sort by two keys, iterative DFS.
- 4Solve Last Stone Weight and Top K Frequent Elements using only the library heap, including max-heap behavior in your language.
- 5Solve Largest Number to practice a custom comparator, and Group Anagrams to practice hashable composite keys.
- 6Solve Merge k Sorted Lists with heap entries that never compare node objects, then Time Based Key-Value Store with your language’s binary-search or ordered-map facilities.
- 7Work through part of the LeetCode 75 plan in your language purely for speed, then correct any cost you misstated on your cheat sheet.
Step 2: Check off what you can do
Tap each one you can genuinely do or explain. Each earns 10 XP.
Step 3: Go deeper
Python documentation: costs of built-in types and the container modulesStart here
Python Software Foundation (docs.python.org)Section: Time complexity of operations on built-in types
Official CPython costs for list, dict, set and str; read with the collections, heapq and bisect pages if Python is your interview language.
Open(opens in a new tab)Link responded, 5 Oct 2026cppreference: C++ containers library, priority_queue, sort and lower_boundStart here
cppreference.comSection: Containers library
Per-container complexity and iterator-invalidation rules, plus the std::sort, std::priority_queue and std::lower_bound pages, if C++ is your interview language.
Open(opens in a new tab)Link responded, 5 Oct 2026Java SE 25 API documentation: java.util collections
OracleSection: PriorityQueue
Documented costs and behavior of PriorityQueue, then HashMap, ArrayDeque, TreeMap and Arrays.sort, if Java is your interview language.
Open(opens in a new tab)Link responded, 5 Oct 2026Go documentation: slices, maps, sort and container/heap
The Go Authors (go.dev)Section: Go Slices: usage and internals
How slices share backing arrays and how append grows them, then map iteration order, sort and container/heap, if Go is your interview language.
Open(opens in a new tab)Link responded, 5 Oct 2026MDN: Array.prototype.sort(), shift(), Map and Set
MDN Web DocsSection: Array.prototype.sort()
Default string comparison, comparator requirements and stability, then shift() and Map, if JavaScript or TypeScript is your interview language.
Open(opens in a new tab)Link responded, 5 Oct 2026
Step 4: Practice
Solve these in your interview language with the goal of idiomatic library use rather than new algorithms. Before submitting, list every library call and its cost. Last Stone Weight and K Closest Points to Origin exercise min- versus max-heap behavior, Largest Number exercises comparators, Group Anagrams exercises hashable keys, Merge k Sorted Lists exercises heap tie-breaking, Time Based Key-Value Store exercises binary search or ordered maps, and Design Circular Queue exercises arrays without hidden linear operations. Use the LeetCode 75 plan afterwards for speed, not for new ideas.
- Last Stone WeightLeetCode(opens in a new tab)Not attemptedLeetCode
- Top K Frequent ElementsLeetCode(opens in a new tab)Not attemptedLeetCode
- Group AnagramsLeetCode(opens in a new tab)Not attemptedLeetCode
- Largest NumberLeetCode(opens in a new tab)Not attemptedLeetCode
- K Closest Points to OriginLeetCode(opens in a new tab)Not attemptedLeetCode
- Merge k Sorted ListsLeetCode(opens in a new tab)Not attemptedLeetCode
- Time Based Key-Value StoreLeetCode(opens in a new tab)Not attemptedLeetCode
- Design Circular QueueLeetCode(opens in a new tab)Not attemptedLeetCode
- LeetCode 75 (official study plan)LeetCode(opens in a new tab)Not attemptedLeetCode
Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).
Step 5: Test yourself
Answer each one out loud or on paper before you finish the lesson.
- Question 1Which type do you use for a FIFO queue, and what does removing from the front cost?
- Question 2Is your language’s default priority queue a min-heap or a max-heap, and how do you reverse it?
- Question 3Is your standard sort stable, and what happens with an inconsistent comparator?
- Question 4How do you use a pair such as a grid coordinate as a hash key in your language?
- Question 5What is your recursion or stack limit, and how do you avoid hitting it?
- Question 6Which operations copy data implicitly, and which share storage?
You’re done when you can
- Produce a one-page cheat sheet for your language whose costs you have checked against official documentation.
- Write a grid BFS, a heap-based top-k and a two-key sort from memory without consulting documentation.
- Name three operations in your language that look constant-time but are linear, with their replacements.
- Solve two of the listed problems in under 15 minutes each with no syntax lookups.


