InterviewSkill

DSA Interview Questions

Data structures and algorithms for coding rounds in software, data, ML, and AI interviews.

10 questions
DSA

How do you solve two sum efficiently?easy

Type
problem-solving
Topic
two-sum
Frequency
common
Roles
software-engineer, ml-engineer, data-engineer
Tags
array, hash-map
Answer

Use a hash map to store values you have seen and check whether the complement exists.

Explanation

For each number x, compute target - x. If that complement is already in the map, you found the pair. This reduces the brute-force O(n^2) approach to O(n) time with O(n) extra space.

Hint: reason from constraints before calculating.
Follow-upHow would the solution change if the input array is sorted?

When do you use the sliding window pattern?medium

Type
conceptual
Topic
sliding-window
Frequency
common
Roles
software-engineer, ml-engineer
Tags
array, string, sliding-window
Answer

Use sliding window for contiguous subarray or substring problems where you can update the answer as the window moves.

Explanation

Instead of recomputing each range from scratch, maintain state such as sum, counts, or distinct characters while expanding and shrinking the window. This often turns O(n^2) work into O(n).

Follow-upWhat is the difference between fixed-size and variable-size sliding windows?

What is the two pointers technique?easy

Type
conceptual
Topic
two-pointers
Frequency
common
Roles
software-engineer, data-engineer
Tags
array, two-pointers
Answer

Two pointers use two indexes that move through a sequence to avoid nested loops.

Explanation

It is common for sorted arrays, palindrome checks, merging, partitioning, and pair-sum problems. The key is knowing how pointer movement changes the search space.

Follow-upWhy does sorting often make two pointers possible?

How do you validate balanced parentheses?easy

Type
problem-solving
Topic
stack-valid-parentheses
Frequency
common
Roles
software-engineer
Tags
stack, string
Answer

Use a stack: push opening brackets and pop when a matching closing bracket appears.

Explanation

The stack stores the most recent unmatched opening bracket. If a closing bracket does not match the top of the stack, the string is invalid. At the end, the stack must be empty.

Hint: reason from constraints before calculating.
Follow-upWhy is a stack a natural fit for nested structures?

When can you apply binary search?medium

Type
conceptual
Topic
binary-search
Frequency
common
Roles
software-engineer, ml-engineer
Tags
binary-search, array
Answer

Use binary search when the search space is sorted or has a monotonic true/false condition.

Explanation

Binary search repeatedly discards half the search space. It works not only on arrays but also on answer ranges, such as finding the minimum capacity that satisfies a condition.

Follow-upWhat causes infinite loops in binary search?

How do you reverse a linked list?medium

Type
problem-solving
Topic
linked-list-reversal
Frequency
common
Roles
software-engineer
Tags
linked-list, pointers
Answer

Iterate through the list and reverse each node's next pointer using previous, current, and next references.

Explanation

At each step, save current.next, point current.next to previous, then move previous and current forward. The final previous pointer becomes the new head.

Hint: reason from constraints before calculating.
Follow-upHow would you reverse a linked list recursively?

What is the difference between BFS and DFS?medium

Type
conceptual
Topic
bfs-dfs
Frequency
common
Roles
software-engineer, ml-engineer
Tags
graph, bfs, dfs
Answer

BFS explores level by level using a queue; DFS explores deeply along a path using recursion or a stack.

Explanation

BFS is useful for shortest path in unweighted graphs and level-order traversal. DFS is useful for connectivity, cycle detection, backtracking, and exploring components.

Follow-upWhich one would you use to find the shortest path in an unweighted graph?

What are inorder, preorder, and postorder tree traversals?medium

Type
conceptual
Topic
tree-traversal
Frequency
common
Roles
software-engineer
Tags
tree, traversal
Answer

They are DFS traversal orders: preorder visits node-left-right, inorder visits left-node-right, and postorder visits left-right-node.

Explanation

Inorder is especially useful for binary search trees because it returns values in sorted order. Preorder can serialize tree structure, and postorder is useful when children must be processed before the parent.

Follow-upWhy does inorder traversal sort a binary search tree?

How do you recognize a dynamic programming problem?hard

Type
conceptual
Topic
dynamic-programming
Frequency
common
Roles
software-engineer, ml-engineer
Tags
dynamic-programming, recursion
Answer

Look for overlapping subproblems and an optimal answer that can be built from smaller answers.

Explanation

DP is useful when recursion repeats the same work. Define the state, recurrence, base cases, and fill order. Interviews often start with a brute-force recursion before adding memoization.

Follow-upWhat is the difference between memoization and tabulation?

How do you explain time and space complexity in an interview?easy

Type
conceptual
Topic
time-space-complexity
Frequency
common
Roles
software-engineer, data-engineer, ml-engineer
Tags
complexity, big-o
Answer

Describe how runtime and memory grow as input size increases, usually using Big O notation.

Explanation

Focus on dominant terms and ignore constants. A clear answer explains loops, nested loops, recursion depth, extra data structures, and tradeoffs between time and memory.

Follow-upHow do you analyze recursive complexity?