Software Engineer
7000+ Software Engineer Interview Questions and Answers
Q101. Matrix Multiplication Task
Given two sparse matrices MAT1
and MAT2
of integers with dimensions 'N' x 'M' and 'M' x 'P' respectively, the goal is to determine the resulting matrix produced by their multiplicatio...read more
Implement a function to multiply two sparse matrices and return the resulting matrix.
Create a function that takes two sparse matrices as input and returns the resulting matrix after multiplication.
Iterate through the non-zero elements of the matrices to perform the multiplication efficiently.
Ensure to handle the sparse nature of the matrices to optimize the multiplication process.
Consider the constraints provided to ensure the function works within the specified limits.
Test t...read more
Q102. Maximum Path Sum in a Matrix
Given an N*M matrix filled with integer numbers, determine the maximum sum that can be obtained from a path starting from any cell in the first row to any cell in the last row.
You ...read more
Find the maximum sum path in a matrix from top row to bottom row by moving down or diagonally.
Use dynamic programming to keep track of maximum sum at each cell.
At each cell, consider the maximum sum from the cell above, left diagonal, and right diagonal.
Add the current cell value to the maximum of the three possibilities.
Repeat this process for all cells in the matrix to find the maximum sum path.
Q103. Reverse the String Problem Statement
You are given a string STR
which contains alphabets, numbers, and special characters. Your task is to reverse the string.
Example:
Input:
STR = "abcde"
Output:
"edcba"
Input...read more
The task is to reverse a given string containing lowercase letters, uppercase letters, digits, and special characters.
Iterate through the string from the last character to the first character and append each character to a new string.
Alternatively, you can use built-in string reversal functions or methods available in your programming language.
To solve the follow-up question with O(1) space complexity, you can use a two-pointer approach. Initialize two pointers, one at the be...read more
Q104. What is the difference between a compiler and an interpreter?
A compiler translates the entire program into machine code before execution, while an interpreter translates and executes the program line by line.
A compiler converts the source code into an executable file, while an interpreter executes the code directly.
Compilers typically produce faster and more efficient code, while interpreters provide faster development and debugging.
Examples of compilers include GCC, Clang, and Visual C++, while examples of interpreters include Python,...read more
Q105. 2.What programming language are you proficient with..?
I am proficient in multiple programming languages including Java, Python, and C++.
Proficient in Java, Python, and C++
Experience with web development languages such as HTML, CSS, and JavaScript
Familiarity with scripting languages like Bash and PowerShell
Knowledge of database languages like SQL and NoSQL
Comfortable with object-oriented programming and design patterns
Q106. Cycle Detection in a Singly Linked List
Determine if a given singly linked list of integers forms a cycle or not.
A cycle in a linked list occurs when a node's next
points back to a previous node in the list. T...read more
Detect if a singly linked list forms a cycle by checking if a node's next points back to a previous node.
Traverse the linked list using two pointers, one moving one step at a time and the other moving two steps at a time.
If the two pointers meet at any point, it indicates the presence of a cycle in the linked list.
If one of the pointers reaches the end of the list (null), it means there is no cycle.
Share interview questions and help millions of jobseekers 🌟
Q107. K-th Smallest Element in BST
Your task is to find the ‘K-th’ smallest element in a given Binary Search Tree (BST).
Explanation:
A Binary Search Tree is a binary tree in which for each node, all elements in the ...read more
Find the K-th smallest element in a Binary Search Tree.
Implement a function to find the K-th smallest element in a BST
Traverse the BST in-order and keep track of the count of nodes visited
Return the value of the K-th smallest node
Handle cases where the K-th smallest element does not exist by returning -1
Q108. Painting Fences Problem Statement
You are given ‘N’ fences. Your task is to compute the total number of ways to paint these fences using only 2 colors, such that no more than 2 adjacent fences have the same col...read more
The task is to compute the total number of ways to paint 'N' fences using 2 colors, such that no more than 2 adjacent fences have the same color.
Use dynamic programming to solve the problem efficiently.
Consider two cases: when the last two fences have the same color and when they have different colors.
Implement a solution that calculates the number of ways to paint 'N' fences modulo 10^9 + 7.
For N = 2, there are 4 valid ways to paint the fences: [0, 1], [1, 0], [1, 1], and [0...read more
Software Engineer Jobs
Q109. Trapping Rain Water Problem Statement
You are given a long type array/list ARR
of size N
, representing an elevation map. The value ARR[i]
denotes the elevation of the ith
bar. Your task is to determine the tota...read more
Calculate the total amount of rainwater that can be trapped between given elevations in an array.
Iterate through the array and calculate the maximum height on the left and right of each bar.
Calculate the amount of water that can be trapped at each bar by taking the minimum of the maximum heights on the left and right.
Sum up the trapped water at each bar to get the total trapped water for the entire array.
Q110. Binary Tree K-Sum Paths Problem
Given a binary tree where each node contains an integer value, and a number 'K', your task is to find and output all paths in the tree where the sum of the node values equals 'K'...read more
Find all paths in a binary tree where the sum of node values equals a given number 'K'.
Traverse the binary tree in a depth-first manner while keeping track of the current path and sum of node values.
When reaching a leaf node, check if the current path sum equals 'K'. If so, add the path to the result.
Continue traversal to explore all possible paths in the tree.
Return the list of paths that satisfy the condition.
Example: For input tree 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1 and...read more
Q111. Convert Sentence Problem Statement
Convert a given string 'S' into its equivalent representation based on a mobile numeric keypad sequence. Using the keypad layout shown in the reference, output the sequence of...read more
The task is to convert a given string into its equivalent representation based on a mobile numeric keypad sequence.
Iterate through each character in the input string and find its corresponding numeric representation on the keypad
Use a mapping of characters to numbers based on the keypad layout provided in the reference
Output the sequence of numbers that corresponds to typing the input string on the keypad
Q112. Find the length of longest substring composed of same digit, within a string of digits: (was asked to complete within 10 mins) E.g. 01241XXXXX901111 Output: 4 Explanation: "1111" is the longest substring
Find the length of longest substring composed of same digit within a string of digits.
Iterate through the string and keep track of the current digit and its count
Update the maximum count whenever a new digit is encountered
Return the maximum count
Q113. Next Permutation Task
Design a function to generate the lexicographically next greater permutation of a given sequence of integers that form a permutation.
A permutation contains all integers from 1 to N exactl...read more
Design a function to generate the lexicographically next greater permutation of a given sequence of integers that form a permutation.
Understand the concept of lexicographically next permutation using algorithms like 'next_permutation' in C++ or 'permutations' in Python.
Implement a function that generates the next lexicographically greater permutation of a given sequence of integers.
Handle cases where no greater permutation exists by returning the lexicographically smallest pe...read more
Q114. What is C++ programming language?
C++ is a high-level programming language used for developing system software, application software, device drivers, and video games.
C++ is an extension of the C programming language.
It supports object-oriented programming concepts like classes, inheritance, and polymorphism.
C++ is used in developing operating systems, browsers, databases, and more.
Examples of popular software written in C++ include Adobe Photoshop, Microsoft Office, and MySQL.
C++ is known for its performance ...read more
Q115. Car Pooling Capacity Problem
You are a cab driver with a car that initially has 'C' empty seats. The car moves in a straight line towards the forward direction only. Your job is to determine if it is possible t...read more
Determine if it is possible to accommodate all passenger trips within a car's capacity without exceeding it at any point.
Iterate through each trip and keep track of the total number of passengers in the car at each point.
Check if the total number of passengers exceeds the car capacity at any point.
Return 'True' if all trips can be accommodated within the car capacity, otherwise return 'False'.
Q116. Sum Of Zeroes Coverage Calculation
You are provided with a binary matrix containing dimensions 'N * M', comprised only of 0s and 1s. Your task is to compute the aggregated sum of coverages for all the zeros in ...read more
Q117. Check if Two Trees are Mirror
Given two arbitrary binary trees, your task is to determine whether these two trees are mirrors of each other.
Explanation:
Two trees are considered mirror of each other if:
- The r...read more
Check if two binary trees are mirrors of each other based on specific criteria.
Compare the roots of both trees.
Check if the left subtree of the first tree is the mirror of the right subtree of the second tree.
Verify if the right subtree of the first tree is the mirror of the left subtree of the second tree.
Q118. Kth Smallest Element Problem Statement
You are provided with an array of integers ARR
of size N
and an integer K
. Your task is to find and return the K
-th smallest value present in the array. All elements in th...read more
Q119. Left Rotations of an Array
You are given an array consisting of N
elements and need to perform Q
queries on that array. Each query consists of an integer indicating the number of elements by which the array sho...read more
Perform left rotations on an array based on given queries.
Create a function that takes the array, number of elements, number of queries, and the queries as input.
For each query, rotate the array by the specified number of elements to the left.
Return the final array after each rotation query.
Q120. What is scheduling? List different types of scheduling
Scheduling is the process of allocating resources to tasks based on priority and availability.
Different types of scheduling include: preemptive and non-preemptive scheduling, round-robin scheduling, priority scheduling, and deadline scheduling.
Preemptive scheduling allows higher priority tasks to interrupt lower priority tasks, while non-preemptive scheduling does not.
Round-robin scheduling allocates a fixed time slice to each task in a cyclic manner.
Priority scheduling assig...read more
Q121. Armstrong Number Problem Statement
You are provided an integer 'NUM'. Determine if 'NUM' is an Armstrong number.
Explanation:
An integer 'NUM' with 'k' digits is an Armstrong number if the sum of its digits, ea...read more
Determine if a given integer is an Armstrong number by checking if the sum of its digits raised to the power of the number of digits equals the number itself.
Iterate through each digit of the number and calculate the sum of each digit raised to the power of the total number of digits.
Compare the calculated sum with the original number to determine if it's an Armstrong number.
Return 'YES' if the number is an Armstrong number, 'NO' otherwise.
Chocolate Distribution Problem
Given an array or list of integers called 'CHOCOLATES', representing the number of chocolates in each packet, your task is to distribute these packets among 'M' students so that:
Distribute chocolates among students to minimize the difference between the maximum and minimum chocolates.
Sort the array of chocolates packets.
Use sliding window technique to find the minimum difference between the maximum and minimum chocolates distributed to students.
Consider edge cases like when the number of students is equal to the number of packets.
Q123. Determine Pythagoras' Position for Parallelogram Formation
Euclid, Pythagoras, Pascal, and Monte decide to gather in a park. Initially, Pascal, Monte, and Euclid choose three different spots. Pythagoras, arrivi...read more
Calculate Pythagoras' coordinates to form a parallelogram with Euclid, Pascal, and Monte.
Calculate the midpoint of the line segment between Euclid and Monte to get the coordinates of Pythagoras.
Use the formula: x = x3 + (x2 - x1), y = y3 + (y2 - y1) to find Pythagoras' coordinates.
Ensure that the coordinates of Pythagoras satisfy the conditions for forming a parallelogram.
Q124. Minimum Number of Coins Problem
Dora, on her visit to India, decides to enjoy Indian cuisine where payments are accepted only in specific denominations. Your task is to help Dora obtain the minimum number of co...read more
Find the minimum number of coins needed to make up a given amount using specific denominations.
Iterate through the available denominations in descending order
For each denomination, calculate the maximum number of coins that can be used
Subtract the total value of coins used from the amount until amount becomes 0
Q125. Rat in a Maze Problem Statement
You need to determine all possible paths for a rat starting at position (0, 0) in a square maze to reach its destination at (N-1, N-1). The maze is represented as an N*N matrix w...read more
Find all possible paths for a rat in a maze from source to destination.
Use backtracking to explore all possible paths in the maze.
Keep track of visited cells to avoid revisiting them.
Recursively try moving in all directions (up, down, left, right) until reaching the destination.
Return a list of strings representing valid paths sorted in alphabetical order.
Q126. Sort Linked List Based on Actual Values
Given a Singly Linked List of integers that are sorted based on their absolute values, the task is to sort the linked list based on the actual values.
The absolute value ...read more
Sort a Singly Linked List based on actual values instead of absolute values.
Traverse the linked list and store the values in an array.
Sort the array based on actual values.
Update the linked list with the sorted values.
Q127. Swap Kth Elements in an Array
Given an array ARR
of size N
, perform the operation to swap the Kth element from the beginning with the Kth element from the end of the array.
Example:
Input:
N = 5, K = 2
ARR = [1,...read more
Swap Kth elements in an array with given constraints.
Create a function that takes the array, K value, and size of the array as input
Swap the Kth element from the beginning with the Kth element from the end
Handle edge cases like K being out of bounds or array size being less than 2
Q128. You have a cuboid (m*n*p) each block of the cuboid is having a metallic ball. Now we are passing X-ray from front face and getting a bool matrix1 of m*p the elements are set if there is a black spot.(as we are...
read moreYes, it is possible to get the accurate result from the given data.
The coordinates (i,j,k) where metallic balls are present can be determined by finding the set elements in both matrix1 and matrix2.
Additional data is not required as the given data is sufficient to determine the coordinates.
The coordinates can be obtained by iterating through the elements of matrix1 and matrix2 and checking for set elements.
Q129. Connecting Ropes with Minimum Cost
You are given 'N' ropes, each of varying lengths. The task is to connect all ropes into one single rope. The cost of connecting two ropes is the sum of their lengths. Your obj...read more
Given 'N' ropes of varying lengths, find the minimum cost to connect all ropes into one single rope.
Sort the lengths of ropes in ascending order.
Keep connecting the two shortest ropes at each step.
Add the cost of connecting the two ropes to the total cost.
Repeat until all ropes are connected.
Return the total cost as the minimum cost to connect all ropes.
Q130. Given the following design - A university has colleges, colleges have departments, departments have students. There are department level elections being held where students nominate themselves to stand for elec...
read moreDesign a database for a university with colleges, departments, and students. Write a query to get students with maximum votes.
Use a relational database like MySQL or PostgreSQL
Create tables for colleges, departments, students, and elections
Use foreign keys to establish relationships between tables
Add columns for election details like candidate names and vote counts
Write a query to join tables and filter for maximum votes per department
Q131. Implement Three Stacks Using a Single Array
You are given a sequence of queries for insertion and deletion operations on 3 separate stacks. Your task is to implement these three stacks using a single array whil...read more
Implement three stacks using a single array and handle insertion and deletion operations efficiently.
Create an array to store elements for all three stacks and keep track of their respective top indices.
For each query, check the type of operation (push or pop) and perform the necessary action.
Handle cases where a pop operation is attempted on an empty stack by returning -1.
Ensure the array size does not exceed the total number of queries.
Example: For input '0 1 5', pop the to...read more
Q132. Kth Largest Element Problem Statement
Ninja enjoys working with numbers, and Alice challenges him to find the Kth largest value from a given list of numbers.
Input:
The first line contains an integer 'T', repre...read more
The task is to find the Kth largest element from a given list of numbers for each test case.
Read the number of test cases 'T'
For each test case, read the number of elements 'N' and the Kth largest number to find 'K'
Sort the array in descending order and output the Kth element
Q133. Minimum Cost to Buy Oranges Problem Statement
You are given a bag of capacity 'W' kg and a list 'cost' of costs for packets of oranges with different weights. Each element at the i-th position in the list indic...read more
Find the minimum cost to purchase a specific weight of oranges given the cost of different weight packets.
Iterate through the list of costs and find the minimum cost to achieve the desired weight
Keep track of the total cost while considering available packet weights
Return -1 if it is not possible to buy exactly the desired weight
Q134. Minimum Umbrellas Problem
You are provided with ‘N’ types of umbrellas, where each umbrella type can shelter a certain number of people. Given an array UMBRELLA
that indicates the number of people each umbrella...read more
The problem involves determining the minimum number of umbrellas required to shelter a specific number of people based on the capacity of each umbrella.
Iterate through the array of umbrella capacities to find the combination that covers exactly 'M' people.
Keep track of the minimum number of umbrellas used to cover 'M' people.
If it is not possible to cover 'M' people exactly, return -1.
Example: For input [2, 3, 4] and M=5, the minimum number of umbrellas required is 2 (2-capac...read more
Q135. 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
Given an array and a target sum, find pairs of elements that add up to the target sum.
Iterate through the array and for each element, check if the complement (target sum - current element) is present in a hash set.
If the complement is found, add the pair to the result list.
Sort the pairs based on the first element, and if the first elements are equal, sort based on the second element.
Q136. Pascal's Triangle Problem Statement
You are provided with an integer N
. The objective is to return a 2-dimensional list representing Pascal’s triangle up to row N
.
A Pascal's triangle is a triangular array wher...read more
Return a 2D list representing Pascal's triangle up to row N.
Iterate through each row up to N, calculating each value based on the values from the previous row
Use a nested loop to generate the triangle efficiently
Consider edge cases like N=1 separately to return [[1]]
Remember to handle the constraints given in the problem statement
Q137. What is a deadlock? How to know if a deadlock is present?
A deadlock is a situation where two or more processes are unable to proceed because they are waiting for each other to release resources.
Deadlocks occur when two or more processes are blocked and unable to continue executing.
To detect a deadlock, look for circular wait, hold and wait, no preemption, and mutual exclusion.
Examples of deadlocks include a printer that is waiting for a user to replace the paper tray, or two trains that are stuck on the same track waiting for each ...read more
Q138. Count Ways to Reach the N-th Stair Problem Statement
You are provided with a number of stairs, and initially, you are located at the 0th stair. You need to reach the Nth stair, and you can climb one or two step...read more
The problem involves finding the number of distinct ways to climb N stairs by taking 1 or 2 steps at a time.
Use dynamic programming to solve the problem efficiently.
The number of ways to reach the Nth stair is the sum of the number of ways to reach the (N-1)th stair and the (N-2)th stair.
Handle base cases for N=0 and N=1 separately.
Consider using modulo operation to avoid overflow for large values of N.
Q139. Decimal to Octal Conversion Problem Statement
Convert a given decimal number into its equivalent octal representation.
Explanation:
The octal number system is a base-8 system, meaning each digit ranges from 0 t...read more
Convert decimal numbers to octal representation.
Iterate through each test case and use built-in functions to convert decimal to octal.
Ensure the output is in base-8 system with digits ranging from 0 to 7.
Handle constraints such as number of test cases and range of decimal numbers.
Q140. Next Greater Element Problem Statement
You are given an array arr
of length N
. For each element in the array, find the next greater element (NGE) that appears to the right. If there is no such greater element, ...read more
The task is to find the next greater element for each element in an array to its right, if no greater element exists, return -1.
Use a stack to keep track of elements for which the next greater element is not found yet.
Iterate through the array from right to left and update the stack accordingly.
Pop elements from the stack until a greater element is found or the stack is empty.
Q141. Kth Largest Element Problem
Given an array containing N
distinct positive integers and a number K
, determine the Kth largest element in the array.
Example:
Input:
N = 6, K = 3, array = [2, 1, 5, 6, 3, 8]
Output...read more
Find the Kth largest element in an array of distinct positive integers.
Sort the array in non-increasing order and return the Kth element.
Ensure all elements in the array are distinct.
Handle multiple test cases efficiently.
Q142. LCA in a Binary Search Tree
You are given a binary search tree (BST) containing N nodes. Additionally, you have references to two nodes, P and Q, within this BST.
Your task is to determine the Lowest Common Anc...read more
The task is to find the Lowest Common Ancestor (LCA) of two nodes in a Binary Search Tree (BST).
Traverse the BST from the root to find the LCA of nodes P and Q.
Compare the values of nodes P and Q with the current node's value to determine the LCA.
If both nodes are on the same side of the current node, move to that side; otherwise, the current node is the LCA.
Example: For input 2 3 and BST 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1, the LCA is node 1.
Q143. Print Series Problem Statement
Given two positive integers N
and K
, your task is to generate a series of numbers by subtracting K
from N
until the result is 0 or negative, then adding K
back until it reaches N
...read more
Generate a series of numbers by subtracting K from N until 0 or negative, then adding K back to reach N without using loops.
Create a recursive function to generate the series.
Subtract K from N until N is 0 or negative, then add K back until N is reached.
Return the series as an array of integers.
Q144. Stock Buy and Sell Problem Statement
You are given an array of integers PRICES
where PRICES[i]
represents the price of a stock on the i-th day, and an integer K
representing the number of transactions you can p...read more
Determine maximum profit with at most K transactions by buying and selling stocks on given days.
Iterate through the array of prices while keeping track of the maximum profit achievable with at most K transactions.
Use dynamic programming to store the maximum profit at each day and transaction count.
Consider buying and selling stocks at each day to calculate the maximum profit.
Return the maximum profit achievable with at most K transactions.
Q145. Triplets with Given Sum Problem
Given an array or list ARR
consisting of N
integers, your task is to identify all distinct triplets within the array that sum up to a specified number K
.
Explanation:
A triplet i...read more
The task is to identify all distinct triplets within an array that sum up to a specified number.
Iterate through the array and use nested loops to find all possible triplets.
Check if the sum of the triplet equals the specified number.
Print the valid triplets or return -1 if no triplet exists.
Q146. Word Occurrence Counting
Given a string 'S' of words, the goal is to determine the frequency of each word in the string. Consider a word as a sequence of one or more non-space characters. The string can have mu...read more
Q147. Word Wrap Problem Statement
You are tasked with arranging 'N' words of varying lengths such that each line contains at most 'M' characters, with each word separated by a space. The challenge is to minimize the ...read more
The goal is to minimize the total cost of arranging 'N' words on each line with a maximum character limit 'M'.
Calculate the cost of each line as the cube of extra space characters needed to reach 'M'.
Minimize the total cost by arranging words to fit within the character limit on each line.
Ensure each word appears fully on one line without breaking across lines.
Q148. Break The Integer Problem Statement
Given an integer N
, the task is to divide this integer into 'K' positive parts (where K ≥ 2
) such that their sum equals N
. The objective is to maximize the product of these '...read more
Given an integer N, divide it into K positive parts to maximize their product.
Divide N into K parts such that their sum equals N
Maximize the product of the K parts
Constraints: 1 ≤ T ≤ 11, 2 ≤ N ≤ 55
Example: For N = 10, parts [3, 3, 4] give product 36
Q149. Cycle Detection in Undirected Graph Problem Statement
You are provided with an undirected graph containing 'N' vertices and 'M' edges. The vertices are numbered from 1 to 'N'. Your objective is to determine whe...read more
Detect cycles in an undirected graph with given vertices and edges.
Use Depth First Search (DFS) to traverse the graph and detect cycles.
Maintain a visited array to keep track of visited vertices and a parent array to keep track of the parent of each vertex.
If while traversing, you encounter a visited vertex that is not the parent of the current vertex, then a cycle exists.
Consider edge cases like disconnected graphs and self-loops.
Q150. Interleaving Two Strings Problem Statement
You are given three strings 'A', 'B', and 'C'. Determine if 'C' is formed by interleaving 'A' and 'B'.
String 'C' is an interleaving of 'A' and 'B' if the length of 'C...read more
The problem involves determining if a string is formed by interleaving two other strings while maintaining their order.
Check if the length of string C is equal to the sum of the lengths of strings A and B.
Iterate through string C and check if all characters of A and B exist in C in the correct order.
If at any point a character in C does not match the corresponding character in A or B, return False.
Return True if all characters of A and B are found in C in the correct order.
Interview Questions of Similar Designations
Top Interview Questions for Software Engineer 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