I've been working with Java for 13 years and have interviewed hundreds of developers.
One thing I've noticed:
Many developers know DSA patterns by name. Far fewer can recognize which pattern fits a problem they've never seen before.
And that's often what makes coding interviews difficult.
So let's test that skill.
I'll give you 12 coding scenarios.
Before revealing the answer, stop for a few seconds and ask yourself:
What pattern would I use here?
1. Two Numbers Add Up to a Target
You're given a sorted array.
Find whether two numbers add up to a given target, without using extra space.
What pattern would you use?
Two Pointers What should you notice? The array is already sorted, and you're looking for two numbers that together reach a target. You can start with one pointer at each end and move them based on the current sum.Answer
Move left pointer if sum is less , move right pointer if sum is more
2. Longest Substring Without Repeating Characters
Given a string, find the longest substring that contains no repeated characters.
What pattern would you use?
Sliding Window What should you notice? You're looking at a contiguous part of a string and want the longest one that satisfies a condition. That's a classic Sliding Window problem.Answer
3. Group the Anagrams
Given:
["eat", "tea", "tan", "ate", "nat", "bat"]
Group words that are anagrams of each other.
What pattern would you use?
Hash Map / Frequency Counter What should you notice? You need to quickly group words that share the same underlying property. A computed key, such as character frequencies, can be stored in a Hash Map.Answer
4. Thousands of Range-Sum Queries
You have an array and repeatedly receive queries such as:
sum(2, 7)
sum(10, 50)
sum(100, 500)
Calculating every range from scratch would be expensive.
What pattern would you use?
Prefix Sum What should you notice? You're repeatedly asking for sums over different ranges of the same array. Precomputing cumulative sums lets you answer each range query efficiently.Answer
5. Search a Rotated Sorted Array
You are given:
[6, 7, 8, 1, 2, 3, 4, 5]
The array was originally sorted and then rotated.
Find a target in O(log n) time.
What pattern would you use?
Binary Search What should you notice? The data is sorted, or partially sorted, and the problem explicitly asks for That should immediately make you think about Binary Search and its variants.Answer
O(log n) search.
6. Are the Brackets Balanced?
Determine whether this is valid:
{[()]}
And this is invalid:
{[(])}
What pattern would you use?
Stack What should you notice? The most recently opened bracket must be the first one closed. That's Last In, First Out (LIFO) — exactly what a Stack provides.Answer
7. Maximum Contiguous Subarray Sum
Find the maximum sum of any contiguous subarray:
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
What pattern would you use?
Kadane's Algorithm What should you notice? You're looking for the best sum from a contiguous part of the array. Kadane's Algorithm solves this in Answer
O(n) time.
8. Shortest Path in an Unweighted Graph
You need to find the shortest path between two nodes in an unweighted graph.
What pattern would you use?
Breadth-First Search (BFS) What should you notice? You want the fewest edges needed to reach the destination. BFS explores the graph level by level, making it a natural choice for shortest paths in an unweighted graph.Answer
9. The Knapsack Problem
You have items with different weights and values.
Your bag has a limited capacity.
For each item, you essentially have two choices:
Take it
or
Don't take it
What pattern would you use?
Dynamic Programming What should you notice? You're repeatedly making choices, and the same smaller problems appear again and again. That's a strong signal for Dynamic Programming.Answer
10. Longest Common Subsequence
Given:
"abcde"
"ace"
Find the length of their longest common subsequence.
What pattern would you use?
Dynamic Programming What should you notice? You're comparing two sequences and looking for the best common subsequence. Problems involving optimal subsequences are common Dynamic Programming patterns.Answer
11. Generate All Valid Parentheses
For n = 3, generate all valid combinations:
((()))
(()())
(())()
()(())
()()()
What pattern would you use?
Backtracking What should you notice? You need to generate many possible solutions, but only keep the ones that satisfy certain rules. That's a classic Backtracking problem.Answer
12. Find the Kth Largest Element
Given an unsorted array, find the kth largest element.
You don't need the entire array sorted.
What pattern would you use?
Quick Select What should you notice? You only need one ranked element, not the complete sorted array. Quick Select can find the kth smallest or largest element without fully sorting the data.Answer
The Pattern Recognition Cheat Sheet
After going through the questions, look at the clues again:
| When you see... | Think... |
|---|---|
| A sorted array and you're looking for two numbers | Two Pointers |
| A contiguous part of a string/array and a longest/shortest condition | Sliding Window |
| A need to quickly group or look up items by a property | Hash Map |
| Repeated sum queries over the same array | Prefix Sum |
Sorted data and O(log n) search |
Binary Search |
| Nested data where the latest item must be processed first | Stack |
| The best sum of a contiguous subarray | Kadane's Algorithm |
| The shortest path in an unweighted graph | BFS |
| Repeated choices with overlapping subproblems | Dynamic Programming |
| Generating all valid possibilities | Backtracking |
| The kth largest/smallest element without full sorting | Quick Select |
The Real Interview Skill
The goal isn't to memorize:
"This problem uses Sliding Window."
The real skill is being able to look at a new problem and ask:
What should I notice?
Is the data sorted?
Are we dealing with a contiguous range?
Are we looking for a pair?
Do we need the shortest path?
Are we making repeated choices?
Do we need to generate all possible solutions?
Does the problem require O(n), O(log n), or constant extra space?
The constraints are often clues to the solution.
That's the difference between:
Knowing DSA patterns
and
Recognizing DSA patterns.
Want to Go Deeper?
I put together a complete Backend Interview Guide covering:
- Core Java
- Java 8–21
- Multithreading
- Spring Boot
- Microservices
- Design Patterns
- Coding Round Patterns
Full guide: The Complete Java Backend Interview Guide
Feedback or questions?
Email me: kamaninikhil71@gmail.com
Top comments (0)