MakeMyTrip
10+ Megamind Consultants Interview Questions and Answers
Q1. Tower of Hanoi Problem Statement
You have three rods numbered from 1 to 3, and 'N' disks initially stacked on the first rod in increasing order of their sizes (largest disk at the bottom). Your task is to move ...read more
Tower of Hanoi problem involves moving 'N' disks from one rod to another following specific rules in less than 2^N moves.
Implement a recursive function to move disks from one rod to another while following the rules.
Use the concept of recursion and backtracking to solve the Tower of Hanoi problem efficiently.
Maintain a count of moves and track the movement of disks in a 2-D array/list.
Ensure that larger disks are not placed on top of smaller disks during the movement.
The solu...read more
Q2. Minimum Operations Problem Statement
You are given an array 'ARR'
of size 'N'
consisting of positive integers. Your task is to determine the minimum number of operations required to make all elements in the arr...read more
Minimum number of operations to make all elements in an array equal by performing addition, subtraction, multiplication, or division.
Iterate through the array to find the maximum and minimum elements.
Calculate the difference between the maximum and minimum elements.
The minimum number of operations needed is the difference between the maximum and minimum elements.
Q3. Maximum Distance Problem Statement
Given an integer array 'A' of size N, your task is to find the maximum value of j - i, with the restriction that A[i] <= A[j], where 'i' and 'j' are indices of the array.
Inpu...read more
Find the maximum distance between two elements in an array where the element at the first index is less than or equal to the element at the second index.
Iterate through the array and keep track of the minimum element encountered so far along with its index.
Calculate the maximum distance by subtracting the current index from the index of the minimum element.
Update the maximum distance if a greater distance is found while iterating.
Return the maximum distance calculated.
Q4. Make Array Elements Equal Problem Statement
Given an integer array, your objective is to change all elements to the same value, minimizing the cost. The cost of changing an element from x
to y
is defined as |x ...read more
Find the minimum cost to make all elements of an array equal by changing them to the same value.
Calculate the median of the array and find the sum of absolute differences between each element and the median.
Sort the array and find the median element, then calculate the sum of absolute differences between each element and the median.
If the array has an even number of elements, consider the average of the two middle elements as the median.
Q5. Count Inversions Problem Statement
Given an integer array ARR
of size N
, your task is to find the total number of inversions that exist in the array.
An inversion is defined for a pair of integers in the array ...read more
Count the total number of inversions in an integer array.
Iterate through the array and for each pair of elements, check if the inversion condition is met.
Use a nested loop to compare each pair of elements efficiently.
Keep a count of the inversions found and return the total count at the end.
Q6. Maximum Consecutive Ones Problem Statement
Given a binary array 'ARR' of size 'N', your task is to determine the maximum length of a sequence consisting solely of 1’s that can be obtained by converting at most ...read more
Find the maximum length of a sequence of 1's by converting at most K zeroes into ones in a binary array.
Iterate through the array and keep track of the current window of 1's and zeroes.
Use a sliding window approach to find the maximum length of the sequence.
Update the window by flipping zeroes to ones within the limit of K flips.
Return the maximum length of the sequence obtained.
Q7. Number of Islands II Problem Statement
You have a 2D grid of dimensions 'N' rows by 'M' columns, initially filled with water. You are given 'Q' queries, where each query contains two integers 'X' and 'Y'. Durin...read more
The task is to determine the number of islands present on a 2D grid after each query of converting water to land.
Create a function that takes the grid dimensions, number of queries, and query coordinates as input.
For each query, convert the water at the given coordinates to land and then count the number of islands on the grid.
Use depth-first search (DFS) or breadth-first search (BFS) to identify connected land cells forming islands.
Return the number of islands after each que...read more
Q8. Square Root with Decimal Precision Problem Statement
You are provided with two integers, 'N' and 'D'. Your objective is to determine the square root of the number 'N' with a precision up to 'D' decimal places. ...read more
Implement a function to find the square root of a number with a given precision.
Create a function that takes 'N' and 'D' as input parameters
Use a mathematical algorithm like Newton's method to approximate the square root
Iterate until the precision condition is met
Return the square root with the required precision
Q9. Shortest Path in a Binary Matrix Problem Statement
Given a binary matrix of size N * M
where each element is either 0 or 1, find the shortest path from a source cell to a destination cell, consisting only of 1s...read more
Find the shortest path in a binary matrix from a source cell to a destination cell consisting only of 1s.
Use Breadth First Search (BFS) algorithm to find the shortest path.
Keep track of visited cells to avoid revisiting them.
Calculate the path length by counting the number of 1s in the path.
Handle edge cases such as invalid inputs and unreachable destination.
Q10. Reach the Destination Problem Statement
You are given a source point (sx, sy) and a destination point (dx, dy). Determine if it is possible to reach the destination point using only the following valid moves:
- ...read more
The problem involves determining if it is possible to reach a destination point from a source point using specified moves.
Iterate through each test case and check if destination is reachable from source using specified moves
Implement a recursive function to explore all possible paths from source to destination
Keep track of visited points to avoid infinite loops
Return true if destination is reachable, false otherwise
Q11. Maximum Sum of Products for Array Rotations
You are given an array ARR
consisting of N
elements. Your task is to determine the maximum value of the summation of i * ARR[i]
among all possible rotations of ARR
. R...read more
Find the maximum sum of products for array rotations.
Iterate through all possible rotations of the array and calculate the sum of products for each rotation.
Keep track of the maximum sum of products found so far.
Return the maximum sum of products obtained.
Q12. 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
Determine the length of the shortest contiguous subarray that needs to be sorted to make the entire array sorted in ascending order.
Iterate from left to right to find the first element out of order.
Iterate from right to left to find the last element out of order.
Calculate the length of the subarray between the two out of order elements.
Q13. Validate Binary Search Tree (BST)
You are given a binary tree with 'N' integer nodes. Your task is to determine whether this binary tree is a Binary Search Tree (BST).
BST Definition:
A Binary Search Tree (BST)...read more
Validate if a given binary tree is a Binary Search Tree (BST) or not.
Check if the left subtree of a node contains only nodes with data less than the node's data.
Check if the right subtree of a node contains only nodes with data greater than the node's data.
Ensure that both the left and right subtrees are also binary search trees.
Traverse the tree in an inorder manner and check if the elements are in sorted order.
Recursively check each node's value against the range of values ...read more
Q14. Longest Duplicate Substring Problem Statement
You are provided with a string 'S'. The task is to determine the length of the longest duplicate substring within this string. Note that duplicate substrings can ov...read more
Find the length of the longest duplicate substring in a given string.
Iterate through all possible substrings of the input string.
Use a rolling hash function to efficiently compare substrings.
Store the lengths of duplicate substrings and return the maximum length.
Q15. Delete Node in Binary Search Tree Problem Statement
You are provided with a Binary Search Tree (BST) containing 'N' nodes with integer data. Your task is to remove a given node from this BST.
A BST is a binary ...read more
Implement a function to delete a node from a Binary Search Tree and return the inorder traversal of the modified BST.
Understand the properties of a Binary Search Tree (BST) - left subtree contains nodes with data less than the node, right subtree contains nodes with data greater than the node.
Implement a function to delete the given node from the BST while maintaining the BST properties.
Perform inorder traversal of the modified BST to get the desired output.
Handle cases where...read more
Q16. 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 of integers and a target sum, find all pairs of elements that add up to the target sum.
Use a hashmap to store the difference between the target sum and each element as keys and their indices as values.
Iterate through the array and check if the current element's complement exists in the hashmap.
Return the pairs of elements that add up to the target sum in sorted order.
Q17. Determine the Left View of a Binary Tree
You are given a binary tree of integers. Your task is to determine the left view of the binary tree. The left view consists of nodes that are visible when the tree is vi...read more
The task is to determine the left view of a binary tree, which consists of nodes visible when viewed from the left side.
Traverse the binary tree level by level from left to right, keeping track of the first node encountered at each level.
Use a queue to perform level order traversal and keep track of the level number for each node.
Store the first node encountered at each level in a result array to get the left view of the binary tree.
Multithreading allows multiple threads to run concurrently, while semaphores are used to control access to shared resources in a synchronized manner.
Multithreading involves running multiple threads within a single process, allowing for parallel execution of tasks.
Semaphores are used to control access to shared resources by allowing only a certain number of threads to access the resource at a time.
Semaphores can be binary (mutex) or counting, with binary semaphores used for mu...read more
Top Full Stack Developer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month