
Nagarro


10+ Nagarro Technical Trainee Interview Questions and Answers
Q1. Nth Prime Number Problem Statement
Find the Nth prime number given a number N.
Explanation:
A prime number is greater than 1 and is not the product of two smaller natural numbers. A prime number has exactly two...read more
Find the Nth prime number given a number N.
A prime number is greater than 1 and is not the product of two smaller natural numbers
A prime number has exactly two distinct positive divisors: 1 and itself
Implement a function to find the Nth prime number based on the given input
Q2. Counting Derangements Problem
A derangement is a permutation of 'N' elements, where no element appears in its original position. For instance, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0} because each element ...read more
Count the total number of derangements possible for a set of 'N' elements.
A derangement is a permutation where no element appears in its original position.
Use the formula for derangements: !n = n! * (1 - 1/1! + 1/2! - 1/3! + ... + (-1)^n/n!)
Return the answer modulo (10^9 + 7) to handle large results.
Q3. Count Palindrome Words in a String
Given a string 'S' consisting of words, your task is to determine the number of palindrome words within 'S'. A word is considered a palindrome if it reads the same backward as...read more
Count the number of palindrome words in a given string.
Split the string into words using whitespace as delimiter.
Check each word if it is a palindrome by comparing it with its reverse.
Increment a counter for each palindrome word found.
Output the total count of palindrome words for each test case.
Q4. Character Formation Check
Determine if the second string STR2
can be constructed using characters from the first string STR1
. Both strings may include any characters.
Input:
The first line contains an integer T...read more
Check if second string can be formed using characters from the first string.
Iterate through each character in STR2 and check if it exists in STR1.
Use a hashmap to store the frequency of characters in STR1 for efficient lookup.
Return 'YES' if all characters in STR2 are found in STR1, otherwise return 'NO'.
Q5. 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 to find the start of the loop.
Adjust the pointers to remove the loop and return the modified linked list.
Q6. Trapping Rainwater Problem Statement
You are given an array ARR
of long type, which represents an elevation map where ARR[i]
denotes the elevation of the ith
bar. Calculate the total amount of rainwater that ca...read more
Calculate the total amount of rainwater that can be trapped within given elevation map.
Iterate through the array to find the maximum height on the left and right of each bar.
Calculate the amount of water that can be trapped above each bar by taking the minimum of the maximum heights on the left and right.
Sum up the trapped water above each bar to get the total trapped water for the elevation map.
Q7. 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 determining the number of distinct ways to climb from the 0th to the Nth stair by climbing one or two steps at a time.
Use dynamic programming to solve this problem efficiently.
The number of ways to reach the Nth stair can be calculated by adding the number of ways to reach the (N-1)th stair and the (N-2)th stair.
Consider base cases where N=0 and N=1 separately.
Handle large values of N by using modulo arithmetic.
Example: For N=3, the number of distinct way...read more
Q8. Spiral Order Traversal of a Binary Tree
Given a binary tree with N
nodes, your task is to output the Spiral Order traversal of the binary tree.
Input:
The input consists of a single line containing elements of ...read more
The task is to output the Spiral Order traversal of a binary tree given in level order.
Implement a function that returns the spiral order traversal as a list
Traverse the binary tree in a spiral order alternating between left to right and right to left
Use a queue to keep track of nodes at each level
Handle null nodes represented by -1 in the input
Q9. Jumping Game Problem Statement
In this problem, you have ‘n’ carrots lined up and denoted by numbers 1 through ‘n’. There are ‘k’ rabbits, and each rabbit can jump to carrots that are multiples of its unique ju...read more
Calculate uneaten carrots after rabbits jump based on their unique factors.
Iterate through each rabbit's jumping factor and mark the carrots they land on as eaten
Calculate the remaining uneaten carrots by counting the ones not marked as eaten
Consider using a data structure like an array to keep track of eaten carrots efficiently
Q10. Distribute N Candies Among K People
Explanation: Sanyam wishes to distribute 'N' candies among 'K' friends. The friends are arranged based on Sanyam's order of likeness. He initially distributes candies such th...read more
Distribute N candies among K people in Sanyam's order of likeness, incrementing distribution by K each round until all candies are distributed.
Distribute candies starting from 1st friend, incrementing by K each round
If remaining candies are fewer than what a friend is supposed to receive, stop distribution
Output the number of candies each friend ends up with at the end of distribution
Q11. 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
The task is to find all pairs of elements in an array that add up to a given target.
Iterate through the array and for each element, check if the target minus the element exists in a hash set.
If it exists, add the pair to the result. If not, add the element to the hash set.
Print the pairs found or (-1, -1) if no pair is found.
Q12. Reverse Linked List Problem Statement
Given a Singly Linked List of integers, your task is to reverse the Linked List by altering the links between the nodes.
Input:
The first line of input is an integer T, rep...read more
Reverse a singly linked list by altering the links between nodes.
Iterate through the linked list and reverse the links between nodes
Use three pointers to keep track of the current, previous, and next nodes
Update the links between nodes to reverse the list
Return the head of the reversed linked list
Q13. Reverse Stack with Recursion
Reverse a given stack of integers using recursion. You must accomplish this without utilizing extra space beyond the internal stack space used by recursion. Additionally, you must r...read more
Reverse a given stack of integers using recursion without extra space or loop constructs.
Use recursion to pop all elements from the original stack and store them in function call stack.
Once the stack is empty, push the elements back in reverse order using recursion.
Ensure to handle base cases for empty stack or single element stack.
Example: If the input stack is [1, 2, 3], after reversal it should be [3, 2, 1].
Q14. 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 for each test case.
Q15. 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.
Q16. Maximum Sum Path in a Binary Tree
Your task is to determine the maximum possible sum of a simple path between any two nodes (possibly the same) in a given binary tree of 'N' nodes with integer values.
Explanati...read more
Find the maximum sum of a simple path between any two nodes in a binary tree.
Use a recursive approach to traverse the binary tree and calculate the maximum sum path.
Keep track of the maximum sum path found so far while traversing the tree.
Consider negative values in the path sum calculation to handle cases where the path can start and end at different nodes.
Handle cases where the path can go through the root node or not.
Q17. Properties of MST in an Undirected Graph
You have a simple undirected graph G = (V, E)
where each edge has a distinct weight. Consider an edge e
as part of this graph. Determine which of the given statements re...read more
In an undirected graph with distinct edge weights, the lightest edge in a cycle is included in every MST, while the heaviest edge is excluded.
The lightest edge in a cycle is always included in every MST of the graph.
The heaviest edge in a cycle is always excluded from every MST of the graph.
This property holds true for all simple undirected graphs with distinct edge weights.
Q18. Find The Closest Perfect Square Problem Statement
You are given a positive integer N
. Your task is to find the perfect square number that is closest to N
and determine the number of steps required to reach that...read more
Find the closest perfect square to a given positive integer and determine the number of steps required to reach that number.
Iterate through perfect squares starting from 1 until the square is greater than the given number
Calculate the distance between the perfect square and the given number
Return the closest perfect square and the distance as output
More about working at Nagarro










Reviews
Interviews
Salaries
Users/Month

