This OOP principle bundles data and methods together in a class and restricts direct access using modifiers like private and public.
What is Encapsulation?
This data structure follows the “Last In, First Out” principle.
What is a Stack?
What is a cycle in a graph?
What is a path that returns to the starting node without reusing edges?
Which traversal visits nodes in this order: root -> left -> right?
What is preorder traversal?
What is the main idea of dynamic programming?
What is breaking problems into smaller subproblems and reusing results?
In system design, why might you trade space complexity for time complexity?
What is
to speed up operations using extra memory (like caching or hashmaps)?
What stack pattern is used to solve “next greater element” problems?
What is a monotonic stack?
What is the key difference between a tree and a general graph?
What is a tree has no cycles (is acyclic)?
What property makes recursion a natural fit for tree problems?
What is trees are made of smaller subtrees (recursive structure)?
What is the difference between memoization and tabulation?
What is
memoization = top-down (recursion), tabulation = bottom-up (iteration)?
What is the difference between method overloading and method overriding?
What is
overloading = same method name, different parameters;
overriding = redefining a parent class method in a subclass?
Why is a heap preferred over sorting every time when repeatedly retrieving the smallest or largest element?
What is because heap operations are O(log n), faster than repeatedly sorting O(n log n)?
What is the time complexity of BFS or DFS on a graph with V vertices and E edges?
What is O(V + E)?
What is a trie primarily used for?
What is storing and searching strings efficiently (by prefix)?
What does dp[i] typically represent in DP problems?
What is the solution to a subproblem up to index i?
You need to design a data structure that supports insert, remove, and getRandom in average O(1) time.
What TWO data structures should you combine to achieve this?
What are an Array/List + a HashMap?
In backtracking, why do we “undo” a choice before trying the next option?
What is to return to a previous state and explore other possibilities?
In Kahn’s algorithm, what does it mean if not all nodes are processed?
What is a cycle exists in the graph?
In level order traversal:
Why do we use len(queue) here?
What is to process one level at a time?
In Edit Distance, what does dp[i][j] represent?
What is the minimum operations to convert first i chars to first j chars?
You are designing Twitter and use a heap to keep track of the most recent tweets.
If you process n tweets and keep only the top k most recent ones in the heap, what is the time complexity?
What is O(n log k)?
In the Daily Temperatures problem, we use a stack to store indices of days that have not found a warmer day yet.
What does i - j represent?
What is the number of days day j has to wait until a warmer temperature?
You are using BFS to find the shortest path in an unweighted graph:
queue = deque([start])
visited = set()
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if neighbor not in visited:
queue.append(neighbor)
What is the issue with this implementation?
What is nodes are not marked as visited when added, so they may be added to the queue multiple times and potentially cause infinite loops?
This DFS is meant to traverse a tree:
def dfs(node):
if not node:
return
dfs(node.left)
dfs(node.right)
What is the bug?
What is it never processes the node (missing visit/output step)?
What is the bug here?
dp = [0] * (n+1)
for i in range(n+1):
dp[i] = dp[i-1] + dp[i-2]
What is missing base cases (dp[0], dp[1])?