Hot Topics
Grab Solution
Technical Round
- Question 1
What is the difference between outer and inner join?
- Answer
INNER JOIN | OUTER JOIN |
1. INNER JOIN results to form a tuple which has only the information related to both the tables. | OUTER JOIN results to form a tuple which also has the information that is not related to the other table. |
2. Clause used INNER JOIN and JOIN. | Clause used LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, etc. |
3. JOIN and INNER JOIN works in the same way. | FULL OUTER JOIN and FULL JOIN works in the same way. |
- Question 2
Finding groups in the matrix.
- Answer
Finding groups in a matrix refers to finding connected components of elements in the matrix that share the same value.
Here is an example solution in Java to find groups in a matrix:
public int findGroups(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int groupCount = 0;
boolean[][] visited = new boolean[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (!visited[row][col]) {
dfs(matrix, visited, row, col, matrix[row][col]);
groupCount++;
}
}
}
return groupCount;
}
private void dfs(int[][] matrix, boolean[][] visited, int row, int col, int value) {
if (row < 0 || row >= matrix.length || col < 0 || col >= matrix[0].length || visited[row][col] || matrix[row][col] != value) {
return;
}
visited[row][col] = true;
dfs(matrix, visited, row - 1, col, value);
dfs(matrix, visited, row + 1, col, value);
dfs(matrix, visited, row, col - 1, value);
dfs(matrix, visited, row, col + 1, value);
}
This solution uses a depth-first search (DFS) algorithm to traverse the matrix and find connected components of elements with the same value. The visited
matrix keeps track of which elements have already been visited to avoid visiting the same element multiple times. The dfs
function is used to traverse the connected components of elements with the same value. The findGroups
function returns the number of groups found in the matrix.
- Question 3
Within a given timeline, find the approximate number of cabs that would be required WRT an approximate number of riders. Given time line 12AM-12PM. Find the approximate number of cabs required.
- Answer
The exact number of cabs required depends on various factors such as the location, time of day, and demand for rides. It is not possible to determine the approximate number of cabs required with only the information provided. Please provide more details about the location and number of riders for a more accurate estimate.
- Question 4
Given binary tree, print its left view.
- Answer
The left view of a binary tree is a set of nodes that can be seen when the tree is viewed from the left side. To print the left view of a binary tree, you can use a level-order traversal, while keeping track of the maximum level seen so far. Here’s a sample code in Python:
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def left_view(root):
if root is None:
return
queue = [(root, 0)]
max_level = -1
while queue:
node, level = queue.pop(0)
if level > max_level:
print(node.data, end=' ')
max_level = level
if node.right:
queue.append((node.right, level + 1))
if node.left:
queue.append((node.left, level + 1))
root = Node(12)
root.left = Node(10)
root.right = Node(30)
root.right.left = Node(25)
root.right.right = Node(40)
left_view(root)
This code will print 12 10 25
which is the left view of the binary tree.
- Question 5
Format a string of numbers to display a currency-example “1234.678” to “1,234.68”.
- Answer
In Python, you can format a number to display as currency using the format
function with a specified format string. Here is an example:
number = 1234.678
formatted_number = "{:,.2f}".format(number)
print(formatted_number)
- Question 6
How to handle micro services with Node.js?
- Answer
Prerequisites and initialization
Setting up the server
Specifying the routes
Building the controller
Establishing the call
Execution
Incorporating the database into your architecture
- Question 7
Difference between HashMap and ConcurrentHashMap.
- Answer
HashMap | ConcurrentHashMap |
1. HashMap is not synchronized hence it is not thread safe. | ConcurrentHashMap is synchronized hence it is thread safe. |
2. Its performance is faster. | Its performance is slower. |
3. It can store null keys and values. | It can store null keys and values. |
- Question 8
What is overfitting and how you deal with it?
- Answer
Overfitting happens when a model works fine with trained data but doesn’t work efficiently in the evaluation set.
10 ways to deal with it:
Train with more data
Data augmentation
Addition of noise to the input data
Feature selection
Cross validation
Simplify data
Regularization
Ensembling
Early stopping
Adding dropout layer
- Question 9
Right view of binary search tree.
- Answer
The right view of a binary search tree is a set of nodes that can be seen when the tree is viewed from the right side. It consists of the rightmost nodes at each level of the tree, starting from the root node and ending at the leaf nodes. In a binary search tree, the right view contains nodes with values greater than the values in the left subtree and less than or equal to the values in the right subtree.
- Question 10
1- Person1 favourite numbers is X, Person2 favourite number is Y, non-empty zero-indexed array A consisting on N integers given, persons are interested in occurrences of their fav numbers X, Y in array, they are looking for longest leading fragment of array in which an equal number of occurrences of X, Y int fund(int X, int Y, int[] A) should return maximum A[P] contains same X and Y or -1 –
- Answer
Here’s one possible implementation of the function in Java:
public int findMaxLengthFragment(int X, int Y, int[] A) {
int N = A.length;
int countX = 0, countY = 0;
int maxLength = -1;
int start = 0;
for (int end = 0; end < N; end++) {
if (A[end] == X) {
countX++;
} else if (A[end] == Y) {
countY++;
}
while (countX > 0 && countY > 0 && countX != countY) {
if (A[start] == X) {
countX--;
} else if (A[start] == Y) {
countY--;
}
start++;
}
if (countX == countY) {
maxLength = Math.max(maxLength, end - start + 1);
}
}
return maxLength;
}
This function uses two pointers, one to keep track of the start of the fragment and the other to keep track of the end. It also keeps count of the number of occurrences of X and Y in the current fragment. The function updates the counts and the start pointer whenever the counts of X and Y become unequal. Finally, if the counts are equal, it updates the maximum length of a fragment with equal occurrences of X and Y.
- Question 11
A Linklist represent a number and every node contain a digit only. Write a function that take Root node and after adding one , it will return the the root node Write a function that add 1 to linkedlist number [2]->[4]->[5]->[7]->[3]->[6] afte plus 1 [2]->[4]->[5]->[7]->[3]->[7] + time and space complexity of the solution.
- Answer
Here’s an implementation of the function in Python:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def plusOne(head):
dummy = ListNode(0)
dummy.next = head
lastNotNine = dummy
curr = head
while curr:
if curr.val != 9:
lastNotNine = curr
curr = curr.next
lastNotNine.val += 1
lastNotNine = lastNotNine.next
while lastNotNine:
lastNotNine.val = 0
lastNotNine = lastNotNine.next
if dummy.val == 1:
return dummy
return dummy.next
The time complexity of this solution is O(n), where n is the number of digits in the linked list. The space complexity is O(1), as we only use a few extra variables.
- Question 12
Equal 3-sum of array.
- Answer
The equal 3-sum problem refers to finding three elements in an array that sum up to a specific target value. It’s a common problem in computer science and often used as a subproblem in more complex algorithms. The brute force approach would be to compare every possible combination of three elements in the array, which would have a time complexity of O(n^3). However, there are more efficient algorithms with better time complexity such as using sorting and two-pointer technique with O(n^2) time complexity.
- Question 13
Library function to convert string to integer.
- Answer
In many programming languages, there is a built-in function to convert a string to an integer. For example:
In Python, the
int()
function can be used to convert a string to an integer:int("123")
returns123
In Java, the
Integer.parseInt()
method can be used:Integer.parseInt("123")
returns123
In C, the
atoi()
function can be used:atoi("123")
returns123
In JavaScript, the
parseInt()
function can be used:parseInt("123")
returns123
Note that the string must represent a valid integer value and not contain any non-numeric characters, otherwise a ValueError
, NumberFormatException
, or similar error will be thrown.