SDE-2
300+ SDE-2 Interview Questions and Answers
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
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
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
“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
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
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
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
Share interview questions and help millions of jobseekers 🌟
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
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'
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Top Interview Questions for SDE-2 Related Skills
Interview experiences of popular companies
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
Reviews
Interviews
Salaries
Users/Month