Top 250 Algorithms Interview Questions and Answers
Updated 13 Jul 2025

Asked in Capgemini

Q. One question of sorting for a list of people belonging to different cities and states.
Sort a list of people by their cities and states.
Use a sorting algorithm like quicksort or mergesort.
Create a custom comparator function that compares the city and state of each person.
If two people belong to the same city and state, sort them by the...read more

Asked in ConsultAdd

Q. Find the second largest element in an array with the least time complexity.
Find the 2nd largest element in an array with the least time complexity.
Sort the array in descending order and return the element at index 1.
Initialize two variables to keep track of the largest and second largest elements.
Iterate through the array a...read more

Asked in Bank of America

Q. Alternate Print Problem Statement
Given two strings A
and B
, your task is to print these two strings in an alternating sequence by indices. That is, the first character of 'A', the first character of 'B', followed by the second ch...read more
The task is to print two strings in an alternating sequence by indices.
Iterate through both strings simultaneously and append characters alternately
Handle the case where one string is longer than the other
Use two pointers to keep track of the current...read more

Asked in Samsung

The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence with increasing order.
Use dynamic programming to solve the LIS problem efficiently.
Maintain an array to store the length of the LIS ...read more

Asked in Intellect Design Arena

Q. Remove Duplicates from a Sorted Array
Given a sorted integer array ARR
of size N
, you need to remove duplicates such that each element appears only once and return the length of this new array.
Input:
The first line of input conta...read more
Remove duplicates from a sorted array in-place and return the length of the modified array.
Use two pointers approach to track unique elements and duplicates.
Modify the input array in-place without using extra space.
Return the length of the modified a...read more

Asked in TCS

Q. Strings of Numbers Problem Statement
You are given two integers 'N' and 'K'. Consider a set 'X' of all possible strings of 'N' number of digits where all strings only contain digits ranging from 0 to 'K' inclusive. Your task is to...read more
The task is to find a string of minimal length that includes every possible string of N digits with digits ranging from 0 to K as substrings.
Generate all possible strings of N digits with digits from 0 to K
Concatenate these strings in a way that all ...read more

Asked in Amazon

Q. Given a sorted array that has been rotated, search for an element in the array.
Search an element in sorted rotated array.
Use binary search to find the pivot point where the array is rotated.
Divide the array into two subarrays and perform binary search on the appropriate subarray.
Handle the cases where the target element is at t...read more

Asked in Bharti Airtel

The minimum number of characters needed to convert a string into a palindrome is the length of the string minus the length of the longest palindromic subsequence of the string.
Find the longest palindromic subsequence of the given string.
Subtract the ...read more

Asked in JUSPAY

Q. Largest Cycle in Maze Problem Statement
Given a maze represented by 'N' cells numbered from 0 to N-1, and an array arr
of 'N' integers where arr[i]
denotes the cell number that can be reached from the 'i'-th cell in one step, iden...read more
Identify the length of the largest cycle in a maze represented by cells and an array of integers.
Iterate through each cell and find the cycle length using DFS or Floyd's Tortoise and Hare algorithm.
Handle self-cycles and cells with no exit by checkin...read more

Asked in Sigmoid

Q. K-th Element of Two Sorted Arrays
You are provided with two sorted arrays, arr1
and arr2
, along with an integer k
. By merging all elements from arr1
and arr2
into a new sorted array, your task is to identify the k
th smallest eleme...read more
The task is to find the kth smallest element of a merged array created by merging two sorted arrays.
Merge the two sorted arrays into a single sorted array
Return the kth element of the merged array
Algorithms Jobs




Asked in TCS

Q. Constellation Identification Problem
Given a matrix named UNIVERSE
with 3 rows and 'N' columns, filled with characters {#, *, .}, where:
- '*' represents stars.
- '.' represents empty space.
- '#' represents a separator between galaxie...read more
The task is to identify constellations shaped like vowels within a matrix filled with characters {#, *, .}.
Iterate through the matrix to find 3x3 constellations shaped like vowels.
Check for vowels 'A', 'E', 'I', 'O', 'U' in the constellations.
Print t...read more

Asked in Wells Fargo

Q. Number of Mismatching Bits
Determine the number of bits that differ between the binary representations of two given integers "first" and "second".
Input:
The input starts with an integer ‘T’ representing the number of test cases.
E...read more
Calculate the number of mismatching bits between two given integers in their binary representation.
Convert the integers to binary representation using bitwise operations.
Count the number of differing bits by comparing the binary representations.
Retur...read more

Asked in TCS

Q. What are the differences between Merge Sort and Heap Sort?
Merge sort and Heap sort are both comparison-based sorting algorithms, but they differ in their approach and performance.
Merge sort is a divide-and-conquer algorithm that divides the input array into two halves, recursively sorts them, and then merge...read more

Asked in Amazon

Q. Maximum 0-1 Distance Problem Statement
Given a binary matrix of size N*M, find the maximum distance 'di' for every 0-cell to its nearest 1-cell, where the distance is measured using Manhattan distance. The task is to determine the...read more
Find the maximum Manhattan distance from a 0-cell to its nearest 1-cell in a binary matrix.
Iterate through the matrix to find all 0-cells and calculate their distances to nearest 1-cells using Manhattan distance formula
Keep track of the maximum dista...read more

Asked in HashedIn by Deloitte

Q. How would you optimize code for generating prime numbers?
Use Sieve of Eratosthenes algorithm to optimize prime number generation.
Implement Sieve of Eratosthenes algorithm to eliminate non-prime numbers
Use boolean array to mark non-prime numbers
Start with 2 and mark all its multiples as non-prime, then move...read more

Asked in People Tech Group

Q. Write code to generate the Fibonacci sequence up to 10.
Code for Fibonacci series up to 10
Declare two variables to store the first two numbers of the series
Use a loop to generate the next numbers in the series by adding the previous two
Print the series up to 10

Asked in Phenom

Q. If you flip a card, the next card will get reversed. Given N cards placed from left to right, how much time will it take to get all cards reversed? Consider time complexity.
To reverse N cards, time complexity is O(N).
The time complexity to reverse N cards is O(N).
The algorithm needs to flip each card once, so the time complexity is linear.
The time it takes to reverse all cards is directly proportional to the number of c...read more

Asked in Flipkart

Q. How would you optimize restaurant listings on Swiggy?
Optimizing the listing of Restaurants on Swiggy involves using data-driven strategies to improve visibility, relevance, and user experience.
Analyze user behavior and preferences to understand their needs and preferences
Implement a ranking algorithm b...read more

Asked in Samsung

Q. Explain the logic of quick sort.
Quick sort is a divide and conquer algorithm that sorts an array by partitioning it into two sub-arrays.
Choose a pivot element from the array
Partition the array around the pivot element
Recursively apply the above steps to the sub-arrays
Combine the so...read more

Asked in PTC

Q. Write a recursive function to reverse a number.
Reverse a number using recursion.
Define a recursive function that takes the number as input
Base case: if the number is less than 10, return the number
Recursive case: return the last digit of the number concatenated with the result of calling the func...read more

Asked in KPIT Technologies

Q. Write code to determine if two words are anagrams.
Code to check if two words are anagrams
Convert both words to lowercase
Remove all spaces and punctuation
Sort the characters in both words
Compare the sorted words

Asked in Rolls-Royce

Q. What are the types of ML algorithms? Give an example of each.
There are several types of ML algorithms, including supervised learning, unsupervised learning, and reinforcement learning.
Supervised learning: algorithms learn from labeled data to make predictions or classifications (e.g., linear regression, decisi...read more

Asked in TCS

Q. Write a program to find even and odd numbers using lambda expressions.
Program to find even and odd number using lambda expression
Create a list of numbers
Use lambda expression to filter even and odd numbers
Print the even and odd numbers

Asked in Ajanta Pharma

Q. How is RPN calculated?
RPN is calculated using a postfix notation where operators come after the operands.
RPN stands for Reverse Polish Notation
Operators come after the operands in RPN
RPN is evaluated using a stack data structure
Example: 3 4 + 5 * = 35
Example: 10 5 / 2 * =...read more

Asked in embedUR Systems

Q. What is the difference between BFS and DFS?
BFS and DFS are two popular graph traversal algorithms. BFS explores the graph level by level while DFS explores the graph depth by depth.
BFS uses a queue data structure while DFS uses a stack or recursion.
BFS is guaranteed to find the shortest path ...read more

Asked in Info Edge

Q. Consecutive Characters Problem Statement
Given a matrix of lowercase characters with dimensions 'N' rows and 'M' columns, and a string 'STR', your goal is to find the longest consecutive character path length for each character in...read more
Find the longest consecutive character path length for each character in a matrix given a string.
Iterate through the matrix and for each character in the string, find the longest consecutive path in all 8 directions.
Keep track of the current path len...read more

Asked in Nagarro

Q. Ninja and the New Year Guests Problem
Ninja has organized a new year party and wants to verify if the guests are programmers by challenging them with a coding task. As an invited programmer, you're tasked to solve it.
You need to ...read more
Compute the number of valid permutations of integers from 0 to N-1 such that at least K positions satisfy ARR[I] = I.
Use dynamic programming to solve the problem efficiently.
Consider the cases where K is equal to N or N-1 separately.
Modulo the result...read more

Asked in Avalara Technologies

Q. Given an integer matrix, find the length of the longest increasing path in the matrix.
Find longest increasing sequence in a matrix
Iterate through each element in the matrix
For each element, check its neighbors to find the longest increasing sequence
Keep track of the longest sequence found so far

Asked in Intuit

Use sliding window technique to find smallest subarray with k distinct values.
Use a sliding window approach to keep track of the subarray with k distinct values.
Use a hashmap to store the frequency of each element in the window.
Slide the window to th...read more

Asked in Amazon

Q. Next Smaller Element Problem Statement
You are provided with an array of integers ARR
of length N
. Your task is to determine the next smaller element for each array element.
Explanation:
The Next Smaller Element for a given array ...read more
Find the next smaller element for each element in an array.
Iterate through the array from right to left and use a stack to keep track of elements.
Pop elements from the stack until a smaller element is found or the stack is empty.
If a smaller element ...read more
Top Interview Questions for Related Skills
Interview Experiences of Popular Companies










Interview Questions of Algorithms Related Designations



Reviews
Interviews
Salaries
Users

