Interview questions · Book 12
Data Structures interview questions
144 questions from 36 pages, each with a short answer. Say your answer first, then open the question to check it.
p. 1 · 4 questions
Adjacency List
1
What is an adjacency list?
An adjacency list is a way of storing a graph in which each node keeps a list of the nodes it connects to, using memory in proportion to its nodes and edges.
2
What is the difference between an adjacency list and an adjacency matrix?
An adjacency list stores only the edges that exist, using O(V + E) memory, and is best for sparse graphs. An adjacency matrix stores a cell for every pair of nodes, using O(V^2) memory, but checks whether any edge exists in O(1), which suits small or dense graphs.
3
What is the space complexity of an adjacency list?
It is O(V + E): one entry per vertex plus one entry per edge in a directed graph, or two per edge in an undirected graph, since each edge is recorded at both ends.
4
How do I represent an adjacency list in code?
The simplest form is a map from each node to an array of neighbors, such as a Python
dictof lists or a JavaScriptMapof arrays. When nodes are numbered from 0 to V - 1, an array of arrays works too and is slightly faster.
p. 2 · 4 questions
B-Tree
1
What is a B-tree?
A B-tree is a self-balancing search tree whose nodes hold many sorted keys and children, which keeps it shallow so lookups need very few disk or page reads.
2
What is the difference between a B-tree and a binary search tree?
A binary search tree node holds one key and has at most two children, so a large tree is many levels deep. A B-tree node holds many keys and has many children, which keeps the tree only a few levels deep and minimizes slow reads from storage.
3
What is the difference between a B-tree and a B+ tree?
In a B-tree, keys and their values can live in any node. In a B+ tree, internal nodes only guide the search and all values live in the leaves, which are linked in sorted order, making range scans faster; most database indexes are B+ trees.
4
What does the B in B-tree stand for?
Nobody knows for sure. Rudolf Bayer and Edward McCreight, who invented it around 1970, never defined it, and popular guesses include balanced, broad, Bayer, and Boeing, where they worked at the time.
p. 3 · 4 questions
Backtracking
1
What is backtracking?
Backtracking is a search technique that builds a solution one choice at a time and undoes the latest choice when it hits a dead end, then tries another option.
2
What is the difference between backtracking and depth-first search?
Depth-first search traverses the nodes of an existing graph or tree. Backtracking applies the same depth-first order to a tree of choices that it generates as it goes, and it abandons a branch as soon as the partial solution breaks a rule.
3
What is the time complexity of backtracking?
In the worst case it is exponential, or even factorial, because it may explore every combination of choices. Pruning cuts the work dramatically in practice, but the worst-case bound usually stays exponential.
4
What problems are solved with backtracking?
Typical examples are sudoku and other constraint puzzles, the N-queens problem, generating all permutations or combinations, finding paths through a maze, and scheduling problems with many rules.
p. 4 · 4 questions
Balanced Tree
1
What is a balanced tree?
A balanced tree is a tree that keeps its height close to log n by rebalancing after changes, so search, insert, and delete stay O(log n) even in the worst case.
2
What makes a tree balanced?
A tree is balanced when its height stays proportional to log n, which usually means that for every node the left and right subtrees have similar heights. Each kind of balanced tree defines this precisely; an AVL tree, for example, allows a height difference of at most one.
3
What is the difference between an AVL tree and a red-black tree?
Both are self-balancing binary search trees with O(log n) operations. AVL trees are balanced more strictly, so lookups are slightly faster, while red-black trees allow a little more imbalance and need fewer rotations when data changes, which suits workloads with many inserts and deletes.
4
Why use a balanced tree instead of a hash table?
A hash table is faster for exact-key lookups, O(1) on average, but it keeps no order. A balanced tree keeps keys sorted, so it can efficiently answer range queries, find the next larger key, and list items in order.
p. 5 · 4 questions
Binary Search
1
What is binary search?
Binary search is an algorithm that finds a value in a sorted list by repeatedly halving the search range, taking O(log n) time instead of checking every item.
2
Why must the list be sorted for binary search?
Binary search decides which half to discard by comparing the target with the middle item. That decision is only correct if everything to the left is smaller and everything to the right is larger, which is true only for sorted data.
3
What is the time complexity of binary search?
Binary search runs in O(log n) time in the worst and average case, and O(1) in the best case, when the middle item is the target. The loop-based version uses O(1) extra memory, while a recursive version uses O(log n) for the call stack.
4
Is it worth sorting data just to run a binary search?
Sorting costs O(n log n), so it only pays off if you search the same data many times. For a single lookup, a linear search in O(n) is faster, and for many exact-key lookups a hash table is often better still.
p. 6 · 4 questions
Binary Search Tree
1
What is a binary search tree?
A binary search tree is a binary tree in which each node's left subtree holds smaller values and its right subtree larger ones, enabling fast ordered lookups.
2
What is the time complexity of a binary search tree?
Search, insert, and delete take O(h) time, where h is the tree's height. That is O(log n) when the tree is balanced, but it degrades to O(n) when the tree becomes a long chain, for example after inserting values in sorted order.
3
What is the difference between a binary tree and a binary search tree?
A binary tree only limits each node to at most two children. A binary search tree adds the rule that left descendants are smaller and right descendants are larger, which is what makes fast searching possible.
4
What is a self-balancing binary search tree?
It is a BST that automatically restructures itself with rotations after inserts and deletes so that its height stays proportional to log n. AVL trees and red-black trees are the best-known examples, and both guarantee O(log n) search, insert, and delete.
p. 7 · 4 questions
Bloom Filter
1
What is a Bloom filter?
A Bloom filter is a compact probabilistic data structure that tells you an item is definitely not in a set or probably is, while using very little memory.
2
Can a Bloom filter give false negatives?
No. If an item was added, all of its bits are set to 1, so the filter always reports it as possibly present. Only false positives are possible, when other items happen to have set all the same bits.
3
How big should a Bloom filter be?
It depends on how many items you expect and what false positive rate you can accept. As a rule of thumb, about 10 bits per item with 7 hash functions gives a rate just under 1 percent, and each extra 5 bits per item cuts it roughly tenfold.
4
What is the difference between a Bloom filter and a hash set?
A hash set stores every item and answers membership exactly, but it needs memory for all the items. A Bloom filter stores only bits, so it is far smaller, but it can return false positives and can't list or, in its basic form, remove items.
p. 8 · 4 questions
Breadth-First Search
1
What is breadth-first search?
Breadth-first search is a graph traversal algorithm that visits nodes in order of their distance from the start, exploring all neighbors before going deeper.
2
What is the difference between BFS and DFS?
BFS explores all nodes at the current distance before moving farther away, using a queue. DFS follows one path as deep as possible before backtracking, using a stack or recursion. Both run in O(V + E) time, but only BFS finds shortest paths by edge count.
3
Does BFS always find the shortest path?
It finds the path with the fewest edges, which is the shortest path when all edges have the same cost. When edges have different weights, such as road distances, use Dijkstra's algorithm instead.
4
What is the time complexity of breadth-first search?
With an adjacency list, BFS runs in O(V + E) time, because it processes each vertex and each edge a constant number of times. With an adjacency matrix, it takes O(V^2), since finding each node's neighbors means scanning a whole row.