Software Developer
7000+ Software Developer Interview Questions and Answers
Q1. Maximum Subarray Sum Problem Statement
Given an array of integers, determine the maximum possible sum of any contiguous subarray within the array.
Example:
Input:
array = [34, -50, 42, 14, -5, 86]
Output:
137
E...read more
Find the maximum sum of any contiguous subarray within an array of integers.
Iterate through the array and keep track of the maximum sum of subarrays encountered so far.
At each index, decide whether to include the current element in the subarray or start a new subarray.
Use Kadane's algorithm to efficiently find the maximum subarray sum in O(N) time complexity.
Q2. Minimum Number of Platforms Needed Problem Statement
You are given the arrival and departure times of N trains at a railway station for a particular day. Your task is to determine the minimum number of platform...read more
Determine the minimum number of platforms needed at a railway station based on arrival and departure times of trains.
Sort the arrival and departure times in ascending order.
Use two pointers to keep track of overlapping schedules.
Increment the platform count when a new train arrives before the previous one departs.
Return the maximum platform count needed.
Software Developer Interview Questions and Answers for Freshers
Q3. Merge Two Sorted Arrays Problem Statement
Given two sorted integer arrays ARR1
and ARR2
of size M and N, respectively, merge them into ARR1
as one sorted array. Assume that ARR1
has a size of M + N to hold all ...read more
Merge two sorted arrays into one sorted array in place.
Iterate from the end of both arrays and compare elements to merge in place
Use two pointers to keep track of the current position in each array
Handle cases where one array is fully merged before the other
Q4. Nth Fibonacci Number Problem Statement
Calculate the Nth term in the Fibonacci sequence, where the sequence is defined as follows: F(n) = F(n-1) + F(n-2)
, with initial conditions F(1) = F(2) = 1
.
Input:
The inp...read more
Calculate the Nth Fibonacci number efficiently using dynamic programming.
Use dynamic programming to store and reuse previously calculated Fibonacci numbers.
Start with base cases F(1) and F(2) as 1, then calculate subsequent Fibonacci numbers.
Optimize the solution to avoid redundant calculations by storing intermediate results.
Time complexity can be reduced to O(N) using dynamic programming.
Example: For N = 5, the 5th Fibonacci number is 5.
Q5. Find Duplicate in Array Problem Statement
You are provided with an array of integers 'ARR' consisting of 'N' elements. Each integer is within the range [1, N-1], and the array contains exactly one duplicated el...read more
The task is to find the duplicate element in an array of integers.
Iterate through the array and keep track of the frequency of each element using a hash map.
Return the element with a frequency greater than 1.
Alternatively, sort the array and check for adjacent elements with the same value.
Q6. Array Intersection Problem Statement
Given two integer arrays/ lists ARR1
and ARR2
of sizes N
and M
respectively, you are required to determine their intersection. An intersection is defined as the set of commo...read more
The task is to find the intersection of two integer arrays/lists.
Read the number of test cases
For each test case, read the size and elements of the first array/list
Read the size and elements of the second array/list
Find the intersection of the two arrays/lists
Print the intersection elements in the order they appear in the first array/list
Share interview questions and help millions of jobseekers 🌟
Q7. Validate Binary Tree Nodes Problem
You are provided with 'N' binary tree nodes labeled from 0 to N-1, where node i has two potential children, recorded in the LEFT_CHILD[i]
and RIGHT_CHILD[i]
arrays. Determine ...read more
The task is to determine if the given binary tree nodes form exactly one valid binary tree.
Check if there is only one root node (a node with no parent)
Check if each node has at most one parent
Check if there are no cycles in the tree
Check if all nodes are connected and form a single tree
Q8. Crazy Numbers Pattern Challenge
Ninja enjoys arranging numbers in a sequence. He plans to arrange them in 'N' rows such that:
- The first row contains 1 number.
- The second row contains 2 numbers.
- The third row c...read more
Arrange numbers in a sequence in 'N' rows with increasing order and repeating after 9.
Iterate through each test case and for each row, print numbers in increasing order with a loop.
Keep track of the current number to print and reset to 1 after reaching 9.
Handle formatting to align numbers correctly in each row.
Ensure to print the correct number of rows based on the input 'N'.
Software Developer Jobs
Q9. Find Terms of Series Problem
Ayush is tasked with determining the first 'X' terms of the series defined by 3 * N + 2, ensuring that no term is a multiple of 4.
Input:
The first line contains a single integer 'T...read more
Generate the first 'X' terms of a series 3 * N + 2, excluding multiples of 4.
Iterate through numbers starting from 1 and check if 3 * N + 2 is not a multiple of 4.
Keep track of the count of terms generated and stop when 'X' terms are found.
Return the list of 'X' terms that meet the criteria.
Example: For X = 4, the output should be [5, 11, 14, 17].
Q10. Maximum Coins Collection Problem
Imagine a two-dimensional grid with 'N' rows and 'M' columns, where each cell contains a certain number of coins. Alice and Bob want to maximize the total number of coins they c...read more
Given a matrix of coins, Alice and Bob have to collect maximum coins with certain conditions.
Alice starts from top left corner and Bob starts from top right corner
They can move to (i+1, j+1) or (i+1, j-1) or (i+1, j)
They have to collect all the coins that are present at a cell
If Alice has already collected coins of a cell, then Bob gets no coins if goes through that cell again
Q11. Connect Ropes Problem Statement
Given a number of ropes denoted as 'N' and an array containing the lengths of these ropes, your task is to connect the ropes into one single rope. The cost to connect two ropes i...read more
The task is to find the minimum cost required to connect all the ropes by summing their lengths.
Iterate through the ropes and connect the two shortest ropes at each step to minimize cost
Use a priority queue to efficiently find the shortest ropes
Keep track of the total cost as you connect the ropes
Example: For input [4, 3, 2, 6], connect 2 and 3 (cost 5), then connect 4 and 5 (cost 9), then connect 9 and 6 (cost 15) for a total cost of 29
Q12. Form a Triangle Problem Statement
You are given an array of integers ARR
with a length of N
. Your task is to determine whether it's possible to construct at least one non-degenerate triangle using the values fr...read more
Determine if it's possible to form a non-degenerate triangle using array elements as sides.
Check if the sum of any two sides is greater than the third side to form a triangle.
If any such combination exists, return 'YES'; otherwise, return 'NO'.
Example: For input [3, 4, 5], sum of 3 + 4 > 5, so 'YES'. For [1, 10, 12, 30], no such combination exists, so 'NO'.
Q13. String Compression Problem Statement
Ninja needs to perform basic string compression. For any character that repeats consecutively more than once, replace the repeated sequence with the character followed by th...read more
Implement a function to compress a string by replacing consecutive characters with the character followed by the count of repetitions.
Iterate through the input string and keep track of consecutive characters and their counts.
Replace consecutive characters with the character followed by the count of repetitions if count is greater than 1.
Return the compressed string as output.
Q14. Fenwick Tree Problem Statement
You are provided with an array/list ARR
consisting of 'N' integers, along with 'Q' queries. Each query can be of one of the following two types:
- Type 1 (Range Sum): Given two int...read more
Implement Fenwick Tree to handle range sum and point update queries on an array.
Implement Fenwick Tree data structure to efficiently handle range sum and point update queries.
For range sum queries, use prefix sum technique to calculate the sum of elements in the given range.
For point update queries, update the Fenwick Tree structure accordingly.
Handle each query type separately and output the required results.
Ensure to follow the constraints provided in the problem statement.
Q15. Minimum Operations to Make Strings Equal
Given two strings, A
and B
, consisting of lowercase English letters, determine the minimum number of pre-processing moves required to make string A
equal to string B
usi...read more
Determine the minimum number of pre-processing moves required to make two strings equal by swapping characters and replacing characters in one string.
Iterate through both strings simultaneously and count the number of characters that need to be swapped.
Consider all possible swaps and replacements to find the minimum number of pre-processing moves.
If the lengths of the strings are different, it's impossible to make them equal.
Handle edge cases like empty strings or strings wit...read more
Q16. 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 find all distinct triplets in an array that sum up to a specified number.
Iterate through all possible triplets using three nested loops.
Check if the sum of the triplet equals the target sum.
Print the triplet if found, else print -1.
Handle duplicate triplets by skipping duplicates during iteration.
Q17. 1. what is the difference between exception and error. How did u solve the errors in the code deployment?
Exception is a runtime error that can be handled, while error is a severe issue that cannot be handled.
Exceptions are caused by the program logic, while errors are caused by external factors like hardware failure or network issues.
Exceptions can be caught and handled using try-catch blocks, while errors require fixing the underlying issue.
Example of exception: NullPointerException. Example of error: Out of Memory Error.
To solve errors in code deployment, identify the root cau...read more
Q18. Reverse Array Elements
Given an array containing 'N' elements, the task is to reverse the order of all array elements and display the reversed array.
Explanation:
The elements of the given array need to be rear...read more
Reverse the order of elements in an array and display the reversed array.
Iterate through the array from both ends and swap the elements until reaching the middle.
Use a temporary variable to store the element being swapped.
Print the reversed array after swapping all elements.
Q19. Arithmetic Subarrays Problem Statement
You are provided with an array A
of length N
. Your task is to determine the number of arithmetic subarrays present within the array A
.
Explanation:
An arithmetic subarray ...read more
Count the number of arithmetic subarrays in an array.
An arithmetic subarray has 3 or more elements with the same difference between consecutive elements.
Loop through the array and check for all possible subarrays with 3 or more elements.
If the difference between consecutive elements is the same, increment the count.
Return the count for each test case.
Q20. 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
Implement a function to find all palindromic numbers from 1 to N.
Iterate from 1 to N and check if each number is a palindrome
Use string manipulation to check for palindromes
Consider edge cases like single-digit numbers and 11
Q21. 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
Implement a wildcard pattern matching algorithm to determine if a given wildcard pattern matches a text string completely.
Create a recursive function to match the pattern with the text character by character
Handle cases for '?' and '*' characters in the pattern
Keep track of the current position in both pattern and text strings
Return 'True' if the pattern matches the text completely, otherwise return 'False'
Q22. 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 so...read more
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 by 10^9 + 7 to avoid overflow issues.
Q23. 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
The question asks to find the total amount of rainwater that can be trapped in the given elevation map.
Iterate through the array and find the maximum height on the left and right side of each bar.
Calculate the amount of water that can be trapped on each bar by subtracting its height from the minimum of the maximum heights on both sides.
Sum up the amount of water trapped on each bar to get the total amount of rainwater trapped.
Q24. Check Permutation Problem
Determine if two given strings, str1
and str2
, are permutations of each other.
Explanation:
Two strings are permutations of each other if the characters of one string can be rearranged...read more
Check if two strings are permutations of each other.
Create character frequency maps for both strings.
Compare the frequency maps to check if they are permutations.
Handle edge cases like empty strings or strings of different lengths.
Q25. 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
The task is to determine if a given singly linked list forms a cycle or not.
A cycle occurs when a node's next points back to a previous node in the list.
To solve this problem, we can use the Floyd's Cycle-Finding Algorithm.
The algorithm uses two pointers, one moving at a normal pace and the other moving twice as fast.
If there is a cycle, the fast pointer will eventually catch up to the slow pointer.
If the fast pointer reaches the end of the list (i.e., it encounters a null no...read more
Q26. Flip Bits Problem Explanation
Given an array of integers ARR
of size N, consisting of 0s and 1s, you need to select a sub-array and flip its bits. Your task is to return the maximum count of 1s that can be obta...read more
Q27. Search In Rotated Sorted Array Problem Statement
Given a sorted array of distinct integers that has been rotated clockwise by an unknown amount, you need to search for a specified integer in the array. For each...read more
Implement a search function to find a specified integer in a rotated sorted array.
Implement a binary search algorithm to find the target integer in the rotated sorted array.
Handle the cases where the target integer may lie in the left or right half of the array after rotation.
Keep track of the mid element and adjust the search space based on the rotation.
Return the index of the target integer if found, else return -1.
Q28. Intersection of Two Unsorted Arrays Problem Statement
Given two integer arrays ARR1
and ARR2
of sizes 'N' and 'M' respectively, find the intersection of these arrays. The intersection is defined as the set of e...read more
Find the intersection of two unsorted arrays while maintaining the order of elements from the first array.
Iterate through the elements of the first array and check if they exist in the second array.
Use a hash set to keep track of elements already seen in the second array for efficient lookup.
Maintain the order of elements from the first array while finding the intersection.
Handle duplicate elements in both arrays appropriately.
Output the intersection elements in the order the...read more
Q29. Balanced Binary Trees Problem Statement
You are provided with an integer 'H'. Your task is to determine and print the maximum number of balanced binary trees possible with height 'H'.
A balanced binary tree is ...read more
The maximum number of balanced binary trees possible with a given height is to be counted and printed.
A balanced binary tree is one in which the difference between the left and right subtree heights is less than or equal to 1.
The number of balanced binary trees can be calculated using dynamic programming.
The number of balanced binary trees with height 'H' can be obtained by summing the product of the number of balanced binary trees for each possible left and right subtree hei...read more
Q30. Greatest Common Divisor Problem Statement
You are tasked with finding the greatest common divisor (GCD) of two given numbers 'X' and 'Y'. The GCD is defined as the largest integer that divides both of the given...read more
Find the greatest common divisor (GCD) of two given numbers 'X' and 'Y'.
Iterate from 1 to the minimum of X and Y, check if both X and Y are divisible by the current number, update GCD if true
Use Euclidean algorithm to find GCD: GCD(X, Y) = GCD(Y, X % Y)
If one of the numbers is 0, the other number is the GCD
Handle edge cases like when one of the numbers is 0 or negative
Q31. Order of People Heights Problem Statement
Consider 'N' individuals numbered from 0 to N-1 standing in a queue. You are provided with two arrays: Height
and Infront
, each consisting of 'N' non-negative integers....read more
The task is to find the actual order of people in a queue based on their heights and the number of taller people in front of them.
Iterate through the given arrays and create a list of tuples containing the height and number of taller people for each person.
Sort the list of tuples in descending order of height and ascending order of the number of taller people.
Create an empty result list and insert each tuple into the result list at the index specified by the number of taller ...read more
Q32. Sort 0s, 1s, and 2s Problem Statement
You are provided with an integer array/list ARR
of size 'N' which consists solely of 0s, 1s, and 2s. Your task is to write a solution to sort this array/list.
Input:
The fi...read more
Sort an array of 0s, 1s, and 2s in linear time with a single scan approach.
Use three pointers to keep track of the positions of 0s, 1s, and 2s in the array.
Iterate through the array once and swap elements based on their values and the pointers.
After the single scan, the array will be sorted in place with 0s, 1s, and 2s in order.
Q33. Triple Sum Problem Statement
Bojack wants to gift Todd a binary tree with N
nodes for his birthday. However, the tree is too large, so he decides to select exactly three nodes such that their sum equals a given...read more
The task is to determine if it is possible to select three nodes from a binary tree such that their sum equals a given value.
Traverse the binary tree and store all the node values in an array
Use three nested loops to iterate through all possible combinations of three nodes
Check if the sum of the three nodes equals the given value
If a valid combination is found, return True
If no valid combination is found, return False
Q34. Find K'th Character of Decrypted String
You are given an encrypted string where repeated substrings are represented by the substring followed by its count. Your task is to find the K'th character of the decrypt...read more
Given an encrypted string with repeated substrings represented by counts, find the K'th character of the decrypted string.
Parse the encrypted string to extract substrings and their counts
Iterate through the substrings and counts to build the decrypted string
Track the position in the decrypted string to find the K'th character
Q35. Maximum Subarray Problem Statement
Ninja has been given an array, and he wants to find a subarray such that the sum of all elements in the subarray is maximum.
A subarray 'A' is considered greater than a subarr...read more
The task is to find the subarray with the maximum sum in a given array.
Iterate through the array and keep track of the current sum and maximum sum seen so far.
If the current sum becomes negative, reset it to 0 as it won't contribute to the maximum sum.
Compare the maximum sum with the sum of the current subarray to update the result.
Handle cases where all elements are negative by returning the maximum element in the array.
Consider edge cases like empty array or array with only...read more
Q36. Missing Number Problem Statement
You are provided with an array named BINARYNUMS
consisting of N
unique strings. Each string represents an integer in binary, covering every integer from 0 to N except for one. Y...read more
Find the missing integer in binary form from an array of unique binary strings.
Iterate through the array of binary strings and convert each string to its decimal equivalent.
Calculate the sum of all integers from 0 to N using the formula (N * (N + 1)) / 2.
Subtract the sum of the integers represented by the binary strings from the total sum to find the missing integer.
Convert the missing integer to binary form and return it as a string without leading zeros.
Q37. Balanced Sequence After Replacement
Given a string of length 'N' containing only the characters: '[', '{', '(', ')', '}', ']'. At certain places, the character 'X' appears in place of any bracket. Your goal is ...read more
Determine if a valid balanced sequence can be achieved by replacing 'X's with suitable brackets.
Iterate through the string and keep track of the count of opening and closing brackets.
If at any point the count of closing brackets exceeds the count of opening brackets, return False.
If all 'X's can be replaced to form a valid balanced sequence, return True.
Q38. Rearrange Array: Move Negative Numbers to the Beginning
Given an array ARR
consisting of N
integers, rearrange the elements such that all negative numbers are located before all positive numbers. The order of e...read more
Yes, this can be achieved by using a two-pointer approach to rearrange the array in-place with O(1) auxiliary space.
Use two pointers, one starting from the beginning and one from the end of the array.
Move the left pointer to the right until it encounters a positive number, and the right pointer to the left until it encounters a negative number.
Swap the elements at the left and right pointers, and continue this process until the pointers meet.
The resulting array will have all ...read more
Q39. Beautiful String Problem Statement
Given a binary string STR
containing either '0' or '1', determine the minimum number of operations needed to make it beautiful. A binary string is called beautiful if it conta...read more
The problem involves determining the minimum number of operations needed to make a binary string beautiful by ensuring it contains alternating 0s and 1s.
Iterate through the binary string and count the number of operations needed to make it beautiful by flipping the bits as required.
Keep track of the current bit and compare it with the next bit to determine if a flip operation is needed.
Return the total number of operations needed for each test case.
Q40. Wave Array Sorting Problem
Your goal is to sort a given unsorted array ARR
such that it forms a wave array.
Explanation:
After sorting, the array should satisfy the wave pattern conditions:
ARR[0] >= ARR[1]
AR...read more
The task is to sort an array in a wave form, where each element is greater than or equal to its adjacent elements.
Iterate through the array and swap adjacent elements if they do not follow the wave pattern
Start from the second element and compare it with the previous element, swap if necessary
Continue this process until the end of the array
Repeat the process for the remaining elements
Return the sorted wave array
Q41. Distance To Nearest 1 in a Binary Matrix Problem
Given a binary matrix MAT
containing only 0s and 1s of size N x M, find the distance of the nearest cell containing 1 for each cell in the matrix.
The distance i...read more
Given a binary matrix, find the distance of the nearest cell having 1 in the matrix for each cell.
Use BFS to traverse the matrix and find the nearest cell having 1 for each cell.
Initialize the output matrix with maximum possible distance.
If the current cell has 1, distance is 0, else update distance based on the nearest cell having 1.
Q42. Triangle of Numbers Pattern
Ninja is tasked with printing a triangle pattern based on a given number 'N' for any test case.
Example:
Input:
N = 4
Output:
1
232
34545
4567654
Explanation:
The pattern comprises n...read more
Print a triangle pattern of numbers based on given input 'N'.
Iterate through each row and print the numbers in a centered and incremented manner
Use nested loops to handle the alignment and incrementing of numbers
Ensure to properly format the output with spaces for alignment
Q43. Alien Dictionary Problem Statement
You are provided with a sorted dictionary (by lexical order) in an alien language. Your task is to determine the character order of the alien language from this dictionary. Th...read more
Q44. Buses Origin Problem Statement
You have been provided with an array where each element specifies the number of buses that can be boarded at each respective bus stop. Buses will only stop at locations that are m...read more
Given an array representing number of buses at each bus stop, determine how many buses originate from each stop.
Iterate through the array and for each element, increment the count of buses originating from the corresponding bus stop.
Use an array to store the count of buses originating from each stop.
Remember to consider the constraint of 1-based indexing for bus stops.
Q45. 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 ce...read more
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 checking arr[i] = i and arr[i] = -1 respectively.
Output the length of the largest cycle found or -1 if no cycles exist.
Q46. There is a 12 km road and a contractor who is in-charge of repairing it. Contractor updates you about the work which is done in patches. Like “Road between 3.2 km to 7.9 km repaired ”, “Road between 1.21 km to...
read moreThe longest continuous patch of a road repaired by a contractor is determined.
Iterate through the updates and keep track of the start and end points of each patch
Calculate the length of each patch and update the longest patch if necessary
Return the start and end points of the longest patch
Q47. Aggressive Cows Problem Statement
Given an array representing positions of stalls and an integer ‘K’ representing the number of aggressive cows, determine the largest minimum distance between any two cows when ...read more
The problem is to assign aggressive cows to stalls in a way that maximizes the minimum distance between any two cows.
Sort the array of stall positions in ascending order.
Use binary search to find the largest minimum distance between cows.
Check if it is possible to assign cows with this minimum distance by iterating through the sorted array.
If it is possible, update the maximum distance and continue binary search for a larger minimum distance.
If it is not possible, continue bi...read more
Q48. Fire in the Cells Problem Statement
Given a matrix MAT
of size N * M
, where each cell is either on fire or safe, determine if a person can reach an escape cell without being burnt. The person starts from a give...read more
Determine if a person can reach an escape cell without encountering fire in a matrix.
Check if the person can reach an escape cell without encountering fire by simulating the movement of the person and fire spread.
If the person can reach an escape cell, return the time taken; otherwise, return -1.
Consider the constraints and edge cases while implementing the solution.
Q49. Maximum Equal Elements After K Operations
You are provided with an array/list of integers named 'ARR' of size 'N' and an integer 'K'. Your task is to determine the maximum number of elements that can be made eq...read more
Determine the maximum number of elements that can be made equal by performing at most K operations on an array of integers.
Sort the array in non-decreasing order to easily identify the elements that need to be increased
Calculate the difference between adjacent elements to determine the number of operations needed to make them equal
Keep track of the total number of operations used and the maximum number of elements that can be made equal
Q50. Permutation in String Problem Statement
Determine if the string str2
contains a permutation of the string str1
as one of its substrings.
Input:
The first line contains an integer 'T', representing the number of...read more
Check if a permutation of one string is a substring of another string.
Create a frequency map of characters in str1.
Iterate through str2 with a window of size N and check if the frequency map matches.
Return 'Yes' if a permutation is found, 'No' otherwise.
Interview Questions of Similar Designations
Top Interview Questions for Software Developer 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