DSA interview questions for placements: the patterns freshers must know

Most DSA interview questions are not new. They are variations on a small set of recurring patterns, and once you can recognise the pattern behind a question, the solution follows almost mechanically. Freshers who memorise fifty specific problems stall on the fifty-first. Freshers who learn the seven or eight underlying patterns can attack a problem they have never seen.

Prashant Jain

KnowledgeGate AI educator

Updated 14 Jul 20265 min read

Most DSA interview questions are not new. They are variations on a small set of recurring patterns, and once you can recognise the pattern behind a question, the solution follows almost mechanically. Freshers who memorise fifty specific problems stall on the fifty-first. Freshers who learn the seven or eight underlying patterns can attack a problem they have never seen.

This post walks through the core patterns that placement and product-company interviews rely on, each with a worked example and its complexity. Learn to spot these, and the coding round stops being a memory test and becomes pattern recognition.

Two pointers: shrink the search from both ends

The two-pointer pattern uses two indices moving through a sorted array, usually one from each end, to avoid checking every pair.

Take the classic: given a sorted array, find two numbers that sum to a target. The brute force checks every pair in O(n squared). With two pointers, put `left` at the start and `right` at the end. If `arr[left] + arr[right]` is too small, move `left` right to increase the sum; if too big, move `right` left. You reach the answer in one pass.

  • Complexity: O(n) time, O(1) extra space, on an already sorted array.

  • How it is tested: pair-sum, three-sum, container-with-most-water, removing duplicates in place. The tell is a sorted array and a question about pairs or ranges.

Sliding window: a moving range over a sequence

The sliding window pattern maintains a contiguous range and slides it across an array or string, expanding and contracting instead of recomputing from scratch.

Example: find the longest substring without repeating characters. Keep a window and a set of characters inside it. Expand the right edge, adding characters; when you hit a repeat, shrink from the left until the repeat is gone. Track the largest window size seen.

A string with a highlighted window sliding left-to-right, showing the right pointer expanding to add a new character and the left pointer contracting when a duplicate appears, with the running maximum length annotated.
  • Complexity: O(n), because each character enters and leaves the window at most once.

  • How it is tested: longest or shortest substring under a constraint, maximum sum of k consecutive elements, anagram detection.

Hashing: trade space for constant-time lookup

Hashing stores elements in a hash map or set so membership and counting become O(1) on average. It is the pattern that converts an O(n squared) nested scan into a single pass.

Example: does an array contain a duplicate? Walk once, inserting each element into a set; if an element is already present, you have your answer. Or, for two-sum on an unsorted array, store each number's complement in a map and check for it as you go.

  • Complexity: O(n) time, O(n) space, average case. Worst-case lookups degrade with poor hash distribution.

  • How it is tested: frequency counts, first-non-repeating character, subarray-sum-equals-k, grouping anagrams. Whenever a question asks "have I seen this before", think hashing.

Recursion and backtracking: build, test, undo

Recursion solves a problem by reducing it to smaller instances of itself. Backtracking is recursion that builds a partial solution, tests it, and undoes the last step when it leads nowhere.

Example: generate all subsets of a set. At each element, branch twice, one path includes it, one excludes it, and recurse on the rest. The base case is an empty remaining set, where you record the built subset.

  • Complexity: subsets and permutations are exponential by nature (there are 2 to the n subsets), so the cost is in the output size. Backtracking's value is pruning branches early so you never explore dead ends fully.

  • How it is tested: permutations, combinations, N-queens, Sudoku, word search on a grid. The signal is "find all" or "is there a valid arrangement".

Trees: recursion with two branches

Binary-tree questions are recursion in disguise. Most reduce to "solve the left subtree, solve the right subtree, combine".

Example: the height of a binary tree is one plus the maximum of the left and right subtree heights, with an empty tree having height zero. Traversals (inorder, preorder, postorder) follow the same recursive shape, and level-order traversal uses a queue instead.

  • Complexity: O(n) to visit every node once; the recursion stack costs O(h), the tree's height.

  • How it is tested: traversals, height and diameter, lowest common ancestor, checking if a tree is balanced or a valid binary search tree.

Graphs: traversal is the foundation

Graph questions almost always start from a traversal, breadth-first search (BFS) or depth-first search (DFS), and build from there.

Example: count the connected components in an undirected graph. Run DFS or BFS from each unvisited node; each fresh start is one new component. BFS also gives the shortest path in an unweighted graph, because it explores level by level.

  • Complexity: O(V plus E) for both BFS and DFS, where V is vertices and E is edges, using an adjacency list.

  • How it is tested: number of islands, cycle detection, shortest path in a grid, topological sort of dependencies. If the data is a network or a grid of connections, it is a graph problem.

Dynamic programming: remember, do not recompute

Dynamic programming (DP) solves problems with overlapping subproblems by solving each subproblem once and storing the result, instead of recomputing it exponentially many times.

Example: the n-th Fibonacci number. Naive recursion recomputes the same values exponentially. Store each computed value in an array (or two rolling variables) and the cost collapses to linear. The same "define a state, write a recurrence, fill a table" method solves knapsack, longest common subsequence, and coin change.

  • Complexity: typically O(number of states times work per state). Fibonacci is O(n); the 0/1 knapsack is O(n times capacity).

  • How it is tested: min or max cost, count-the-ways, can-you-partition questions. The tell is that a greedy choice fails and the same subproblem recurs.

How these patterns show up in placement rounds

Placement coding rounds and product-company interviews draw on exactly these patterns, usually one or two per problem, occasionally combined (a sliding window that uses a hash map, a graph that needs DP on top). KnowledgeGate's published question bank carries over 1,400 data-structures questions and more than 1,100 algorithm questions, so there is deep practice available on each of these patterns rather than a handful of examples.

The way to use them is to solve by pattern, not by problem. When you read a new question, ask which pattern it fits before you write a line: sorted array and pairs points to two pointers; "have I seen this" points to hashing; "find all arrangements" points to backtracking; overlapping subproblems point to DP.

Your next step

If you want these patterns taught in order with interview-focused problems and solutions, our DSA using Java course is built for product-based placement coding rounds and technical interviews. If your programming fundamentals need firming up first, the Coding for Placements course covers C, C++, Java and Python from the basics.

For deeper single-topic study, our dynamic programming explained and graph algorithms: BFS, DFS and shortest paths walkthroughs go step by step, and stacks and queues explained covers the structures that underpin traversal. The full placement preparation category lists our company-wise coverage.

Learn the pattern behind the question, practise until recognition is instant, and the coding round becomes marks you count on rather than a topic you fear.