Top 250 Algorithms Interview Questions and Answers

Updated 13 Jul 2025

Asked in Capgemini

2d ago

Q. One question of sorting for a list of people belonging to different cities and states.

Ans.

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

5d ago

Q. Find the second largest element in an array with the least time complexity.

Ans.

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

6d ago

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

Ans.

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

6d ago
Q. The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.
Ans.

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

Are these interview questions helpful?

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

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

6d ago

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

Ans.

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

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

Asked in Amazon

2d ago

Q. Given a sorted array that has been rotated, search for an element in the array.

Ans.

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

3d ago
Q. You are given a string. What is the minimum number of characters that need to be inserted to convert it into a palindrome?
Ans.

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

2d ago

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

Ans.

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

6d ago

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 kth smallest eleme...read more

Ans.

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

Red Hat India Pvt Ltd logo
Senior Principal Data Scientist 10-15 years
Red Hat India Pvt Ltd
4.3
Pune
Red Hat India Pvt Ltd logo
Principal Data Scientist 7-12 years
Red Hat India Pvt Ltd
4.3
Pune
Sodexo Food Solutions India Pvt. Ltd.ces logo
FTM Professional 3-5 years
Sodexo Food Solutions India Pvt. Ltd.ces
4.1
Krishnagiri

Asked in TCS

3d ago

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

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

2d ago

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

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

5d ago

Q. What are the differences between Merge Sort and Heap Sort?

Ans.

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

3d ago

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

Ans.

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

2d ago

Q. How would you optimize code for generating prime numbers?

Ans.

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

1d ago

Q. Write code to generate the Fibonacci sequence up to 10.

Ans.

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

3d ago

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.

Ans.

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

3d ago

Q. How would you optimize restaurant listings on Swiggy?

Ans.

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

3d ago

Q. Explain the logic of quick sort.

Ans.

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

5d ago

Q. Write a recursive function to reverse a number.

Ans.

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

4d ago

Q. Write code to determine if two words are anagrams.

Ans.

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

2d ago

Q. What are the types of ML algorithms? Give an example of each.

Ans.

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

4d ago

Q. Write a program to find even and odd numbers using lambda expressions.

Ans.

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

2d ago

Q. How is RPN calculated?

Ans.

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

1d ago

Q. What is the difference between BFS and DFS?

Ans.

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

3d ago

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

Ans.

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

4d ago

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

Ans.

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

1d ago

Q. Given an integer matrix, find the length of the longest increasing path in the matrix.

Ans.

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

3d ago
Q. Given an array of size n, how would you find the smallest subarray that contains k distinct values?
Ans.

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

1d ago

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

Ans.

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

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
 • 895 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