Skip to the page

Foundations · core

The collections you will use

Every coding round uses the same few containers in Java, Python and C++. You must know each one's name, its cost for each operation, and its traps (which heap is a min-heap, integer overflow, negative division, recursion depth).

The notes

What you must know

  • Dynamic array: Java ArrayList (get O(1), add at the end O(1) amortised, add or remove at an index O(n), contains O(n)); Python list (index O(1), append and pop() O(1), pop(0) and insert(0, x) O(n), 'in' O(n)); C++ vector (v[i] O(1), push_back O(1) amortised, insert or erase in the middle O(n), stored in one block of memory).
  • Hash map and hash set: Java HashMap and HashSet (get, put, contains O(1) on average, no order guarantee, HashMap allows a null key; LinkedHashMap keeps insertion order); Python dict and set (average O(1), keys must be hashable so a list cannot be a key but a tuple can, a dict keeps insertion order since Python 3.7); C++ unordered_map and unordered_set (average O(1), worst case O(n), no order, no default hash for std::pair).
  • Sorted map and set (keys kept in order, O(log n)): Java TreeMap (a red-black tree; floorKey, ceilingKey, firstKey) and TreeSet; C++ std::map and std::set (usually a red-black tree; lower_bound, iteration in sorted order). Python's standard library has none: use a sorted list with the bisect module (insert is O(n)) or the third-party sortedcontainers package, and say so in the interview.
  • Queue and deque: Java ArrayDeque (amortised O(1) at both ends, no null elements; use it for a stack or a queue, not the old Stack class); Python collections.deque (append, appendleft, pop, popleft O(1); an index in the middle is O(n)); C++ std::deque (O(1) at both ends and O(1) random access, but not stored in one block), with std::queue and std::stack as thin wrappers.
  • Heap: Java PriorityQueue is a MIN-heap by natural order (offer and poll O(log n), peek O(1), remove(Object) and contains O(n); iterating it does not give sorted order; use Comparator.reverseOrder() for a max-heap). Python heapq is a min-heap kept inside a plain list (heappush, heappop; for a max-heap push the negated value; tuples compare field by field). C++ priority_queue is a MAX-heap by default (top O(1), push and pop O(log n)); for a min-heap write priority_queue<int, vector<int>, greater<int>>.
  • Integer overflow: Java int has 32 bits and wraps silently (Integer.MAX_VALUE + 1 is Integer.MIN_VALUE); use long, or Math.addExact which throws ArithmeticException. In C++ signed overflow is undefined behaviour; use long long. Python int has no limit. Safe middle index: lo + (hi - lo) / 2, not (lo + hi) / 2.
  • Division and remainder with negative numbers: Java and C++ truncate toward zero (-7 / 2 is -3, -7 % 2 is -1); Python // rounds down (-7 // 2 is -4) and % takes the sign of the divisor (-7 % 2 is 1). Java has Math.floorMod and Math.floorDiv for the rounding-down result.
  • Recursion depth: Python stops at about 1000 nested calls by default (sys.getrecursionlimit, then RecursionError; raising it too high can crash the program); Java throws StackOverflowError after some thousands of calls (it depends on the thread's stack size); C++ has no fixed limit and a too deep recursion crashes the program. For deep input use an explicit stack.

The red pen

Common traps: mistake → fix

  • Mistake: Java's PriorityQueue prints its items in sorted order

    Fix: only poll() returns them in order; the internal array and the iterator are in heap order.

  • Mistake: Python's heapq is a max-heap or its own class

    Fix: it is a set of functions on an ordinary list, and the smallest item is always at index 0.

  • Mistake: In C++, map[key] only reads the value

    Fix: if the key is missing it inserts a default value (0 for int), so use find or count to test.

  • Mistake: Python's list.pop(0) is a good queue operation

    Fix: it is O(n); use collections.deque with popleft().

Easy to hard

Questions interviewers ask, easy to hard

  1. Which container do you use as a queue in Java, in Python and in C++, and what does each operation cost?
  2. How do you get a max-heap in Java, in Python and in C++?
  3. You add 100,000 numbers, each up to 10^9, in a Java int. What goes wrong and how do you fix it?
  4. Why can a tuple be a dict key in Python but a list cannot?
  5. What does -7 / 2 give in Java, in C++ and in Python?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview