Data structures & algorithms interview questions

13 questions across 4 sections, in the order you would learn them. Every answer here is one that works — but an answer you have read is not an answer you can give, so use Test yourself and say it out loud first.

Working out whether you need this

Read the section that teaches this

If this comes up in an interview

Do AI engineering roles still ask algorithm questions?

It depends on the company rather than the role title. Large companies and infrastructure-flavoured teams generally still do. Product-focused AI companies where agents and retrieval are the product frequently do not, running practical build rounds instead.

How do you decide how much algorithm preparation to do?

From the target list. Check what the specific companies run, through candidate reports or by asking the recruiter directly. Forty hours is either essential or wasted depending entirely on that answer.

The high-frequency patterns

Read the section that teaches this

If this comes up in an interview

When do you reach for a hash map?

Whenever the question involves lookup, counting, grouping or duplicate detection. It turns a scan into a constant-time check, which is usually what converts a quadratic solution to a linear one.

How do you recognise a sliding window problem?

It asks about a contiguous subarray or substring with some property, usually the longest or shortest satisfying a condition. The shape is always expand with the loop, contract when the property breaks.

What is binary search on the answer?

When the answer is a value in a range and checking whether a candidate works is cheap, you binary search the range of possible answers rather than the input. The input does not need to be sorted; the answer space is.

What is a monotonic stack for?

Next-greater and next-smaller element questions in linear time. You keep the stack ordered, and when a new element breaks the order you have found the answer for everything it displaces.

Trees, graphs and the rest

Read the section that teaches this

If this comes up in an interview

When would you use BFS rather than DFS?

When the question involves levels, or the shortest path in an unweighted graph, since BFS reaches nodes in order of distance. DFS suits paths, subtree properties and anything naturally recursive.

How do you find the top k elements efficiently?

Keep a heap of size k rather than sorting everything. That is n log k rather than n log n, and it works on a stream where you cannot hold all the data at once.

What does topological sort solve?

Ordering with dependencies: course prerequisites, build order, task scheduling. It also detects cycles, since a cycle means some nodes never reach indegree zero and the output is shorter than the input.

What is the difference between recursion and backtracking?

Backtracking is recursion that undoes its choice before trying the next one. The undo step, usually a pop, is what makes it backtracking, and forgetting it is the most common bug in these problems.

Solving one under interview conditions

Read the section that teaches this

If this comes up in an interview

How do you approach a problem you have not seen before?

Restate it, clarify input size and edge cases, give the brute force with its complexity, identify the wasteful part, propose the improvement and its complexity, then write it while narrating. The brute force is not a concession, it is a working baseline and thinking time.

What do you do if you cannot solve it?

Say where I am and what I have eliminated, and propose the next thing to try. A working brute force with clear reasoning scores better than silence, and taking a hint well is a positive signal rather than a failure.

How important is finishing?

Less than candidates assume. A clear approach with correct complexity analysis, communicated well, usually scores better than a silently produced solution the interviewer could not follow.

These are the questions for one topic. The whole question bank covers every topic on the site.