SDE-2

300+ SDE-2 Interview Questions and Answers

Updated 7 Nov 2024

Popular Companies

search-icon
Q1. Maximum Frequency Number

Ninja is given an array of integers that contain numbers in random order. He needs to write a program to find and return the number which occurs the maximum times in the given input. He ...read more

Frequently asked in,
Q2. K Most Frequent Words

You have been given an array/list 'WORDS' of 'N' non-empty words, and an integer 'K'. Your task is to return the 'K' most frequent words sorted by their frequency from highest to lowest.

No...read more

Ans.

Given an array of words and an integer k, return the k most frequent words sorted by frequency.

  • Use a hashmap to count the frequency of each word

  • Use a priority queue to keep track of the k most frequent words

  • Sort the priority queue based on frequency and lexicographical order

SDE-2 Interview Questions and Answers for Freshers

illustration image
Q3. Design Question

“Rent a Ride” As a customer to Rent a Ride you book a cab. We charge you as per the distance covered. We charge 8rs/km. The moment you click the button to RIDE, we search for the nearby drivers w...read more

Q4. Reverse String

You are given a string ‘S’. You are also given ‘M’ integers in an array ‘A’. You perform ‘M’ operations on this string. The operations are given in an array ‘A’ of size ‘M’.

You perform the operat...read more

Are these interview questions helpful?
Q5. Find Square root of an integer

You are given an integer ‘A’. Your task is to find the greatest non-negative integer whose square is less than or equal to ‘A’.

Square of a number is the product of the number with...read more

Q6. Count Ways To Reach The N-th Stairs

You have been given a number of stairs. Initially, you are at the 0th stair, and you need to reach the Nth stair. Each time you can either climb one step or two steps. You are...read more

Frequently asked in, ,

Share interview questions and help millions of jobseekers 🌟

man-with-laptop
Q7. Alien Dictionary

Ninja is learning a new but strange language known as Alien Language. Alien language possesses the same alphabets as of English language, but their order is different. The order of letters are g...read more

Ans.

The task is to check whether the given words are sorted lexicographically in an alien language.

  • Read the number of test cases

  • For each test case, read the number of words, the words themselves, and the order string

  • Check if the words are sorted lexicographically based on the given order string

  • Print 'YES' if the words are sorted, else print 'NO'

Q8. Print all pairs with given sum

You have been given an integer array/list(arr) and a number 'Sum'. Find and return the total number of pairs in the array/list which when added, results equal to the 'Sum'.

Note:
G...read more

SDE-2 Jobs

SDE-1 and SDE-2 (React Native) 1-6 years
Licious
3.7
Kochi
SDE-2 2-4 years
Simplilearn
3.2
₹ 7 L/yr - ₹ 12 L/yr
Bangalore / Bengaluru
SDE-2 Frontend 4-6 years
Hiverhq
4.1
Bangalore / Bengaluru
Q9. Unique Element In Sorted Array

Nobita wants to impress Shizuka by guessing her lucky number.

Shizuka gave Nobita a sorted list of ‘N’ numbers such that every number occurred twice in the list except Shizuka’s lu...read more

Q10. Pair Sum

You are given an integer array 'ARR' of size 'N' and an integer 'S'. Your task is to return the list of all pairs of elements such that each sum of elements of each pair equals 'S'.

Note:

Each pair shou...read more
Q11. Make Palindrome

You are given a string STR of length N consisting of lowercase English Alphabet letters. Your task is to return the count of minimum characters to be added at front to make the string a palindrom...read more

Q12. Ways To Make Coin Change

You are given an infinite supply of coins of each of denominations D = {D0, D1, D2, D3, ...... Dn-1}. You need to figure out the total number of ways W, in which you can make a change fo...read more

Frequently asked in,
Q13. Search In The Array

You are given two arrays ‘arr’ of size ‘n’ and ‘queries’ of size ‘q’. For each element ‘q’ in the array 'queries', your task is to find the sum of all elements in the array ‘arr’ which are le...read more

Q14. Remove Consecutive Duplicates From String

You are given a string 'STR' consisting of lower and upper case characters. You need to remove the consecutive duplicates characters, and return the new string.

Exampl...read more
Ans.

The task is to remove consecutive duplicate characters from a given string and return the new string.

  • Iterate through the characters of the string

  • Compare each character with the next character

  • If they are the same, skip the next character

  • If they are different, append the current character to the new string

Q15. Sum of even & odd

Ninja got a test problem in which he was given an integer and he had to find the sum of even digits and odd digits separately.

Ninja is very weak in studies and didn’t know much about odd and e...read more

Q16. Minimum Character Deletion

You are given a string ‘STR’. You need to find and return the minimum number of characters to be deleted from ‘STR’ so that the frequency of each character in the string becomes unique...read more

Q17. Sort An Array of 0s, 1s and 2s

You have been given an array/list ARR consisting of ‘N’ elements. Each element in the array is either 0, 1 or 2.

Now, your task is to sort this array/list in increasing order. For ...read more

Ans.

The task is to sort an array of 0s, 1s, and 2s in increasing order.

  • Use a three-pointer approach to partition the array into three sections: 0s, 1s, and 2s.

  • Initialize three pointers: low, mid, and high. low points to the start of the array, mid points to the current element being processed, and high points to the end of the array.

  • While mid <= high, if ARR[mid] is 0, swap ARR[low] and ARR[mid], increment low and mid. If ARR[mid] is 1, increment mid. If ARR[mid] is 2, swap ARR[m...read more

Q18. Binary Tree Zigzag Traversal

You have been given a Binary Tree of 'N' nodes, where the nodes have integer values. Your task is to print the zigzag traversal of the given tree.

Note:
In zigzag order, level 1 is p...read more
Q19. Find Palindromes

You are given an integer ‘N’. Your task is to find all palindromic numbers from 1 to ‘N’.

Palindromic integers are those integers that read the same backward or forwards.

Note:
Order of numbers ...read more
Q20. Rearrange string

You are given a string “S”. Your task is to rearrange the characters of a string “S”, such that it does not contain any two adjacent characters which are the same.

If it is possible to rearrange...read more

Q21. Container with most water

Given a sequence of ‘N’ space-separated non-negative integers A[1],A[2],A[3],......A[i]…...A[n]. Where each number of the sequence represents the height of the line drawn at point 'i'. ...read more

Q22. Number of Subsequences with Even and Odd Sum

You are given an array consisting of 'N' positive integers, and your task is to find the number of subsequences with odd sum and the number of subsequences with even ...read more

Q23. Puzzle

You are trying to cook an egg for exactly fifteen minutes, but instead of a timer, you are given two ropes which burn for exactly 1 hour each. The ropes, however, are of uneven densities – i e , half the ...read more

Q24. Clone a Linked List with random pointers

Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node o...read more

Q25. Maximum Path Sum

You are given an n-ary tree consisting of ‘N’ nodes. Your task is to return the maximum sum of the path from the root to the leaf node.

For example:
For the given tree: 

The path 1 -> 3 -> 7 pr...read more
Q26. Maximum Subarray Sum

You are given an array/list ARR consisting of N integers. Your task is to find the maximum possible sum of a non-empty subarray(contagious) of this array.

Note: An array C is a subarray of a...read more

Ans.

The task is to find the maximum possible sum of a non-empty subarray of an array.

  • Iterate through the array and keep track of the maximum sum encountered so far

  • If the current element is greater than the sum so far, start a new subarray

  • If the current element plus the sum so far is greater than the maximum sum, update the maximum sum

  • Return the maximum sum

Frequently asked in,
Q27. Minimum number of jumps to reach end

You have been given an array 'ARR' of ‘N’ integers. You have to find the minimum number of jumps needed to reach the last index of the array i.e ‘N - 1’ if at any index ‘i’ w...read more

Q28. Find Number Of Islands

You are given a 2-dimensional array/list having N rows and M columns, which is filled with ones(1) and zeroes(0). 1 signifies land, and 0 signifies water.

A cell is said to be connected to...read more

Q29. Median in a stream

Given that integers are read from a data stream. Your task is to find the median of the elements read so far.

Median is the middle value in an ordered integer list. If the size of the list is ...read more

Q30. Minimum steps taken by knight to reach target

You are given a 2D matrix of size N x N which is nothing but a square chessboard. Given the starting and target positions of the knight, you are supposed to return t...read more

Q31. Job Sequencing Problem

You are given a N x 2 2-D array 'Jobs' of 'N' jobs where Jobs[i][0] denote the deadline of i-th job and Jobs[i][1] denotes the profit associated with i-th job.

You will make a certain prof...read more

Q32. Shopping Options

You are given the list of costs of pants in an array “pants”, shirts in an array “shirts”, shoes in an array “shoes”, and skirts in an array “skirts”. You are also given a budget amount ‘X’ to s...read more

Q33. Wildcard Pattern Matching

Given a text and a wildcard pattern of size N and M respectively, implement a wildcard pattern matching algorithm that finds if the wildcard pattern is matched with the text. The matchi...read more

Q34. Connect N Ropes With Minimum Cost

You have been given 'N' ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to con...read more

Ans.

The task is to connect N ropes into one rope with minimum cost.

  • Sort the array of rope lengths in ascending order.

  • Initialize a variable to keep track of the total cost.

  • While there are more than one rope, take the two shortest ropes and connect them.

  • Add the cost of connecting the two ropes to the total cost.

  • Replace the two shortest ropes with the connected rope.

  • Repeat the above steps until only one rope remains.

  • Return the total cost.

Q35. Find the minimum element in a sorted and rotated array

You're given a sorted array that has now been rotated 'K' times, which is unknown to you. Rotation here means that every element is shifted from its positio...read more

Q36. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache to support the following operations:

1. get(key) - Return the value of the key if the key exists in the cache, otherwise return...read more
Q37. Partition Equal Subset Sum

You are given an array 'ARR' of 'N' positive integers. Your task is to find if we can partition the given array into two subsets such that the sum of elements in both subsets is equal....read more

Q38. Maximum In Sliding Windows Of Size K

Given an array/list of integers of length ‘N’, there is a sliding window of size ‘K’ which moves from the beginning of the array, to the very end. You can only see the ‘K’ nu...read more

Q39. Maximum meetings

You are given the schedule of N meetings with their start time Start[i] and end time End[i]. But you have only 1 meeting room. So, you need to tell the meeting numbers you can organize in the gi...read more

Q40. Maximum Number

You are given an array of N elements. This array represents the digits of a number. In an operation, you can swap the value at any two indices. Your task is to find the maximum number by using ope...read more

Q41. Minimum count of balls in a bag

You are given an integer array ‘ARR’ of size ‘N’, where ‘ARR[i]’ denotes the number of balls in the ‘i-th’ bag. You are also given an integer ‘M’, denoting the maximum number of o...read more

Q42. Smallest Subarray with K Distinct Elements

Given an array 'A' consisting of 'N' integers, find the smallest subarray of 'A' containing exactly 'K' distinct integers.

Note :
If more than one such contiguous suba...read more
Q43. Basic Puzzles

A noticeable aspect in this puzzles is the fact that there’s a circular misplacement, which implies if apple is wrongly labelled as Apple, Apple can’t be labelled as Orange, i.e., it has to be labe...read more

Q44. Find The Sum Of The Left Leaves

Given a binary tree with ‘root’. Your task is to find the sum of all the left leaf nodes.

Properties of leaf:-

In a binary tree, a leaf is a node such that it does not have any ch...read more

Q45. Boundary Traversal of Binary Tree

You have been given a binary tree of integers. Your task is to print the boundary nodes of this binary tree in Anti-Clockwise direction starting from the root node.

NOTE:
The bo...read more
Q46. Maximum size rectangle binary sub-matrix with all 1s

You are given an 'N' * 'M' sized binary-valued matrix 'MAT, where 'N' is the number of rows and 'M' is the number of columns. You need to return the maximum s...read more

Q47. Minimum cost of reducing Array by merging any adjacent elements repetitively

You are given an array 'ARR' consisting of 'N' positive integers, and you need to reduce the size of the array to 1 by performing an o...read more

Q48. Sort 0 1

You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don'...read more

Q49. Validate Binary Search Tree

Given a binary tree with N number of nodes, check if that input tree is BST (Binary Search Tree) or not. If yes, return true, return false otherwise.

A binary search tree (BST) is a b...read more

Q50. Diameter Of Binary Tree

You are given a Binary Tree. You are supposed to return the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two end nodes i...read more

Ans.

The diameter of a binary tree is the length of the longest path between any two end nodes in the tree.

  • The diameter of a binary tree can be calculated by finding the maximum of the following three values: 1) the diameter of the left subtree, 2) the diameter of the right subtree, and 3) the longest path that passes through the root node.

  • To find the diameter of a binary tree, we can use a recursive approach where we calculate the height of each node and update the diameter at ea...read more

1
2
3
4
5
6
7
Next
Interview Tips & Stories
Ace your next interview with expert advice and inspiring stories

Interview experiences of popular companies

3.7
 • 10k Interviews
4.1
 • 4.9k Interviews
4.0
 • 1.3k Interviews
3.9
 • 390 Interviews
3.9
 • 201 Interviews
3.8
 • 198 Interviews
3.4
 • 140 Interviews
3.7
 • 77 Interviews
View all

Calculate your in-hand salary

Confused about how your in-hand salary is calculated? Enter your annual salary (CTC) and get your in-hand salary

SDE-2 Interview Questions
Share an Interview
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter