Skip to main content

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

Full page
  1. 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. 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. 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. 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 dict of lists or a JavaScript Map of 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

Full page
  1. 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. 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. 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. 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

Full page
  1. 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. 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. 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. 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

Full page
  1. 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. 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. 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. 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. 6 · 4 questions

Binary Search Tree

Full page
  1. 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. 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. 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. 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

Full page
  1. 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. 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. 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. 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.

More

Settings