Top 250 Algorithms Interview Questions and Answers

Updated 5 Jul 2025

Asked in Amazon

1w ago

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

Ans.

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

1w ago

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

Ans.

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

1w ago

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

Ans.

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

1w ago

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

Ans.

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

Are these interview questions helpful?

Asked in TCS

4d ago

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

Ans.

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

4d ago

Q. Given an array, find the index of a specific value.

Ans.

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.

Share interview questions and help millions of jobseekers 🌟
man with laptop

Asked in InMobi

5d ago

Q. Given an array, find three numbers a, b, and c such that a + b = c.

Ans.

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

Q. TELL ME SOMETHING ABOUT NMST

Ans.

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

2w ago

Q. Write a program to print your name.

Ans.

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

1w ago

Q. What is efficiency?

Ans.

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

IBM India Pvt. Limited logo
Senior Software Developer 5-10 years
IBM India Pvt. Limited
4.0
₹ 8 L/yr - ₹ 34 L/yr
(AmbitionBox estimate)
Hyderabad / Secunderabad
IBM India Pvt. Limited logo
Senior Software Developer 3-6 years
IBM India Pvt. Limited
4.0
Kochi
IBM India Pvt. Limited logo
Staff Software Engineer 5-10 years
IBM India Pvt. Limited
4.0
₹ 17 L/yr - ₹ 32 L/yr
(AmbitionBox estimate)
Bangalore / Bengaluru

Asked in Oracle

1d ago

Q. Given a map of coffee shops and a person on the map, give the closest n coffee shops to him.

Ans.

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

6d ago

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

Ans.

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

5d ago

Q. Write an insertion sort algorithm.

Ans.

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

1w ago

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
Ans.

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

1w ago

Q. What is GCD? Explain it in detail.

Ans.

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

2w ago

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

Ans.

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

1w ago

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

Ans.

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

2w ago

Q. Implement a recursive function to calculate the Fibonacci number.

Ans.

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

5d ago

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

Ans.

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

1w ago

Q. Find all subsets of a number set such that the sum of these numbers is equal to a given number.

Ans.

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

2w ago

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.

Ans.

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

2w ago

Q. Given a string and a matrix of characters, determine if the string exists in the matrix.

Ans.

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

1w ago

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

Ans.

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

1d ago

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

Ans.

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

2w ago

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

Ans.

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

2w ago

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

Ans.

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

2w ago

Q. How do you find all distinct elements in a list?

Ans.

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

4d ago
Q. How can you check if two strings are anagrams of each other?
Ans.

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

2w ago

Q. Design an algorithm for the snake and ladders game.

Ans.

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

5d ago

Q. How can hash collisions be avoided?

Ans.

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

Previous
1
2
3
4
5
6
7
Next

Interview Experiences of Popular Companies

TCS Logo
3.6
 • 11.1k Interviews
Infosys Logo
3.6
 • 7.9k Interviews
Wipro Logo
3.7
 • 6.1k Interviews
Cognizant Logo
3.7
 • 5.9k Interviews
Amazon Logo
4.0
 • 5.4k Interviews
Capgemini Logo
3.7
 • 5.1k Interviews
Oracle Logo
3.7
 • 893 Interviews
Goldman Sachs Logo
3.5
 • 392 Interviews
Adobe Logo
3.9
 • 247 Interviews
View all
interview tips and stories logo
Interview Tips & Stories
Ace your next interview with expert advice and inspiring stories
Algorithms Interview Questions
Share an Interview
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 Lakh+

Reviews

10L+

Interviews

4 Crore+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits