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:
| Format | What it looks like | What's tested |
|---|---|---|
| Online coding test | Timed problems on a testing platform, auto-graded | Correctness and efficiency |
| Live coding interview | Shared editor or whiteboard with an engineer | Problem-solving, communication, code quality |
| Take-home project | A small project over a few days | Real-world code, structure, tests |
| Pair programming | Work on a task with an engineer | Collaboration and practical skill |
| System design | Design a large system on a whiteboard | Architecture, 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:
- Clarify. Restate the problem, ask about input size, edge cases and whether the input is sorted.
- Work an example by hand.
- Say the brute-force approach and its complexity.
- Improve it. Look for a pattern (hashing, two pointers, sorting) that removes repeated work.
- 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.
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
| Pattern | Use it when | Typical complexity |
|---|---|---|
| Hash map / set | You need fast lookups or counts | O(n) time, O(n) space |
| Two pointers | Sorted arrays, pairs, reversing | O(n) time, O(1) space |
| Sliding window | Best subarray or substring of some kind | O(n) time |
| Stack | Matching brackets, "next greater" problems | O(n) time |
| Binary search | Sorted data, or searching an answer range | O(log n) per search |
| Tree traversal (DFS/BFS) | Hierarchies, paths, levels | O(n) time |
| Graph search | Networks, grids, dependencies | O(V + E) time |
| Heap | Top-k, merging sorted lists, scheduling | O(n log k) time |
| Dynamic programming | Overlapping subproblems, counting ways, best value | Varies; 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).
Coding interview examples: stacks and binary search
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
| Step | What to cover |
|---|---|
| 1. Requirements | Core features, users, and what's out of scope |
| 2. Scale | Rough reads and writes per second, data size, growth |
| 3. API | The main endpoints or operations |
| 4. Data model | What you store and in what kind of database |
| 5. High-level design | Clients, load balancers, services, databases, caches, queues |
| 6. Deep dive | One or two components in detail, such as caching or sharding |
| 7. Trade-offs | Consistency 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:
| Week | Focus |
|---|---|
| 1 | Arrays, hashing, two pointers, sliding window |
| 2 | Stacks, binary search, linked lists |
| 3 | Trees, graphs, heaps |
| 4 | Dynamic 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.
