The Talent500 Blog

Coding patterns for Technical interviews

Programming code abstract technology background of software developer and Computer script

Technical interviews are a common part of the hiring process for software engineering roles. During these interviews, candidates are evaluated on their coding skills, problem-solving abilities, and technical knowledge. The interview format can vary, but coding questions are a key component.

Sometimes you may sit and solve 1000+ Leetcode problems and still get stuck at some coding problems. The main problem is that candidates are having trouble understanding generic patterns. It is like a template that you can use to solve multiple questions.

In this article, we will walk you through some of the most asked coding interview question patterns with examples so that next time you get a clear idea when doing a question.

So let’s get started!

Sliding Window

The Sliding Window pattern is a technique used to work with a specific range of elements within an array or linked list. It’s handy for solving tasks like finding the longest subarray with certain characteristics, such as the longest sub-array problem.

Here’s how it works: You start with the first element and create a “window” of elements. This window shifts one step to the right at a time, and its size changes to match the problem you’re tackling. The window can stay the same size or expand and contract depending on what you need to achieve.

Here’s a basic outline of how the sliding window technique works:

Two pointer approach

These types of problems mostly deal with sorted lists (arrays or linked lists), and the goal is to find a set of elements that fulfil certain conditions. It involves using two pointers that traverse the array or list from different positions or directions to achieve a condition.

Here is the basic outline for the Two pointer approach 

Breadth-First Search

This approach draws inspiration from the Breadth First Search (BFS) algorithm used to traverse trees. It employs a queue to manage nodes at each level before progressing to the subsequent level. Any problem requiring a level-by-level traversal of a tree can be effectively tackled using this strategy.

This is how BFS works

Depth First Search

DFS, inspired by how we explore mazes, delves deeply into paths before backtracking. It’s like systematically searching through a drawer’s items. 

This method is effective for finding specific routes, searching, and understanding connections in structures. It can be done by either repeatedly exploring child paths using recursion, or by managing exploration steps manually with a stack.

By going deep first, DFS can uncover hidden solutions efficiently in complex situations.

How DFS Works:

If using recursion, recursively apply the same process to the child nodes.

Binary Search

 Binary search is a clever algorithm used to find a specific value in a sorted list or array. It starts in the middle and checks if the desired value is higher or lower. Based on the result, it narrows down the search to the upper or lower half. This process continues until the target is found or the search range becomes empty.

This is how binary search works

  1. If the middle value is equal to the target, you’ve found the value and the search is successful.
  2. If the middle value is less than the target, the target must be in the right half. Update left to middle + 1 to narrow the search to the right half.
  3. If the middle value is greater than the target, the target must be in the left half. Update right to middle – 1 to narrow the search to the left half.

Top K elements

This pattern helps us find the top, smallest, or frequently occurring ‘K’ elements from a given collection of items. The best data structure for keeping track of ‘K’ elements is a heap. Think of it like special storage that helps us manage these ‘K’ elements effectively.

Here is the minimal workflow of this approach

Subsets

Many coding interview challenges involve working with different arrangements and selections of elements from a set. Starting with an empty set, we will iterate through all numbers one-by-one, and add them to existing sets to create subsets.

Let’s take a simple problem and understand how it works:

The question is from Leetcode: Given an integer array nums of unique elements, return all possible subsets. The solution set must not contain duplicate subsets. Return the solution in any order.

Input: nums = [1,2,3]

Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

  1. We will start with an empty set: [ [ ] ]
  2. We will now add the first number to the set [ [] , [1] ]
  3. Now add the second number(2) to all the elements inside the set [2],[1,2]] and add it to the original set [ [], [1],[2],[1,2] ]
  4. Repeat the steps until all elements are traversed.

Here is a basic implementation of the above approach in JavaScript:

// Define a function called findallSubsets with a default array parameter sets.

function findallSubsets(sets = [1, 2, 3]) {

  // Create an initial subset containing an empty array.

  let subsets = [[]];

  // Loop through each element in the sets array.

  for (i = 0; i < sets.length; i++) {

    // Get the current element.

    let current = sets[i];

    // Store the current number of subsets.

    let lengthofSubset = subsets.length;

    // Loop through existing subsets.

    for (j = 0; j < lengthofSubset; j++) {

      // Create a new subset by adding the current element to an existing subset.

      // This creates a new subset for each existing subset.

      subsets.push([subsets[j], current]);

    }

  }

  // Return the final array of subsets.

  return subsets;

}

// Call the findallSubsets function without passing any arguments.

// By default, it will use [1, 2, 3] as the input set.

findallSubsets();

Output 


While there are several other coding patterns that may be less frequently discussed compared to the aforementioned ones, if you’re interested in expanding your knowledge about these patterns, numerous free resources are available on the internet. These resources can provide you with further insights and guidance.

Wrapping it up

Thanks for taking the time to read about coding patterns for technical interviews. We’ve explored some really handy strategies that often come up during interviews. 

From Two Pointers to Sliding Window, and from BFS to DFS, we’ve broken down these techniques to help you understand how they work. By getting the hang of these patterns, you’ll be ready to tackle various coding challenges in interviews with more confidence.

 Good luck with your interview preparations!

0