coding interview

Coding Interview Questions: 20 Examples, DSA + System Design

·7 min read
Coding Interview Questions: 20 Examples, DSA + System Design

The short answer

A coding interview tests whether you can turn a problem into working, efficient code while explaining your thinking. Most questions come from a small set of patterns: arrays and hashing, two pointers, sliding window, stacks, binary search, trees, graphs and dynamic programming. Senior roles add system design. Learn the patterns, practise out loud, and always state the time and space complexity.

  • Clarify the problem and examples before writing any code.
  • Say a brute-force solution first, then improve it.
  • Test your code with edge cases: empty input, one item, duplicates, very large input.
  • In system design, start with requirements and scale, then sketch the components.
For job seekers

Cohesyve, which publishes this guide, runs practice simulations of the real work in engineering roles, with a scored report on where you lost marks. 3-day free trial, cancel anytime.

A coding interview asks you to solve a programming problem, usually live with an interviewer or in a timed online test, and to explain your reasoning as you go. The problems look endless, but most coding interview questions are built from a dozen or so patterns. Learn the patterns and you'll recognise new questions quickly.

This guide gives 20 coding interview examples grouped by pattern, each with the approach and its complexity, then covers system design interview questions and a practice plan. We describe solutions in words so you can write them in any language.

What happens in a coding interview?

Formats differ by employer, but most follow one of these:

FormatWhat it looks likeWhat's tested
Online coding testTimed problems on a testing platform, auto-gradedCorrectness and efficiency
Live coding interviewShared editor or whiteboard with an engineerProblem-solving, communication, code quality
Take-home projectA small project over a few daysReal-world code, structure, tests
Pair programmingWork on a task with an engineerCollaboration and practical skill
System designDesign a large system on a whiteboardArchitecture, trade-offs, scale

If you've been sent a test on a named platform, see our guides to HackerRank, Codility, CodeSignal, HireVue coding and the Amazon online assessment.

How do you answer a coding interview question?

Use the same five steps every time:

  1. Clarify. Restate the problem, ask about input size, edge cases and whether the input is sorted.
  2. Work an example by hand.
  3. Say the brute-force approach and its complexity.
  4. Improve it. Look for a pattern (hashing, two pointers, sorting) that removes repeated work.
  5. Code, then test with normal, edge and tricky cases, and state the final time and space complexity.

Talking through each step matters. Interviewers can only credit reasoning they can hear.

For job seekers

Beyond LeetCode: practise the engineering work itself

Try a simulation of real developer tasks for your target role and get a scored report on where you lost marks, so you know what to fix before the interview. 3-day free trial, cancel anytime.

DSA interview questions by pattern

PatternUse it whenTypical complexity
Hash map / setYou need fast lookups or countsO(n) time, O(n) space
Two pointersSorted arrays, pairs, reversingO(n) time, O(1) space
Sliding windowBest subarray or substring of some kindO(n) time
StackMatching brackets, "next greater" problemsO(n) time
Binary searchSorted data, or searching an answer rangeO(log n) per search
Tree traversal (DFS/BFS)Hierarchies, paths, levelsO(n) time
Graph searchNetworks, grids, dependenciesO(V + E) time
HeapTop-k, merging sorted lists, schedulingO(n log k) time
Dynamic programmingOverlapping subproblems, counting ways, best valueVaries; often O(n) or O(n²)

Coding interview examples: arrays and hashing

1. Two sum. Given a list of numbers and a target, return the positions of two numbers that add up to the target.

Approach: walk the list once, storing each number's position in a hash map. For each number, check whether target minus that number is already in the map. O(n) time, O(n) space.

2. Contains duplicate. Return true if any value appears twice.

Approach: add each value to a set; if it's already there, return true. O(n) time.

3. Group anagrams. Group words that are anagrams of each other.

Approach: use the sorted letters of each word (or a count of each letter) as a hash-map key. O(n × k log k) time with sorting, where k is the word length.

4. Product of array except self. For each position, return the product of all other numbers, without division.

Approach: one pass builds products of everything to the left, a second pass multiplies in everything to the right. O(n) time.

Coding interview examples: two pointers and sliding window

5. Valid palindrome. Is a string the same forwards and backwards, ignoring punctuation and case?

Approach: one pointer at each end, skip non-letters, compare and move inwards. O(n) time, O(1) space.

6. Container with most water. Given heights, find two lines that hold the most water.

Approach: pointers at both ends; always move the shorter one inwards, since moving the taller can't help. O(n).

7. Longest substring without repeating characters.

Approach: a sliding window with a set or map of the last position of each character; move the left edge past any repeat. O(n).

8. Valid parentheses. Are brackets correctly matched and nested?

Approach: push opening brackets on a stack; each closing bracket must match the top. The stack must be empty at the end. O(n).

9. Binary search. Find a target in a sorted array.

Approach: compare with the middle item and discard half each time. O(log n). Practise getting the boundaries right; off-by-one errors are the classic bug.

10. Search in a rotated sorted array.

Approach: binary search, but at each step work out which half is properly sorted and whether the target lies in it. O(log n).

Coding interview examples: linked lists and trees

11. Reverse a linked list.

Approach: walk the list, pointing each node back at the previous one. Keep three references: previous, current, next. O(n) time, O(1) space.

12. Detect a cycle in a linked list.

Approach: a slow pointer moves one step, a fast pointer two. If they meet, there's a cycle. O(n) time, O(1) space.

13. Maximum depth of a binary tree.

Approach: recursion: depth is one plus the larger of the left and right depths. O(n).

14. Validate a binary search tree.

Approach: recurse with a valid range (minimum and maximum) for each node, narrowing it as you go down. Checking only a node against its children isn't enough.

15. Level-order traversal.

Approach: breadth-first search with a queue, processing one level at a time. O(n).

Coding interview examples: graphs, heaps and dynamic programming

16. Number of islands. Count groups of connected land cells in a grid.

Approach: scan the grid; on each unvisited land cell, start a DFS or BFS that marks the whole island. O(rows × columns).

17. Course schedule. Can you finish all courses given prerequisites?

Approach: treat courses as a directed graph and check for a cycle, using topological sort. O(V + E).

18. Top k frequent elements.

Approach: count with a hash map, then keep a min-heap of size k. O(n log k).

19. Climbing stairs. How many ways to climb n stairs taking 1 or 2 steps?

Approach: the ways to reach step n are the ways to reach n−1 plus n−2. Build up from the bottom. O(n) time, O(1) space.

20. Coin change. The fewest coins that make an amount.

Approach: for each amount from 1 upwards, the best answer is one plus the best answer for (amount minus each coin). O(amount × number of coins).

What are common system design interview questions?

System design interviews are usually for mid-level and senior engineers. You're asked to design a large system and discuss trade-offs.

Common prompts include:

  • Design a URL shortener.
  • Design a chat or messaging service.
  • Design a news feed.
  • Design a rate limiter.
  • Design a file storage and sharing service.
  • Design a notification system.

How to structure a system design answer

StepWhat to cover
1. RequirementsCore features, users, and what's out of scope
2. ScaleRough reads and writes per second, data size, growth
3. APIThe main endpoints or operations
4. Data modelWhat you store and in what kind of database
5. High-level designClients, load balancers, services, databases, caches, queues
6. Deep diveOne or two components in detail, such as caching or sharding
7. Trade-offsConsistency versus availability, cost, failure handling

Say your assumptions out loud and check them with the interviewer. There's rarely one right design; the interviewer wants to see sound reasoning about trade-offs.

How should you prepare for a coding interview?

A four-week example plan:

WeekFocus
1Arrays, hashing, two pointers, sliding window
2Stacks, binary search, linked lists
3Trees, graphs, heaps
4Dynamic programming, mixed timed practice, one mock interview

Tips:

  • Pick one language and know its standard library well: lists, maps, sets, sorting, heaps.
  • Solve problems out loud, as if an interviewer were listening.
  • Time yourself on mixed sets once you know the patterns.
  • Review failed problems a few days later and solve them again from scratch.
  • Do at least one mock interview with another engineer.
  • Prepare behavioural answers too. Most engineering processes include them; our STAR method guide helps.

Role-specific preparation helps as well. See our preparation guides for backend, frontend and full-stack developers, and our software engineer interview questions.

Practising the real work

Coding interview questions test patterns. Many teams also want to see how you handle realistic tasks: reading unfamiliar code, fixing a bug, building a small feature. Cohesyve, which publishes this guide, runs practice simulations of the real work in roles such as backend developer and frontend developer, with a scored report on where you lost marks. If you're coming from a bootcamp, our guide to whether coding bootcamps are worth it covers what to add to your portfolio.

FAQs

What are the most common coding interview questions?

Questions built on hashing, two pointers, sliding windows, stacks, binary search, trees, graphs and dynamic programming. The 20 examples above cover each pattern.

Which language should I use in a coding interview?

The one you know best, unless the role requires a specific language. Python is popular because it's concise, but fluency matters more than the choice.

How many problems should I practise?

Aim for understanding over volume. Being able to solve a few problems from each pattern without hints is worth more than skimming hundreds of solutions.

What if I get stuck in a coding interview?

Say what you're thinking, go back to a small example, and propose a brute-force solution. Interviewers often give hints when they can see your reasoning.

Do junior developers get system design questions?

Less often. Junior interviews focus on coding and fundamentals; system design usually starts at mid-level roles. Some employers include a lighter design question for juniors.

What is a DSA interview?

DSA stands for data structures and algorithms. A DSA interview is a coding interview focused on those topics, such as arrays, trees, graphs, sorting and dynamic programming.

For job seekers

Beyond LeetCode: practise the engineering work itself

Try a simulation of real developer tasks for your target role and get a scored report on where you lost marks, so you know what to fix before the interview. 3-day free trial, cancel anytime.

3 days

free trial

Unlimited

tests on Pro

Full

report on every test

Any

real job posting

3-day free trial · Then Pro $20/mo · Cancel anytime

For hiring teams

Interviewing candidates rather than being interviewed? Cohesyve finds and contacts candidates from your job description, tests them on a simulation of the real work and ranks them.

Test engineers on real coding work →

Practise before it counts

3-day free trial

Start practising