Coding Interview Questions
Coding Interview Questions
Q6. Write a function to nd the maximum for each and every contiguous subarray of
size k.
• Input: N = 9, K = 3 arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
• Output: {3, 3, 4, 5, 5, 5, 6}
• Explanation: In the rst subarray of size 3: {1,2,3}, the value 3 is maximum,
similarly for all such subarrays for size 3.
Q8. Write a function to print all unique rows of the given matrix.
Input:
{{1, 1, 1, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 1, 1, 0, 0}}
Output:
{{1, 1, 1, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0}}
Q10. Find the subsequence of length 3 with the highest product from a sequence of
non-negative integers, with the elements in increasing order.
• Input: n = 8 arr[ ] = {6, 7, 10, 1, 2, 3, 11, 12}
• Output: {10, 11, 12}
The three increasing elements of the given arrays are 10, 11, and 12, which form a three-
size subsequence with the highest product.
Q12. Write a function to connect nodes at the same level of a binary tree
Input: 100
/ \
13 15
/ \ \
14 1 20
Q13. Write a function to nd number of structurally unique binary trees are possible
fi
fi
Input: N = 3
Output: 5 for N = 3, there are 5 possible BSTs:
1 3 3 21
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Q15. Write a function to determine whether duplicate elements in a given array are
within a given distance of each other.
• Input: arr[] = {1, 2, 3, 4, 2, 1, 2} range=3
• Output: True
Q16. Write a recursive function to calculate the height of a binary tree in Java.
• Consider that every node of a tree represents a class called Node as given below:
public class Node{
int data;
Node left;
Node right;
}
Q19. Given an m x n 2D grid map of '1’s which represents land and '0’s that
represents water return the number of islands (surrounded by water and formed by
connecting adjacent lands in 2 directions - vertically or horizontally).
Assume that the boundary cases - which are all four edges of the grid are
surrounded by water.
Constraints are:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] can only be ‘0’ or ‘1’.
Example:
fi
fi
Input: grid = [
[“1” , “1” , “1” , “0” , “0”],
[“1” , “1” , “0” , “0” , “0”],
[“0” , “0” , “1” , “0” , “1”],
[“0” , “0” , “0” , “1” , “1”]
]
Output: 3
Q21. How to nd out if the given two strings are anagrams or not?
Hint:
Two strings are anagrams if they contain a similar group of characters in a varied
sequence.
Q22. How do you calculate the number of vowels and consonants in a String?
Hint:
Loop through the string
Hint:
Try implementing nested loop
Q28. Write a code to nd the missing number in a given integer array of 1 to 100.
Q29. Write a code to check if two rectangles overlap with each other.
Q31. Write a code to add an element at the middle of the linked list.
Q37. Write a code to remove Nth Node from the end of a linked list.
Q38. Write a code to check if two strings are a rotation of each other.
Q39. Write a code to nd the sum of two linked lists using Stack.
Q40. Write a code to remove all elements from a linked list of integers which
matches with given value.
Q42. Write a code to nd the highest repeating word from a given le.
Q46. Write a code to add two numbers without using the plus operator.
Q47. Write a code to count a number of leaf nodes in a given binary tree.
Q48. Write a code to traverse a binary tree in postorder traversal without recursion.
Q50. Write a code to nd the third node from the end in a singly linked list.
fi
fi
fi
fi
fi