Study Guide

LeetCode Data Structures Guide — Pick the Right One Every Time

Choosing the right data structure is half the battle in coding interviews. This practical guide covers every data structure that matters for LeetCode, with the exact situations where each one shines and a decision framework you can use under pressure.

11 min read|

The right data structure turns a hard problem into an easy one

A practical decision framework for every interview data structure

The Right Data Structure Turns a Hard Problem Into an Easy One

Most candidates who struggle with leetcode data structures are not failing because they cannot code. They are failing because they pick the wrong data structure and spend twenty minutes fighting their own solution. The difference between a clean O(n) answer and a messy O(n squared) brute force often comes down to a single choice made in the first sixty seconds of reading the problem.

Think about it this way: Two Sum is trivial with a hash map and painful with nested loops. Finding the median of a stream is elegant with two heaps and nearly impossible without them. Detecting a cycle in a linked list is instant with Floyd's algorithm and awkward with a visited set. The data structure IS the solution.

This guide gives you a practical decision framework for every data structure that matters in coding interviews. For each one, you will learn what it does, when to reach for it, what problems it solves, and — just as importantly — when NOT to use it. By the end, you will have a mental flowchart that turns data structure selection from guesswork into a systematic process.

Arrays and Strings — The Foundation of Every Interview

Arrays are the most fundamental data structure for interviews and the one you will use most often. They offer O(1) random access by index, which makes them perfect for problems that require looking up, swapping, or iterating over elements in a predictable order. Dynamic arrays (like Python lists or JavaScript arrays) handle resizing automatically, so you rarely need to worry about capacity.

Strings in most languages are essentially character arrays with some extra methods. For interview purposes, treat them the same way — iterate by index, use two pointers, apply sliding window. The key difference is that strings are immutable in many languages, so building a result character by character requires a list or StringBuilder to avoid O(n squared) concatenation.

When you see a problem involving contiguous elements, subarrays, subsequences, or in-place modification, arrays are your starting point. Techniques like two pointers, sliding window, prefix sums, and binary search all operate directly on arrays. Problems like Best Time to Buy and Sell Stock (#121), Product of Array Except Self (#238), and Maximum Subarray (#53) are classic array problems.

  • O(1) access by index — fastest random access of any data structure
  • O(n) search for unsorted arrays — use binary search on sorted arrays for O(log n)
  • O(1) amortized append — dynamic arrays handle resizing automatically
  • O(n) insert or delete at arbitrary position — elements must shift
  • Best for: contiguous data, ordered iteration, two pointers, sliding window, prefix sums
  • Watch out for: off-by-one errors, in-place modification that corrupts unprocessed data

Hash Maps and Sets — O(1) Lookup Changes Everything

If there is one data structure that solves more leetcode data structures problems than any other, it is the hash map. The reason is simple: O(1) average-case lookup eliminates the need for nested loops. Any time you find yourself writing a brute-force solution with two nested for loops to find a pair or check for existence, a hash map can almost certainly reduce it to a single pass.

Hash maps store key-value pairs with constant-time insert, lookup, and delete. Hash sets are the same idea without values — they just track membership. Use a hash map when you need to associate data (like frequency counting, index tracking, or grouping), and use a hash set when you only need to know whether something exists.

The Two Sum pattern is the canonical example: instead of checking every pair in O(n squared), store each number's complement in a hash map as you iterate, and check if the current number's complement already exists. This one trick — "have I seen the thing I need before?" — applies to dozens of problems. Group Anagrams (#49), Longest Consecutive Sequence (#128), and Contains Duplicate (#217) all follow the same core idea.

When deciding between a hash map and a sorted structure like a tree map, ask yourself: do I need the data ordered? If you need to find the minimum, maximum, or iterate in sorted order, a tree map (balanced BST) gives O(log n) operations with ordering. If you only need fast lookup, the hash map wins every time.

  • O(1) average insert, lookup, delete — the fastest associative container
  • Hash set: O(1) membership check, deduplication, tracking seen elements
  • Frequency counting: count occurrences of each element in a single pass
  • Two Sum pattern: store complements to eliminate nested loops
  • Hash map vs tree map: hash map for speed, tree map when you need sorted order
  • Watch out for: hash collisions in adversarial inputs, unordered iteration
💡

Pro Tip

When in doubt, start with a hash map — it solves more LeetCode problems than any other single data structure because O(1) lookup eliminates the need for nested loops.

Stacks and Queues — LIFO, FIFO, and Monotonic Patterns

Stacks and queues are simple data structures with powerful applications in interviews. A stack follows Last-In-First-Out (LIFO) — the most recently added element is the first one removed. A queue follows First-In-First-Out (FIFO) — elements are processed in the order they arrive. These two orderings solve fundamentally different categories of problems.

Stacks are the natural choice for problems involving nested structures, matching pairs, or "most recent" processing. Valid Parentheses (#20) is the textbook example, but stacks go far beyond bracket matching. Monotonic stacks — stacks that maintain elements in sorted order — solve "next greater element" and "largest rectangle" problems in O(n). Daily Temperatures (#739), Next Greater Element (#496), and Largest Rectangle in Histogram (#84) all use monotonic stacks.

Queues power breadth-first search (BFS), which is the standard approach for shortest-path problems in unweighted graphs and level-order tree traversal. When you see "minimum number of steps" or "shortest path," a queue-based BFS is almost always the answer. Problems like Binary Tree Level Order Traversal (#102), Rotting Oranges (#994), and Word Ladder (#127) all use queues.

The deque (double-ended queue) deserves special mention. It supports O(1) operations at both ends, making it perfect for sliding window maximum problems. The Sliding Window Maximum (#239) uses a monotonic deque to track the maximum element as the window slides, achieving O(n) instead of the naive O(n times k) approach.

  • Stack (LIFO): parentheses matching, expression evaluation, DFS, undo operations
  • Monotonic stack: next greater/smaller element, largest rectangle, stock span
  • Queue (FIFO): BFS traversal, shortest path in unweighted graphs, task scheduling
  • Deque: sliding window maximum/minimum, maintaining sorted window in O(1) per operation
  • Best for: ordered processing where insertion/removal order matters
  • Watch out for: using a stack when you need BFS (queue), or vice versa

Trees and Heaps — Hierarchical Data and Priority Access

Trees appear in roughly 35 percent of coding interview questions, making them one of the most important leetcode data structures to master. A binary tree has at most two children per node, and most tree interview problems boil down to recursive DFS traversals — inorder, preorder, or postorder — or level-order BFS.

Binary search trees (BSTs) add an ordering property: left children are smaller, right children are larger. This enables O(log n) search, insert, and delete when the tree is balanced. Validate Binary Search Tree (#98) and Kth Smallest Element in a BST (#230) test whether you understand this property. In practice, balanced BSTs power language-level sorted containers like Java's TreeMap and C++'s std::map.

Tries (prefix trees) are specialized trees for string operations. Each node represents a character, and paths from root to leaf spell out words. They provide O(L) lookup where L is the word length — independent of how many words are stored. Implement Trie (#208) and Word Search II (#212) are classic trie problems. Reach for a trie when you see autocomplete, prefix matching, or dictionary-based searches.

Heaps (priority queues) are the most underused data structure in interviews. A min-heap gives you the smallest element in O(1) and insert/extract in O(log n). A max-heap does the same for the largest. Any time you see "find the K smallest," "find the K largest," "merge K sorted lists," or "median from a stream," a heap is the right tool. Problems like Top K Frequent Elements (#347), Merge K Sorted Lists (#23), and Find Median from Data Stream (#295) are all heap problems.

  • Binary tree: recursive DFS (inorder, preorder, postorder), BFS level-order traversal
  • BST: O(log n) search/insert/delete when balanced, sorted iteration via inorder traversal
  • Trie: O(L) prefix lookup, autocomplete, dictionary word search
  • Min-heap: O(1) minimum access, O(log n) insert/extract — use for K smallest/largest
  • Max-heap: same as min-heap but tracks the largest element
  • Best for: hierarchical relationships, sorted access, priority-based processing
ℹ️

Key Insight

Heaps are the most underused data structure in interviews — candidates who recognize 'find the K smallest/largest' as a heap problem save 10+ minutes of coding time.

Graphs and Union-Find — Modeling Relationships and Connectivity

Many interview problems that do not look like graph problems actually are. Any time you have entities with relationships — cells in a grid, courses with prerequisites, people in a social network — you are dealing with a graph. Recognizing when to model a problem as a graph is a critical skill for data structure selection in interviews.

The two main representations are adjacency lists and adjacency matrices. Adjacency lists use a hash map or array of lists where each node maps to its neighbors. They are space-efficient for sparse graphs (O(V + E)) and are the default choice for interviews. Adjacency matrices use a 2D array where matrix[i][j] indicates an edge between nodes i and j. They are better for dense graphs or when you need O(1) edge lookup, but they use O(V squared) space.

BFS and DFS are your primary graph traversal tools. Use BFS for shortest paths in unweighted graphs and DFS for exhaustive exploration, cycle detection, and topological sorting. Number of Islands (#200) uses DFS/BFS on a grid. Course Schedule (#207) uses topological sort. Clone Graph (#133) tests your ability to traverse and reconstruct a graph simultaneously.

Union-Find (Disjoint Set Union) is a specialized data structure for connected component problems. It supports two operations: find (which component does this element belong to?) and union (merge two components). With path compression and union by rank, both operations run in nearly O(1) amortized time. Use union-find when you need to dynamically track connectivity — problems like Number of Connected Components (#323), Redundant Connection (#684), and Accounts Merge (#721) are natural fits.

  • Adjacency list: O(V + E) space, best for sparse graphs — the default interview choice
  • Adjacency matrix: O(V^2) space, O(1) edge lookup — use for dense graphs
  • BFS on graphs: shortest path (unweighted), level-order exploration
  • DFS on graphs: cycle detection, topological sort, path finding, connected components
  • Union-Find: near O(1) connectivity queries, dynamic component tracking
  • Watch out for: forgetting to track visited nodes (infinite loops), directed vs undirected edges

The Data Structure Decision Framework

After studying all of these data structures for interviews individually, the real skill is knowing which one to reach for when you first read a problem. Here is a practical decision framework you can use under interview pressure. It does not cover every edge case, but it handles the vast majority of problems you will encounter.

Start by asking what operation dominates the problem. If you need O(1) lookup or existence checking, use a hash map or hash set. If you need to maintain elements in sorted order, consider a BST, heap, or sorted array with binary search. If you need to process elements in a specific order (most recent first, or first-come-first-served), use a stack or queue.

Next, look at the problem structure. If it involves hierarchical relationships or recursive substructure, think trees. If it involves pairwise relationships or connectivity, think graphs. If it asks for K smallest or largest elements, or anything involving priority, think heaps. If it involves prefixes or dictionary words, think tries.

Finally, check the constraints. If n is small (under 1000), even an O(n squared) array-based approach may work — do not over-engineer with complex data structures when a simple solution fits within the time limit. Interviewers value clarity over cleverness. A clean array solution that runs in time is better than a buggy heap solution that saves a factor of log n.

Practice applying this framework on YeetCode. Each flashcard presents a problem, and before you flip it, ask yourself: what data structure does this problem need? Over time, the pattern recognition becomes automatic, and choosing the right data structure for any interview question becomes second nature.

  1. 1Need O(1) lookup or existence check? Use a hash map or hash set
  2. 2Need sorted/ordered data? Use a BST, heap, or sorted array with binary search
  3. 3Need LIFO processing (most recent)? Use a stack. Need FIFO (first-come)? Use a queue
  4. 4Hierarchical or recursive structure? Think trees. Pairwise relationships? Think graphs
  5. 5Find K smallest/largest or need priority? Use a heap (priority queue)
  6. 6Prefix matching or dictionary lookups? Use a trie
  7. 7Small input (n < 1000)? A simple array-based approach may be all you need
⚠️

Important

Don't over-engineer your data structure choice — if an array with sorting works within the time constraint, use it. Interviewers value simplicity over cleverness.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now