SDE-2

200+ SDE-2 Interview Questions and Answers

Updated 7 Nov 2024
search-icon

Q1. Maximum Frequency Number Problem Statement

Given an array of integers with numbers in random order, write a program to find and return the number which appears the most frequently in the array.

If multiple elem...read more

Frequently asked in,

Q2. K Most Frequent Words Problem Statement

Given an array of N non-empty words and an integer K, return the K most frequent words sorted by their frequency from highest to lowest.

Example:

Input:
N = 6, K = 2
words...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. Reverse String Operations Problem Statement

You are provided with a string S and an array of integers A of size M. Your task is to perform M operations on the string as specified by the indices in array A.

The ...read more

Q4. 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...read more

Are these interview questions helpful?

Q5. Count Ways To Reach The N-th Stair Problem Statement

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

Frequently asked in, ,

Q6. Alien Dictionary Problem Statement

Ninja is mastering an unusual language called the Alien Language. Although it uses the same alphabets as English, the sequence of these alphabets is different. This sequence i...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'

Share interview questions and help millions of jobseekers 🌟

man-with-laptop

Q7. Find All Pairs with Given Sum

Given an integer array arr and an integer Sum, count and return the total number of pairs in the array whose elements add up to the given Sum.

Input:

The first line contains two sp...read more

Q8. Unique Element In Sorted Array

Nobita wants to impress Shizuka by correctly guessing her lucky number. Shizuka provides a sorted list where every number appears twice, except for her lucky number, which appears...read more

Q9. Pair Sum Problem Statement

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

Note:

Each pa...read more

Q10. Make Palindrome Problem Statement

You are provided with a string STR of length N comprising lowercase English alphabet letters. Your task is to determine and return the minimum number of characters that need to...read more

Q11. Ways To Make Coin Change

Given an infinite supply of coins of varying denominations, determine the total number of ways to make change for a specified value using these coins. If it's not possible to make the c...read more

Frequently asked in,

Q12. Sum Queries in a Sorted Array

Given two arrays arr and queries, your task is to determine the sum of all elements in arr that are less than or equal to each element in queries. The array arr is provided in sort...read more

Q13. Remove Consecutive Duplicates from String

Given a string STR consisting of both lower and upper case characters, your task is to remove consecutive duplicate characters and return the newly formed string.

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

Q14. Sum of Even & Odd Digits

You need to determine the sum of even digits and odd digits separately from a given integer.

Input:

The first line of input is an integer T representing the number of test cases. Each t...read more

Q15. Minimum Character Deletion Problem Statement

You have been provided a string STR. Your task is to find and return the minimum number of characters that need to be deleted from STR so that each character's frequ...read more

Q16. Binary Tree Zigzag Traversal Problem Statement

Given a Binary Tree comprised of 'N' nodes with integer values, your task is to print the zigzag traversal of the tree.

Note:

The zigzag pattern implies that the f...read more

Q17. Problem: Sort an Array of 0s, 1s, and 2s

Given an array/list ARR consisting of integers where each element is either 0, 1, or 2, your task is to sort this array in increasing order.

Input:

The input starts with...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. Palindromic Numbers Finder

Given an integer 'N', your task is to identify all palindromic numbers from 1 to 'N'. These are numbers that read the same way forwards and backwards.

Input:

The first line provides a...read more

Q19. Rearrange String Problem Statement

Given a string ‘S’, your task is to rearrange its characters so that no two adjacent characters are the same. If it's possible, return any such arrangement, otherwise return “...read more

Q20. Container with Most Water Problem Statement

Given a sequence of 'N' space-separated non-negative integers A[1], A[2], ..., A[i], ..., A[n], where each number in the sequence represents the height of a line draw...read more

Q21. Number of Subsequences with Even and Odd Sum

Your task is to determine the number of subsequences with odd sums and the number of subsequences with even sums from a given array of positive integers. As resultin...read more

Q22. Clone a Linked List with Random Pointers

You are provided with a linked list where each node has two pointers. The first pointer directs to the next node, and the second one, known as the 'random' pointer, can ...read more

Q23. ...read more

Maximum Path Sum Problem Statement

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

Example:

Input:
For the given tree:

Q24. Maximum Subarray Sum Problem Statement

Given an array ARR consisting of N integers, your goal is to determine the maximum possible sum of a non-empty contiguous subarray within this array.

Example of Subarrays:...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

Q25. Minimum Number of Jumps Problem

Given an array ARR of N integers, determine the minimum number of jumps required to reach the last index of the array (i.e., N - 1). From any index i, you can jump to an index i ...read more

Q26. Median in a Stream Problem Statement

Your task is to determine the median of integers as they are read from a data stream. The median is the middle value in the ordered list of numbers. If the list length is ev...read more

Q27. Minimum Knight Moves Problem

Given a square chessboard of size N x N, determine the minimum number of moves a knight requires to reach a target position from a starting position.

Input:

 The first line of input...read more

Q28. Number of Islands Problem Statement

You are provided with a 2-dimensional matrix having N rows and M columns, containing only 1s (land) and 0s (water). Your goal is to determine the number of islands in this ma...read more

Q29. Job Sequencing Problem Statement

You are provided with a N x 2 2-D array called Jobs consisting of N jobs. In this array, Jobs[i][0] represents the deadline of the i-th job, while Jobs[i][1] indicates the profi...read more

Q30. Shopping Options Problem Statement

Given arrays representing the costs of pants, shirts, shoes, and skirts, and a budget amount 'X', determine the total number of valid combinations that can be purchased such t...read more

Q31. Wildcard Pattern Matching Problem Statement

Implement a wildcard pattern matching algorithm to determine if a given wildcard pattern matches a text string completely.

The wildcard pattern may include the charac...read more

Q32. Connect Ropes with Minimum Cost

Given 'N' ropes, each having different lengths, your task is to connect these ropes into one single rope. The cost to connect two particular ropes is equal to the sum of their le...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.

Q33. Find Minimum in Rotated Sorted Array

You are presented with a sorted array that has been rotated an unknown number of times. The nature of this rotation is such that each element shifts to the right with the la...read more

Q34. LRU Cache Implementation Problem

Design and implement a Least Recently Used (LRU) cache data structure. This cache must support the following operations efficiently:

  • get(key): Return the value associated with ...read more

Q35. Partition Equal Subset Sum Problem

Given an array ARR consisting of 'N' positive integers, determine if it is possible to partition the array into two subsets such that the sum of the elements in both subsets i...read more

Q36. Maximum Meetings Problem Statement

Given the schedule of N meetings with their start time Start[i] and end time End[i], you need to determine which meetings can be organized in a single meeting room such that t...read more

Q37. Maximum Number With Single Swap

You are given an array of N elements that represent the digits of a number. You can perform one swap operation to exchange the values at any two indices. Your task is to determin...read more

Q38. Minimum Count of Balls in a Bag Problem Statement

You are given an integer array ARR of size N, where ARR[i] represents the number of balls in the i-th bag. Additionally, you have an integer M, which indicates ...read more

Q39. Sliding Window Maximum Problem Statement

You are given an array/list of integers with length 'N'. A sliding window of size 'K' moves from the start to the end of the array. For each of the 'N'-'K'+1 possible wi...read more

Q40. Smallest Subarray with K Distinct Elements Problem

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

Input:

The first line contains two intege...read more

Q41. Boundary Traversal of Binary Tree Problem Statement

Given a binary tree consisting of integers, your task is to provide the boundary nodes of this tree in an anti-clockwise direction, starting with the root nod...read more

Q42. Find The Sum Of The Left Leaves Problem Statement

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

Example:

Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Outpu...read more

Q43. Maximum Size Rectangle Binary Sub-Matrix with All 1s

Given a binary-valued matrix of size N x M, your task is to determine the largest area of a submatrix that contains only 1's.

Input:

The first line contains ...read more

Q44. Minimum Cost to Reduce Array Problem Statement

You are given an array ARR containing N positive integers. Your task is to reduce the size of this array to 1 by repetitively merging adjacent elements. Each time ...read more

Q45. Sort 0 and 1 Problem Statement

Given an integer array ARR of size N containing only integers 0 and 1, implement a function to sort this array. The solution should scan the array only once without using any addi...read more

Q46. Validate Binary Search Tree Problem Statement

Given a binary tree with 'N' nodes, determine if it is a Binary Search Tree (BST). Return true if the tree is a BST, otherwise return false.

Example:

Input:
1 2 3 4...read more
Q47. ...read more

Diameter of a Binary Tree Problem Statement

Given a binary tree, return the length of its diameter. The diameter of a binary tree is defined as the length of the longest path between any two nodes in the tree.

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

Q48. Find All Pairs Adding Up to Target

Given an array of integers ARR of length N and an integer Target, your task is to return all pairs of elements such that they add up to the Target.

Input:

The first line conta...read more
Frequently asked in,

Q49. Find Distinct Palindromic Substrings

Given a string 'S', identify and print all distinct palindromic substrings within it. A palindrome reads the same forwards and backwards. For example, 'bccb' is a palindrome...read more

Q50. Median of Two Sorted Arrays

Given two sorted arrays A and B of sizes N and M, find the median of the merged array formed by combining arrays A and B. If the total number of elements, N + M, is even, the median ...read more

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

Interview experiences of popular companies

3.7
 • 10.5k Interviews
4.1
 • 5.1k Interviews
4.0
 • 1.4k Interviews
3.8
 • 407 Interviews
3.9
 • 211 Interviews
3.4
 • 143 Interviews
3.5
 • 88 Interviews
3.6
 • 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

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