Top 250 Algorithms Interview Questions and Answers
Updated 5 Jul 2025

Asked in Amazon

Q. Valid Sudoku Problem Statement
You are given a 9 X 9 2D matrix named MATRIX
which contains some cells filled with digits (1 to 9) and some cells left empty (denoted by 0).
Your task is to determine if there is a way to fill all th...read more
The task is to determine if a given 9x9 matrix can be filled with digits 1-9 to form a valid Sudoku solution.
Iterate through each cell in the matrix.
For each empty cell, try filling it with a digit from 1-9 and check if it satisfies the Sudoku condit...read more

Asked in Wipro

Q. Knapsack Problem Statement
There is a potter with a limited amount of pottery clay (denoted as 'K' units) who can make 'N' different items. Each item requires a specific amount of clay and yields a certain profit. The goal is to m...read more
The Knapsack Problem involves maximizing profit by choosing which items to make with limited resources.
Understand the problem statement and constraints provided.
Implement a dynamic programming solution to solve the Knapsack Problem efficiently.
Iterat...read more

Asked in Mentor Graphics

Q. Devise an algorithm to determine the Nth-to-Last element in a singly linked list of unknown length. If N = 0, then your algorithm must return the last element. You should parse the list only once
Algorithm to find Nth-to-Last element in a singly linked list of unknown length
Traverse the list and maintain two pointers, one at the beginning and one at Nth node from beginning
Move both pointers simultaneously until the second pointer reaches the ...read more

Asked in Josh Technology Group

Q. Maximum Sum BST Problem Statement
You are presented with a Binary Tree 'root', which may not necessarily be a Binary Search Tree (BST). Your objective is to identify the maximum sum of node values in any subtree that qualifies as ...read more
The task is to find the maximum sum of node values of any subtree that is a Binary Search Tree(BST).
Traverse the binary tree in a bottom-up manner
For each node, check if it forms a BST and calculate the sum of its subtree
Keep track of the maximum sum...read more

Asked in TCS

Q. Space Survival Game Challenge
Ninja is in space with unlimited fuel in his super spaceship. He starts with a health level H
and his spaceship has an armour A
. Ninja can be on only one of the three planets at a time: VENUS, MARS, o...read more
Calculate the maximum time Ninja can survive in a space survival game challenge.
Create a function that takes initial health and armour as input for each test case
Simulate Ninja's movement between planets and update health and armour accordingly
Keep t...read more

Asked in InvestCloud

Q. Given an array, find the index of a specific value.
Finding the index value of a given element in an array of strings.
Iterate through the array and compare each element with the given value.
If a match is found, return the index of that element.
If no match is found, return -1.

Asked in InMobi

Q. Given an array, find three numbers a, b, and c such that a + b = c.
Find 3 numbers in an array where a+b=c.
Loop through the array and check for all possible combinations of a and b.
Use a hash table to store the values of a and b, and check if c is present in the hash table.
Sort the array and use two pointers to find ...read more

Asked in Fiat Chrysler Automobiles

Q. TELL ME SOMETHING ABOUT NMST
NMST stands for National Mathematics and Science Talent Examination.
NMST is a competitive exam conducted for students in the field of mathematics and science.
It aims to identify and nurture talented students in these subjects.
The exam is open to stud...read more

Asked in DXC Technology and 3 others

Q. Write a program to print your name.
A program to print name using an array of strings.
Declare an array of strings with the name.
Assign the name to the array.
Loop through the array and print each string.

Asked in Rostfrei Steels and 5 others

Q. What is efficiency?
Efficiency is the ability to do something in a way that achieves maximum productivity with minimum wasted effort or expense.
Efficiency is a measure of how well a system or process performs.
It is often expressed as a percentage of the total input that...read more
Algorithms Jobs




Asked in Oracle

Q. Given a map of coffee shops and a person on the map, give the closest n coffee shops to him.
Given a map of coffee shops and a person, find the closest n coffee shops to him.
Use the person's location and calculate the distance to each coffee shop on the map.
Sort the coffee shops by distance and return the closest n.
Consider using a data stru...read more

Asked in ShareChat

Q. Square Root of an Integer Challenge
Given an integer 'A', the objective is to find the largest non-negative integer whose square is less than or equal to 'A'.
The square of a number is defined as the product of the number with its...read more
Find the largest non-negative integer whose square is less than or equal to a given integer.
Use binary search to efficiently find the square root of the given integer.
Start with a range from 0 to the given integer and iteratively narrow down the rang...read more

Asked in BridgeLabz Solutions

Q. Write an insertion sort algorithm.
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time.
Start with the second element and compare it with the first element, swap if necessary
Move to the third element and compare it with the first and secon...read more

Asked in Amazon

Q. Find Pair Sum Equal to K
Given an integer array arr
and an integer 'Sum'
, find and return the total number of pairs in the array which, when added together, result in the 'Sum'
.
Note:
The array can contain duplicate elements. Pair...read more
Find total number of pairs in array that sum to given value.
Use a hashmap to store frequency of each element in the array.
Iterate through the array and check if (Sum - current element) exists in the hashmap.
Increment count of pairs if found and updat...read more

Asked in YASH Technologies

Q. What is GCD? Explain it in detail.
GCD stands for Greatest Common Divisor. It is the largest positive integer that divides two or more numbers without leaving a remainder.
GCD is used to find the highest common factor between two or more numbers.
It is often used in mathematical calcula...read more

Asked in Jio Platforms

Q. Distinct Subarrays with At Most K Odd Elements
Given an array A
of N
integers, determine the total number of distinct subarrays that contain at most K
odd elements.
Example:
Input:
A = [3, 2, 3], K = 1
Output:
4
Explanation:
The d...read more
Count the total number of distinct subarrays with at most K odd elements in an array.
Iterate through all subarrays and count the number of odd elements in each subarray.
Use a hashmap to keep track of the count of distinct subarrays with at most K odd...read more

Asked in PayPal

Q. Painter's Partition Problem
You are given an array/list of length 'N'. Each element of the array/list represents the length of a board. There are 'K' painters available to paint these boards. Each unit of a board takes 1 unit of t...read more
Find the minimum time required for 'K' painters to paint 'N' boards with given lengths.
Use binary search to find the minimum and maximum possible time to paint all boards.
Iterate through the possible time range and check if it is feasible to paint al...read more

Asked in Mystifly Consulting

Q. Implement a recursive function to calculate the Fibonacci number.
Code for Fibonacci number using recursion
Define a function that takes an integer as input
If the input is 0 or 1, return the input
Else, return the sum of the function called with input-1 and input-2
Call the function with the desired input

Asked in Microsoft Corporation

Q. IP Address Formation from String
Given a string S
consisting only of digits from 0 to 9, your task is to find all potential IP addresses that can be formed from S
and list them in lexicographical order. If it's not possible to gen...read more
Given a string of digits, find all potential valid IP addresses that can be formed from it.
Split the string into four parts and check if each part is a valid IP segment (0-255).
Use backtracking to generate all possible combinations of valid IP addres...read more

Asked in Samsung

Q. Find all subsets of a number set such that the sum of these numbers is equal to a given number.
Find all subsets of a number set with a given sum
Use a recursive approach to generate all possible subsets
For each subset, calculate the sum and check if it matches the given number
Store the subsets that satisfy the condition

Asked in Flipkart

Q. Given an array of integers, find Pythagorean triplets. i.e. find a, b, and c which satisfies a^2 + b^2 = c^2. Integers could be positive or negative.
Find Pythagorean triplets in an array of integers.
Loop through the array and pick two numbers at a time.
Calculate the sum of squares of the two numbers.
Check if the sum is a perfect square.
If yes, then it is a Pythagorean triplet.
Repeat until all pos...read more

Asked in Microsoft Corporation

Q. Given a string and a matrix of characters, determine if the string exists in the matrix.
Find if a given string exists in a given matrix of characters
Iterate through each character in the matrix and check if it matches the first character of the given string. If it does, perform a depth-first search to check if the rest of the string can...read more

Asked in Expedia Group

Q. Version Comparison
Given two strings, Version1
and Version2
, each representing version numbers, determine which one is the latest version.
Explanation:
The input strings consist of digits and dots only. Both strings start and end ...read more
Compare two version numbers to determine the latest version.
Split the version numbers by '.' and compare each part from left to right.
If a part in Version2 is greater than the corresponding part in Version1, Version2 is the latest.
Handle cases where ...read more

Asked in Goldman Sachs

Q. Counting Pairs Problem Statement
Given a positive integer N
, determine the count of all possible positive integral pairs (X, Y)
that satisfy the equation 1/X + 1/Y = 1/N
.
Example:
Input:
T = 1
N = 2
Output:
3
Explanation:
The solut...read more
Count the number of positive integral pairs (X, Y) that satisfy the given equation.
Iterate through all possible values of X and calculate corresponding Y to check if the equation is satisfied.
Optimize the solution by considering the constraints and p...read more

Asked in Walmart and 6 others

Q. Graph Coloring Problem
You are given a graph with 'N' vertices numbered from '1' to 'N' and 'M' edges. Your task is to color this graph using two colors, such as blue and red, in a way that no two adjacent vertices (connected by a...read more
Given a graph with 'N' vertices and 'M' edges, determine if it can be colored using two colors without adjacent vertices sharing the same color.
Use graph coloring algorithm like BFS or DFS to check if the graph can be colored with two colors without ...read more

Asked in Atlassian

Q. Job Scheduling Problem
You are provided with a list of jobs, where each job has a specific deadline and profit. The goal is to schedule these jobs such that the total profit is maximized. Each job requires exactly one unit of time...read more
The goal is to schedule jobs to maximize profit while meeting deadlines. Each job takes one unit of time and only one job can be scheduled at a time.
Sort the jobs in decreasing order of profit
Iterate through the sorted jobs and schedule them based on...read more
Asked in Nutrabay

Q. How do you find all distinct elements in a list?
To find distinct elements in a list
Create an empty set
Iterate through the list and add each element to the set
Return the set

Asked in Amazon

Check if two strings are anagrams by comparing the sorted versions of the strings.
Sort both strings and compare if they are equal.
Use a hashmap to store the frequency of characters in each string and compare the maps.
Ignore spaces and punctuation whe...read more

Asked in Effiya Technologies

Q. Design an algorithm for the snake and ladders game.
Algorithm for Snake and Ladders game
Create a board with 100 squares
Assign snakes and ladders to specific squares
Roll a dice to move player's token on the board
Check if the new position is a snake or ladder
Repeat until a player reaches the final squar...read more

Asked in Community Brands

Q. How can hash collisions be avoided?
To avoid hash collisions, use a good hash function, increase the size of the hash table, and handle collisions using techniques like chaining or open addressing.
Use a good hash function that distributes the keys evenly across the hash table.
Increase ...read more
Top Interview Questions for Related Skills
Interview Experiences of Popular Companies










Interview Questions of Algorithms Related Designations



Reviews
Interviews
Salaries
Users

