Oct 13, 2024


Throughout the years, numerous computer algorithms have been developed. In software engineering interviews, companies frequently assess a candidate's understanding of these algorithms.

Array Algorithms

  1. Merge Sort: A divide-and-conquer algorithm that sorts an array by dividing it into halves, sorting each half, and merging them.

  2. Quick Sort: A divide-and-conquer algorithm that sorts an array by selecting a pivot and partitioning the array around the pivot.

  3. Two-Pointer Technique: A technique often used to solve problems involving arrays or linked lists. It involves using two pointers to iterate through the data structure, often to find pairs or subarrays that meet certain conditions.

Example questions: - Find two numbers in a sorted array that add up to a specific target. - Determine if there exists a subarray with a given sum in an unsorted array.

  1. Kadane's Algorithm: This algorithm efficiently finds the maximum sum of a contiguous subarray within a one-dimensional array. It works by iterating through the array while maintaining a running sum of the current subarray. If the running sum becomes negative, it is reset to zero, as a negative sum would decrease the total sum of any subsequent subarray. The algorithm keeps track of the maximum sum encountered during the iteration. Kadane's Algorithm operates in O(n) time complexity, making it highly efficient for large datasets.

Example questions: - What is the maximum sum of a contiguous subarray in the given array? - How can you modify Kadane's Algorithm to also return the start and end indices of the subarray with the maximum sum?

  1. Sliding Window Technique: A method used to solve problems involving arrays or lists by maintaining a subset of elements that satisfies certain conditions. It is particularly useful for problems involving subarrays or subsequences.

Example questions: - Find the maximum sum of a subarray with a fixed length. - Determine the smallest subarray length with a sum greater than or equal to a given value.

  1. Prefix Sum: A technique that involves precomputing an array of prefix sums to quickly calculate the sum of elements in any subarray. This is useful for range sum queries and can be computed in O(n) time, with each query answered in O(1) time.

Example questions: - Given an array of integers, find the length of the longest subarray with a sum equal to zero. - You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Find the number of ways to assign symbols to make the sum of integers equal to target S. - Given an array of integers, find the maximum product of a subarray. - You are given a string and a dictionary of words. Determine if the string can be segmented into a space-separated sequence of one or more dictionary words. - Given a string, find the length of the longest substring without repeating characters.

Search Algorithms

  1. Binary Search: Efficiently finds the position of a target value within a sorted array.

Example questions: - Find the square root of a number with a precision of up to 6 decimal places. - Search for a target value in a rotated sorted array. - Find the smallest element in a sorted array that is greater than a given target. - Locate the first and last position of a target value in a sorted array.

  1. Depth-First Search (DFS): An algorithm for traversing or searching tree or graph data structures, starting at the root and exploring as far as possible along each branch.

Example questions: - Given a binary tree, return all root-to-leaf paths. - Find all connected components in an undirected graph. - Determine if a path exists between two nodes in a directed graph. - Generate all possible permutations of a given list of numbers. - Solve a maze represented by a 2D grid, where 0s are paths and 1s are walls, to find a path from start to finish.

  1. Breadth-First Search (BFS): An algorithm for traversing or searching tree or graph data structures, starting at the root and exploring the neighbor nodes at the present depth prior to moving on to nodes at the next depth level.

Example questions: - Given a binary tree, return the level order traversal of its nodes' values. - Find the shortest path in an unweighted graph from a starting node to a target node. - Determine the minimum number of moves required to transform one word into another using a given set of transformations. - Given a 2D grid of '1's (land) and '0's (water), count the number of islands. - Find the minimum number of steps to reach the end of a maze from a starting point, where some cells are blocked.

Graph Algorithms

  1. Dijkstra's Algorithm: Elegantly determines the shortest path between nodes in a graph, often used to model road networks.

Example questions: - Given a graph with weighted edges, determine the shortest path from a starting node to all other nodes. - You are given a map of cities connected by roads, each with a travel time. Find the quickest route from one city to another. - In a network of computers, find the minimum time required to send a message from one computer to all others. - Given a grid where each cell has a cost, find the path from the top-left to the bottom-right with the minimum cost. - You have a list of flights with their durations. Determine the shortest travel time from a starting airport to a destination airport.

  1. A Search Algorithm: A pathfinding and graph traversal algorithm, which is often used in games and web-based maps. This algorithm is similar to Dijkstra's algorithm but incorporates heuristics to prioritize paths that appear to lead more directly to the goal. While Dijkstra's algorithm explores all possible paths to find the shortest one, A uses a heuristic to estimate the cost to reach the goal from a given node, allowing it to focus on more promising paths and often find the shortest path more efficiently.

  2. Kruskal's Algorithm: A minimum spanning tree algorithm that finds an edge of the least possible weight that connects any two trees in a forest.

  3. Prim's Algorithm: A minimum spanning tree algorithm that starts with a single vertex and grows the spanning tree one edge at a time.

  4. Bellman-Ford Algorithm: Computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.

Optimization and Problem Solving

  1. Dynamic Programming: This technique addresses complex problems by decomposing them into simpler subproblems, often storing intermediate results to avoid redundant calculations in future computations.

  2. Backtracking: A general algorithm for finding all (or some) solutions to computational problems, notably constraint satisfaction problems.

  3. Greedy Algorithms: Algorithms that make the locally optimal choice at each stage with the hope of finding a global optimum.

  4. Divide and Conquer: An algorithm design paradigm based on multi-branched recursion.

String Algorithms

These algorithms are not frequently encountered in coding interviews, but having knowledge of them can earn you extra credit.

  1. Knuth-Morris-Pratt (KMP) Algorithm: A string searching (or substring searching) algorithm that searches for occurrences of a word within a main text string.

  2. Rabin-Karp Algorithm: A string searching algorithm that uses hashing to find any one of a set of pattern strings in a text.


Join us on WeChat

Ming Dao School uses 1-1 coaching and group events to help high-tech professionals grow their careers and handle career transitions.

If you like to join our upcoming mock system design interview events or other coaching programs, please contact us on LinkedIn.