0% found this document useful (0 votes)
14 views

Coding Interview Questions

SOmething

Uploaded by

Imran Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Coding Interview Questions

SOmething

Uploaded by

Imran Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Pdf -10 Coding based questions

Q1. Write a program to remove duplicates from a sorted array in place?


• Input: {1, 1, 1, 2, 3, 3, 6, 6, 7}
• Output: {1, 2, 3, 6, 7}
• Explanation: The given input has only 1,2,3,6, and 7 as unique elements, hence the
output only lists them out.

Q2. Write a function for zigzag traversal in a binary tree


• Input:
• Output: [1, 3, 2, 4, 5, 6, 8, 7]
• Explanation: Zigzag Traversal rst iterates the given level of the tree from left to
right and then the next level as the right to the level.

Q3. Write a function to sort a linked list of 0s, 1s and 2s


• Input: 0->1->0->2->1->0->2->1
• Output: 0->0->0->1->1->1->2->2
• Explanation: All 0’s will come rst then 1s and then 2s. This can be done in O(n)
time by counting the occurrences of all three and rearranging them in the linked list.

Q4. Write a function to detect cycle in an undirected graph


• Input: n = 4, e = 4 , 0 1, 1 2, 2 3, 3 1
• Output: Yes
• Explanation: The graph is represented as follows in adjacency list representation:
0->1
1->2
2->3
3->1
From the above representation, we can see that there exists a cycle: 1→2→3→1

Q5. Write a function to convert an in x expression to post x expression


• Input: a+b*(c^d)
• Output: abcd^*+

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.

Q7. Write a function to merge two sorted binary search tree


Input:
First BST
7
/ \
5 9
fi
fi
fi
fi
fi
fi
Second BST
4
/ \
3 12
Output: 3 4 5 6 7 9 12

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}}

Q9. Write a function to nd number of subarrays with product less than K


• Input: arr = [1, 6, 2, 3, 2, 1], k = 12
• Output: 11

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.

Q11. Write a function to implement Quicksort on Doubly Linked List


• Input: 8<->10<->1<->7<->6
• Output: 1<->6<->7<->8<->10

Q12. Write a function to connect nodes at the same level of a binary tree
Input: 100
/ \
13 15
/ \ \
14 1 20

Output: 100-> NULL


/ \
13 -> 15 -> NULL
/ \ \
14 -> 1 -> 20 -> NULL

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

Q14. Implement LRU(Least Recently Used) Cache

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;
}

Q17. Write Java code to count number of nodes in a binary tree

Q18. Print Left view of any binary trees.


Hint:
• The main idea to solve this problem is to traverse the tree in pre order manner and
pass the level information along with it. If the level is visited for the rst time, then
we store the information of the current node and the current level in the hashmap.
Basically, we are getting the left view by noting the rst node of every level.
• At the end of traversal, we can get the solution by just traversing the map.

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

Q20. Write a topological sort code in java.


Hint:
• Topological sorting is a linear ordering of vertices such that for every directed edge
ij, vertex i comes before j in the ordering.
• Topological sorting is only possible for Directed Acyclic Graph (DAG).

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

Q23. How do you get the matching elements in an integer array?

Hint:
Try implementing nested loop

Q24. Write a code to implement insertion sort.

Q25. Write a code to reverse an array

Q26. Showcase Inheritance with the help of a program

Q27. Write a code to implement a radix sort algorithm.

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.

Q30. Write a code to implement a counting sort algorithm .

Q31. Write a code to add an element at the middle of the linked list.

Q35. Write a code to nd the maximum occurring character in a given String.


fi
fi
fi
Q36. Write a code to nd all the permutations of a string.

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.

Q41. Write a code to print Floyd’s triangle.

Q42. Write a code to nd the highest repeating word from a given le.

Q43. Write a code to merge sort algorithm.

Q44. Write a code to convert a decimal number to binary

Q45. Write a code to implement Depth First Search Algorithm.

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.

Q49. Write a code to check if a given linked list is a palindrome.

Q50. Write a code to nd the third node from the end in a singly linked list.
fi
fi
fi
fi
fi

You might also like