100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Common Data Structures Interview Questions

A curated set of frequently asked data structures interview questions with technically accurate, concise answers.

Interview PrepIntermediate14 min readJul 8, 2026
Analogies

Overview

Data structures interviews rarely test whether you memorized a definition. They test whether you understand the tradeoffs behind a choice: why an array beats a linked list for random access, why a hash map gives average O(1) lookup but can degrade, and why a balanced tree guarantees what a plain BST cannot. This topic collects the questions that come up again and again across coding interviews, with answers that focus on the 'why', not just the 'what'.

🏏

Cricket analogy: Like a selector explaining why they picked Bumrah over Siraj — not reciting stats but justifying the tradeoff between pace and control, since interviews reward reasoning about why, not just naming players.

Frequently Asked Questions

Q: Array vs linked list — what are the real tradeoffs?

Arrays store elements in contiguous memory, so indexing is O(1) and iteration is cache-friendly, but inserting or deleting in the middle requires shifting elements, an O(n) operation. Linked lists give O(1) insertion/deletion once you have a reference to the node, but indexing requires O(n) traversal and each node has pointer overhead plus poor cache locality. Choose arrays when you read far more than you insert/delete in the middle; choose linked lists when you need frequent mid-sequence insertion/deletion and don't need random access, e.g. implementing an LRU cache's node list alongside a hash map.

🏏

Cricket analogy: Like a stadium's numbered seats — you reach seat 42 instantly, but squeezing a latecomer into row 10 means shifting everyone down; a chain of fans holding hands lets you insert someone anywhere instantly, but finding "the 42nd fan" means counting from the start.

Q: When would you use a stack instead of a queue?

Use a stack (LIFO) when the most recently added item must be processed first: function call stacks, undo/redo, matching brackets, DFS traversal, and expression evaluation. Use a queue (FIFO) when items must be processed in the order they arrived: task scheduling, BFS traversal, print job queues, and buffering streamed data. The core signal is whether order of arrival should match order of processing (queue) or be reversed (stack).

🏏

Cricket analogy: Use a stack when reviewing the most recent over first, like a DRS review of the last ball bowled; use a queue when batsmen come in strictly in the order set on the team sheet, first in the lineup goes first.

Q: BST vs hash table for lookups — which is faster and when would you still pick a BST?

A hash table gives average O(1) lookup, insert, and delete, which is faster than a balanced BST's O(log n) in the average case. However, a BST keeps keys in sorted order, enabling O(log n) range queries, in-order traversal for sorted output, floor/ceiling/predecessor/successor queries, and stable performance without needing a good hash function. Pick a hash table for pure existence/lookup checks; pick a BST (or a balanced variant like an AVL or red-black tree) when you need ordered operations.

🏏

Cricket analogy: A hash table is like knowing a player's stats instantly by name lookup, but a sorted batting-average leaderboard (a BST) lets you instantly find who's just above or below a given rank, which a plain name lookup can't do.

Q: What is the time complexity of common operations across arrays, linked lists, and hash tables?

Array: access O(1), search O(n), insert/delete at end O(1) amortized, insert/delete at arbitrary index O(n). Singly linked list: access O(n), search O(n), insert/delete at head O(1), insert/delete at tail O(n) without a tail pointer (O(1) with one), insert/delete at known node O(1). Hash table: access by key O(1) average / O(n) worst case, search O(1) average, insert O(1) average, delete O(1) average. Interviewers expect you to state both the average and worst case, not just the average.

🏏

Cricket analogy: An interviewer expects you to know a bowler's economy rate both in a flat T20 chase (worst case) and across a whole tournament (average) — stating only one paints an incomplete picture, just like stating only average-case complexity for a hash table.

Q: How does a hash map achieve average O(1) lookup?

A hash function maps a key to an array index (a bucket). If the hash function distributes keys uniformly and the table resizes to keep the load factor (elements / buckets) low, each bucket holds very few elements on average, so lookup, insert, and delete only touch a small constant number of entries — O(1) on average. Collisions are handled with chaining (each bucket holds a list) or open addressing (probing for the next free slot). Worst case is O(n) if all keys collide into one bucket, which is why a good, well-distributed hash function and periodic resizing matter.

🏏

Cricket analogy: A hash function is like assigning each player to a dugout seat by jersey number mod the number of seats; if too many players share one seat (high load factor) the coach resizes the dugout, and if the assignment is bad, one seat gets overcrowded — a worst-case pileup.

Q: How would you detect a cycle in a linked list?

Use Floyd's cycle detection (the 'tortoise and hare' algorithm): one pointer moves one step at a time, another moves two steps. If there is a cycle, the fast pointer eventually laps the slow pointer and they meet; if the fast pointer reaches null, there is no cycle. This runs in O(n) time and O(1) extra space, compared to O(n) space if you tracked visited nodes in a hash set.

🏏

Cricket analogy: Like two fielders jogging around the boundary at different speeds to check if it's a closed loop — if the faster one laps the slower one, it's a loop; this needs no extra notepad, unlike marking every spot visited on a map.

Q: When would you choose a heap over sorting the entire dataset?

A heap is ideal when you only need the minimum/maximum (or top-k) repeatedly without needing the full data sorted. Building a heap is O(n), and each extract-min/max is O(log n) — finding the k smallest of n elements is O(n + k log n) with a heap versus O(n log n) to fully sort. Heaps also support efficient priority queue operations (decrease-key, insert) used in algorithms like Dijkstra's shortest path and in scheduling.

🏏

Cricket analogy: Like a heap-based leaderboard that lets you instantly pull the current top run-scorer without sorting the whole tournament table, useful when you only ever need the current best, not the full ranked list.

Q: What's the difference between BFS and DFS, and how do you choose?

BFS explores level by level using a queue and finds the shortest path in an unweighted graph; it uses O(V) extra space in the worst case for the queue and visited set. DFS explores as deep as possible before backtracking, using a stack (explicit or via recursion); it uses less memory for wide graphs but can miss the shortest path. Choose BFS for shortest-path/level-order problems, DFS for exhaustive search, topological sort, cycle detection, and connectivity problems where path length doesn't matter.

🏏

Cricket analogy: BFS is like scouting all bowlers of one pace bracket before moving to the next bracket, guaranteeing you find the fastest bowler overall first; DFS is like following one bowler's entire spell in detail before checking the next bowler, useful for deep analysis but not for finding "fastest first."

Q: Why is resizing a dynamic array (e.g. Python list) still considered O(1) amortized for append?

When a dynamic array is full, it allocates a new block (typically double the size) and copies all existing elements over — an O(n) operation. But because this only happens occasionally (geometric growth), the total cost of n appends is O(n), so each append is O(1) on average, i.e. amortized. A single append can still spike to O(n) in the worst case, which matters in latency-sensitive interview follow-ups.

🏏

Cricket analogy: Like a stadium that occasionally doubles its seating capacity by building a whole new stand and moving everyone over, a costly one-time event, but because it happens rarely, average seat allocation per fan stays cheap even though one unlucky match day sees the full move.

Q: How would you design a data structure that supports insert, delete, and getRandom all in O(1)?

Combine an array with a hash map. The array stores the values (enabling O(1) random access via a random index for getRandom). The hash map stores value → index in the array. To delete in O(1), swap the element to delete with the last element in the array, update the hash map for the swapped element, then pop the last element — avoiding the O(n) shift a plain array delete would need.

🏏

Cricket analogy: Like keeping a numbered list of playing-XI names for random selection (instant pick) alongside a name-to-slot lookup; to drop a player, swap them with the last name on the list, update the lookup, then trim the list, avoiding shifting the whole XI.

Quick Reference

  • Array: O(1) access, O(n) middle insert/delete
  • Linked list: O(1) insert/delete at known node, O(n) access
  • Hash table: O(1) average lookup/insert/delete, O(n) worst case
  • BST (balanced): O(log n) for all core operations, keeps sorted order
  • Heap: O(log n) insert/extract-min/max, O(1) peek
  • Stack = LIFO, used for DFS, undo, expression parsing
  • Queue = FIFO, used for BFS, scheduling, buffering
  • Floyd's cycle detection: O(n) time, O(1) space
  • Dynamic array append: O(1) amortized due to geometric resizing
  • Array + hash map combo gives O(1) insert/delete/getRandom

Key Takeaways

  • Always state both average and worst-case complexity, not just one
  • Interviewers reward reasoning about tradeoffs over memorized definitions
  • Know the classic combo patterns: array+hashmap, heap+hashmap (top-k, LFU/LRU)
  • Be ready to justify BFS vs DFS and stack vs queue with a concrete example
  • Practice explaining amortized analysis clearly — it's a common follow-up

Practice what you learned

Was this page helpful?

Topics covered

#Python#DataStructuresStudyNotes#DataStructures#CommonDataStructuresInterviewQuestions#Common#Data#Structures#Interview#StudyNotes#SkillVeris