Associate Software Engineer
400+ Associate Software Engineer Interview Questions and Answers for Freshers
Popular Companies
Q1. 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 the array and use nested loops to find all possible triplets.
Keep track of the triplets that sum up to the target number.
Handle cases where no triplet exists for a given target sum.
Q2. 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
The problem is to connect N ropes of different lengths 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 remaining, 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 as the mini...read more
Q3. Intersection of Two Arrays II
Given two integer arrays ARR1
and ARR2
of size N
and M
respectively, find the intersection of these arrays. An intersection refers to elements that appear in both arrays.
Note:
Inp...read more
Find the intersection of two integer arrays in the order they appear in the first array.
Iterate through the first array and store elements in a hashmap with their frequencies.
Iterate through the second array and check if the element exists in the hashmap, decrement frequency if found.
Return the elements that have non-zero frequencies as the intersection.
Q4. Ninja and Alternating Largest Problem Statement
Ninja is given a sequence of numbers and needs to rearrange them so that every second element is greater than its neighbors on both sides.
Example:
Input:
[1, 2, ...read more
The task is to rearrange the given array such that every second element is greater than its left and right element.
Read the number of test cases
For each test case, read the number of elements in the array and the array elements
Iterate through the array and swap elements at odd indices with their adjacent elements if necessary
Check if the rearranged array satisfies the conditions and print 1 if it does, else print 0
Q5. Add K Nodes Problem Statement
You are given a singly linked list of integers and an integer 'K'. Your task is to modify the linked list by inserting a new node after every 'K' node in the linked list. The value...read more
Modify a singly linked list by inserting a new node after every 'K' nodes with the sum of previous 'K' nodes.
Traverse the linked list while keeping track of 'K' nodes at a time
Calculate the sum of the 'K' nodes and insert a new node with the sum after every 'K' nodes
Handle the case where the number of remaining nodes is less than 'K' by inserting a node with the sum of remaining nodes
Update the pointers accordingly to maintain the linked list structure
Q6. Longest Common Prefix After Rotation
You are given two strings 'A' and 'B'. While string 'A' is constant, you may apply any number of left shift operations to string 'B'.
Explanation:
Your task is to calculate ...read more
The question asks to find the minimum number of left shift operations required to obtain the longest common prefix of two given strings.
Perform left shift operations on string B to find the longest common prefix with string A
Count the number of left shift operations required to obtain the longest common prefix
Return the minimum number of left shift operations for each test case
Share interview questions and help millions of jobseekers 🌟
Q7. Factorial Trailing Zeros Problem
You are provided with a positive integer N. Your goal is to determine the smallest number whose factorial has at least N trailing zeros.
Example:
Input:
N = 1
Output:
5
Explanat...read more
Find the smallest number whose factorial has at least N trailing zeros.
Calculate the number of 5's in the prime factorization of the factorial to determine the trailing zeros.
Use binary search to find the smallest number with at least N trailing zeros.
Consider edge cases like N = 0 or N = 1 for factorial trailing zeros problem.
Q8. Balanced Parentheses Combinations
Given an integer N
representing the number of pairs of parentheses, find all the possible combinations of balanced parentheses using the given number of pairs.
Explanation:
Con...read more
Generate all possible combinations of balanced parentheses given the number of pairs.
Use backtracking to generate all valid combinations of balanced parentheses.
Keep track of the number of open and close parentheses used in each combination.
Recursively build the combinations by adding open parentheses if there are remaining, and then adding close parentheses if the number of open parentheses is greater than the number of close parentheses.
Example: For N = 2, valid combination...read more
Associate Software Engineer Jobs
Q9. Chocolate Distribution Problem
You are given an array/list CHOCOLATES
of size 'N', where each element represents the number of chocolates in a packet. Your task is to distribute these chocolates among 'M' stude...read more
Distribute chocolates among students to minimize the difference between the largest and smallest number of chocolates.
Sort the array of chocolates packets.
Use sliding window technique to find the minimum difference between the largest and smallest packets distributed to students.
Return the minimum difference as the output.
Q10. Intersection of Linked List Problem Statement
You are provided with two singly linked lists of integers. These lists merge at a node of a third linked list.
Your task is to determine the data of the node where ...read more
Given two linked lists, find the node where they intersect, if any.
Traverse both lists to find their lengths and the difference in lengths
Move the pointer of the longer list by the difference in lengths
Traverse both lists in parallel until they meet at the intersection node
Q11. Kth Smallest and Largest Element Problem Statement
You are provided with an array 'Arr' containing 'N' distinct integers and a positive integer 'K'. Your task is to find the Kth smallest and Kth largest element...read more
Find the Kth smallest and largest elements in an array.
Sort the array and return the Kth element for smallest and (N-K+1)th element for largest.
Ensure K is within the array size to avoid out of bounds error.
Handle multiple test cases efficiently by iterating through each case.
Q12. 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
Find pairs of elements in an array that sum up to a given value, sorted in a specific order.
Iterate through the array and for each element, check if the complement (S - current element) exists in a hash set.
If the complement exists, add the pair to the result list.
Sort the result list based on the criteria mentioned in the problem statement.
Q13. 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
An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.
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 is an Armstrong number.
Return 'YES' if the number is an Armstrong number, 'NO' otherwise.
Q14. Combination Sum Problem Statement
Given three integers X
, Y
, and Z
, calculate the sum of all numbers that can be formed using the digits 3, 4, and 5. Each digit can be used up to a maximum of X
, Y
, and Z
times ...read more
Calculate the sum of all numbers that can be formed using the digits 3, 4, and 5 with given constraints.
Iterate through all possible combinations of 3, 4, and 5 based on the given constraints.
Calculate the sum of each combination and add them up.
Return the final sum modulo 10^9 + 7.
Q15. Equilibrium Index Problem Statement
Given an array Arr
consisting of N integers, your task is to find the equilibrium index of the array.
An index is considered as an equilibrium index if the sum of elements of...read more
Find the equilibrium index of an array where sum of elements on left equals sum on right.
Iterate through the array and calculate the total sum of all elements.
For each index, calculate the left sum and right sum and check if they are equal.
Return the index if found, otherwise return -1.
Q16. One of the questions for Critical Reasoning was:- Two cars A and B cross a flyover in 10 minutes and 30 minutes respectively. Find the speed of Car A. Statements: Car B travels at the 50kmph Train A and B are t...
read moreCar A's speed is 90kmph
Use the formula: Speed = Distance/Time
Assume the distance to be the same for both cars
Calculate Car A's time using the given information
Substitute the values in the formula to get Car A's speed
Q17. Sub Sort Problem Statement
You are given an integer array ARR
. Determine the length of the shortest contiguous subarray which, when sorted in ascending order, results in the entire array being sorted in ascendi...read more
The question asks to find the length of the shortest contiguous subarray that needs to be sorted in order to sort the whole array.
Iterate through the array and find the first element that is out of order from the left side.
Iterate through the array and find the first element that is out of order from the right side.
Find the minimum and maximum element within the subarray from the above steps.
Expand the subarray from both sides until all elements within the subarray are in the...read more
Q18. Reverse Linked List Problem Statement
Given a singly linked list of integers, your task is to return the head of the reversed linked list.
Example:
Input:
The given linked list is 1 -> 2 -> 3 -> 4 -> NULL.
Outp...read more
To reverse a singly linked list of integers, return the head of the reversed linked list.
Iterate through the linked list, reversing the pointers to point to the previous node instead of the next node.
Keep track of the previous, current, and next nodes while traversing the list.
Update the head of the reversed linked list to be the last element of the original list.
Q19. Infix to Postfix Conversion
You are provided with a string EXP
which represents a valid infix expression. Your task is to convert this given infix expression into a postfix expression.
Explanation:
An infix exp...read more
Convert a given infix expression to postfix expression.
Use a stack to keep track of operators and operands.
Follow the rules of precedence for operators (*, / have higher precedence than +, -).
Handle parentheses by pushing them onto the stack and popping when closing parenthesis is encountered.
Q20. Tiling Problem Statement
Given a board with 2 rows and N columns, and an infinite supply of 2x1 tiles, determine the number of distinct ways to completely cover the board using these tiles.
You can place each t...read more
The problem involves finding the number of distinct ways to completely cover a 2xN board using 2x1 tiles.
Use dynamic programming to solve the tiling problem efficiently.
Define a recursive function to calculate the number of ways to tile the board.
Consider both horizontal and vertical placements of tiles.
Implement the function to handle large values of N by using modulo arithmetic.
Optimize the solution to avoid redundant calculations.
Q21. Decode String Problem Statement
Your task is to decode a given encoded string back to its original form.
Explanation:
An encoded string format is
The task is to decode an encoded string back to its original form by repeating the encoded string 'count' times.
Parse the input string to extract the count and the encoded string within the brackets
Use recursion to decode the encoded string by repeating it 'count' times
Handle nested encoded strings by recursively decoding them
Output the decoded string for each test case
Q22. Find Middle of Linked List
Given the head node of a singly linked list, your task is to return a pointer pointing to the middle node of the linked list.
When the number of elements is odd, return the middle ele...read more
Return the middle node of a singly linked list, selecting the one farther from the head node in case of even number of elements.
Traverse the linked list using two pointers, one moving twice as fast as the other.
When the fast pointer reaches the end, the slow pointer will be at the middle node.
Return the node pointed by the slow pointer as the middle node.
Q23. Longest Path In Directed Graph Problem Statement
Given a Weighted Directed Acyclic Graph (DAG) comprising 'N' nodes and 'E' directed edges, where nodes are numbered from 0 to N-1, and a source node 'Src'. Your ...read more
The task is to find the longest distances from a source node to all nodes in a weighted directed acyclic graph.
Implement a function that takes the number of nodes, edges, source node, and edge weights as input.
Use a topological sorting algorithm to traverse the graph and calculate the longest distances.
Return an array of integers where each element represents the longest distance from the source node to the corresponding node.
Q24. N-th Term Of Geometric Progression
Find the N-th term of a Geometric Progression (GP) series given the first term A, the common ratio R, and the term position N.
Explanation:
The general form of a GP series is ...read more
Calculate the N-th term of a Geometric Progression series given the first term, common ratio, and term position.
Iterate through each test case and apply the formula A * R^(N-1) to find the N-th term
Use modular arithmetic to handle large calculated terms by returning the result modulo 10^9 + 7
Q25. Ninja and Substrings Problem Statement
Ninja has to determine all the distinct substrings of size two that can be formed from a given string 'STR' comprising only lowercase alphabetic characters. These substrin...read more
The task is to find all the different possible substrings of size two that appear in a given string as contiguous substrings.
Iterate through the string and extract substrings of size two
Store the substrings in an array
Return the array of substrings
Q26. Question 2 was, Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n.
Find sum of numbers in range 1 to m (both inclusive) not divisible by n. Return difference between sum of non-divisible and divisible numbers.
Iterate through range 1 to m and check if number is divisible by n.
If not divisible, add to sum of non-divisible numbers.
If divisible, add to sum of divisible numbers.
Return difference between sum of non-divisible and divisible numbers.
Q27. Find First Repeated Character in a String
Given a string 'STR' composed of lowercase English letters, identify the character that repeats first in terms of its initial occurrence.
Example:
Input:
STR = "abccba"...read more
The task is to find the first repeated character in a given string of lowercase English letters.
Iterate through the string and keep track of characters seen so far in a set.
If a character is already in the set, return it as the first repeated character.
If no repeated character is found, return '%'.
Q28. Largest BST Subtree Problem
Given a binary tree with 'N' nodes, determine the size of the largest subtree that is also a BST (Binary Search Tree).
Input:
The first line contains an integer 'T', representing the...read more
The problem involves finding the size of the largest subtree that is also a Binary Search Tree in a given binary tree.
Traverse the binary tree in a bottom-up manner to check if each subtree is a BST.
Keep track of the size of the largest BST subtree encountered so far.
Use recursion to solve the problem efficiently.
Consider edge cases like empty tree or single node tree.
Example: For input 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1, the largest BST subtree has size 3.
Q29. Prime Time Again Problem Statement
You are given two integers DAY_HOURS
and PARTS
. Consider a day with DAY_HOURS
hours, which can be divided into PARTS
equal parts. Your task is to determine the total instances...read more
Calculate total instances of equivalent prime groups in a day divided into equal parts.
Divide the day into equal parts and find prime numbers in each part.
Identify prime groups where prime numbers occur at the same position in different parts.
Return the total number of equivalent prime groups found.
Q30. Palindrome String Validation
Determine if a given string 'S' is a palindrome, considering only alphanumeric characters and ignoring spaces and symbols.
Note:
The string 'S' should be evaluated in a case-insensi...read more
Check if a given string is a palindrome after removing special characters, spaces, and converting to lowercase.
Remove special characters and spaces from the string
Convert the string to lowercase
Check if the modified string is a palindrome by comparing characters from start and end
Q31. Bipartite Graph Problem Statement
Determine if a given graph is bipartite. A graph is bipartite if its vertices can be divided into two independent sets, 'U' and 'V', such that every edge ('u', 'v') connects a ...read more
The function checks whether a given graph is bipartite or not.
A bipartite graph can be divided into two independent sets such that every edge connects a vertex from one set to the other.
We can use graph coloring algorithm to check if the graph is bipartite.
Start by coloring the first vertex with one color and all its neighbors with the other color.
Continue coloring the remaining vertices, making sure that no adjacent vertices have the same color.
If at any point, we find that ...read more
Q32. 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
The task is to 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 platform count when a new train arrives before the previous one departs.
Q33. Find the Longest Palindromic Substring
Given a string ‘S’ composed of lowercase English letters, your task is to identify the longest palindromic substring within ‘S’.
If there are multiple longest palindromic ...read more
Find the longest palindromic substring in a given string, returning the rightmost one if multiple substrings are of the same length.
Iterate through each character in the string and expand around it to check for palindromes
Keep track of the longest palindromic substring found so far
Return the rightmost longest palindromic substring
Q34. Detect and Remove Loop in Linked List
For a given singly linked list, identify if a loop exists and remove it, adjusting the linked list in place. Return the modified linked list.
Expected Complexity:
Aim for a...read more
Detect and remove loop in a singly linked list in place with O(n) time complexity and O(1) space complexity.
Use Floyd's Cycle Detection Algorithm to identify the loop in the linked list.
Once the loop is detected, use two pointers approach to find the start of the loop.
Adjust the pointers to remove the loop and return the modified linked list.
Example: For input 5 2 and 1 2 3 4 5, output should be 1 2 3 4 5.
Q35. Implementing a Priority Queue Using Heap
Ninja has been tasked with implementing a priority queue using a heap data structure. However, he is currently busy preparing for a tournament and has requested your ass...read more
Implement a priority queue using a heap data structure by completing the provided functions: push(), pop(), getMaxElement(), and isEmpty().
Understand the operations: push() to insert element, pop() to remove largest element, getMaxElement() to return largest element, and isEmpty() to check if queue is empty.
Implement a heap data structure to maintain the priority queue.
Handle different types of queries based on the input provided.
Ensure correct output for each type 3 query.
Te...read more
Q36. 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 previously calculated Fibonacci numbers to avoid redundant calculations.
Start with base cases F(1) and F(2) as 1, then iterate to calculate subsequent Fibonacci numbers.
Return the Nth Fibonacci number as the final result.
Q37. DFS Traversal Problem Statement
Given an undirected and disconnected graph G(V, E)
, where V
is the number of vertices and E
is the number of edges, the connections between vertices are provided in the 'GRAPH' m...read more
The question asks to print the DFS traversal of an undirected and disconnected graph.
Implement a Depth First Search (DFS) algorithm to traverse the graph.
Use a visited array to keep track of visited vertices.
For each unvisited vertex, start a DFS traversal and print the connected component.
Sort the vertices of each connected component in ascending order before printing.
Q38. Pythagorean Triplets Detection
Determine if an array contains a Pythagorean triplet by checking whether there are three integers x, y, and z such that x2 + y2 = z2 within the array.
Input:
The first line contai...read more
Detect if an array contains a Pythagorean triplet by checking if there are three integers x, y, and z such that x^2 + y^2 = z^2.
Iterate through all possible combinations of three integers in the array and check if x^2 + y^2 = z^2.
Use a nested loop to generate all possible combinations efficiently.
Return 'yes' if a Pythagorean triplet is found, otherwise return 'no'.
Q39. Constellation Identification Problem
Given a matrix named UNIVERSE
with 3 rows and 'N' columns, filled with characters {#, *, .}, where:
- '*' represents stars.
- '.' represents empty space.
- '#' represents a separ...read more
The task is to identify constellations shaped like vowels within a matrix filled with characters {#, *, .}.
Iterate through the matrix to find 3x3 constellations shaped like vowels.
Check for vowels 'A', 'E', 'I', 'O', 'U' in each 3x3 constellation.
Print the vowels found in each constellation for each test case.
Q40. Remove String from Linked List Problem
You are provided with a singly linked list where each node contains a single character, along with a string 'STR'. Your task is to remove all occurrences of the string 'ST...read more
Remove all occurrences of a given string from a singly linked list by starting from the end of the list.
Traverse the linked list from the end to the beginning to efficiently remove the string occurrences.
Check for the string 'STR' in each node and remove it if found.
Continue checking for new occurrences of 'STR' after removal to ensure all instances are removed.
Return the head of the modified linked list after processing each test case.
Q41. Remove Vowels from a Given String
Given a string STR
of length N
, your task is to remove all the vowels present in that string and return the modified string.
Explanation:
English alphabets ‘a’, ‘e’, ‘i’, ‘o’, ...read more
Remove vowels from a given string while maintaining the relative position of other characters.
Iterate through each character in the string and check if it is a vowel (a, e, i, o, u).
If the character is not a vowel, add it to the modified string.
Return the modified string as the output.
Q42. Replace Spaces in a String
Given a string STR
consisting of words separated by spaces, your task is to replace all spaces between words with the characters "@40".
Input:
The first line contains an integer ‘T’ d...read more
Replace spaces in a string with '@40'.
Iterate through the string and replace spaces with '@40'.
Return the modified string for each test case.
Handle multiple test cases by iterating through each one.
Q43. 1) What is NullPointerExceprion and give me a example?
NullPointerException is a runtime exception that occurs when a program tries to access or use an object reference that is null.
It is a common exception in Java programming.
It is thrown when a program attempts to use an object reference that has not been initialized.
It indicates that there is an attempt to access or invoke a method on an object that is null.
Example: String str = null; str.length();
Q44. Buy and Sell Stock Problem Statement
Imagine you are Harshad Mehta's friend, and you have been given the stock prices of a particular company for the next 'N' days. You can perform up to two buy-and-sell transa...read more
The task is to determine the maximum profit that can be achieved by performing up to two buy-and-sell transactions on a given set of stock prices.
Iterate through the array of stock prices to find the maximum profit that can be achieved by buying and selling stocks.
Keep track of the maximum profit that can be achieved by considering all possible combinations of buy and sell transactions.
Ensure that you sell the stock before buying again to adhere to the constraints of the prob...read more
Q45. 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.
Q46. Maximum Sum Path in a Binary Tree Problem Statement
You are provided with a binary tree consisting of N
nodes where each node has an integer value. The task is to determine the maximum sum achievable by a simpl...read more
Find the maximum sum achievable by a simple path between any two nodes in a binary tree.
Traverse the binary tree to find all possible paths and calculate their sums.
Keep track of the maximum sum encountered during traversal.
Consider paths that may include the same node twice.
Implement a recursive function to explore all paths in the tree.
Optimize the solution to avoid redundant calculations.
Q47. Minimum Cash Flow Problem
Consider 'N' friends who have borrowed money from one another. They now wish to settle their debts by giving or taking cash among themselves in a way that minimizes the total cash flow...read more
The problem involves settling debts among friends by minimizing total cash flow.
Create a graph where nodes represent friends and edges represent debts.
Calculate the net amount each friend owes or is owed by summing up the debts and credits.
Use a recursive algorithm to settle debts by finding the maximum and minimum amounts owed.
Repeat the process until all debts are settled.
Output the total cash flow required to settle all debts.
Q48. Swap Two Numbers Problem Statement
Given two integers a
and b
, your task is to swap these numbers and output the swapped values.
Input:
The first line contains a single integer 't', representing the number of t...read more
Swap two numbers 'a' and 'b' and output the swapped values.
Create a temporary variable to store one of the numbers before swapping
Swap the values of 'a' and 'b' using the temporary variable
Output the swapped values as 'b' followed by 'a'
Example: If 'a' = 3 and 'b' = 4, after swapping 'a' will be 4 and 'b' will be 3
Q49. Minimise Sum Problem Statement
You are given a matrix of 'N' rows and 'M' columns and a non-negative integer 'K'. Determine the minimum possible sum of all elements in each submatrix after performing at most 'K...read more
Given a matrix and a non-negative integer K, find the minimum possible sum of all elements in each submatrix after performing at most K decrements.
Iterate through all submatrices and find the minimum possible sum after performing decrements
Keep track of the number of decrements performed on each element
Use dynamic programming to optimize the solution
Ensure not to decrease any number below 0
Return the minimum possible sum modulo 10^9 + 7
Q50. Remove Vowels from a String
Given a string STR
of length N
, your task is to remove all the vowels from that string and return the modified string.
Input:
The first line of input contains an integer 'T' represen...read more
Remove all vowels from a given string and return the modified string.
Iterate through each character in the string and check if it is a vowel (a, e, i, o, u or A, E, I, O, U).
If the character is not a vowel, add it to a new string.
Return the new string with all vowels removed.
Interview Questions of Similar Designations
Top Interview Questions for Associate 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