DS Interview Question

Insert element/node to sorted singly linked list in java (example/ algorithm)

Given the sorted linked list, we need to insert the element or node in the linked list such that linked list remains sorted. Suppose the sorted linked list as shown in Fig 1, We need to find the proper place before we need to insert the new element in the linked list, so that sorted order remains intact.

Find Intersection / join point of two single linked lists in java (example)

Given the two linked list, they are intersected at some node. We need to find out the intersection point of these two linked list. We have shown two linked list Linked list 1 and linked list 2, both linked lists are intersected at node 50. We need to find out the node 50 as per our problem statement.

Reverse single linked list in java using non recursive algorithm (examples)

Given the linked list, we need to reverse the linked list. The linked list is shown in Fig 1, the head is location at node 1. Each node in a list have reference to next node in the list. The node 1 will have reference to node 2 and node 2 will have reference to node 3 and so on. The linked list is null terminated or marker for end of linked list.

Check leaf nodes of binary tree are at same level – BFS / example

What is level in binary tree?
Level is distance or hops of node with respect to root node. The root node is considered to be at level 0, so children of root node will be at level 1 (level of root node + 1). So, we can calculate the level of all the nodes in binary tree.
As an example, consider the tree:

Find ceil value of input data in binary search tree in java (DFS/Example)

Given the binary search tree, we need to find the Ceil value of given input data.
What is Ceil value ? The smallest value just greater than or equal to given input data. We have already discussed about Floor of input data in BST. The logical flow of ceil value in BST is exactly same as that floor value. Let us find Ceil value for BST shown in Fig 1.

Connect nodes at same level (set next sibling)-binary tree DFS/example

Given a binary tree, we need to connect the nodes lying at same level. We have already discussed about the tree traversal using breadth first search algorithm, where we are printing the nodes level by level. By the end of this article we will achieve the same result using next sibling reference. In binary tree, we generally have following information.

Check binary tree is Quasi-Isomorphic of other tree in java (DFS / Example)

We are given two binary tree, we need to find out whether one binary tree is Quasi Isomorphic of other binary tree. Quasi Isomorphic tree? Trees are considered to Quasi isomorphic where we concerned about the structures. The structure of both tree either should be identical or obtained by swapping (or Mirror) its children.

Check given binary trees are Isomorphic in java (recursive / examples)

We are given two binary tree, we need to find out whether one binary tree is Isomorphic of other binary tree. What is Isomorphic meaning? As per English dictionary meaning is “being of identical or similar form, shape, or structure”. By apply above definition to our problem statement, If given binary have identical or similar form or shape or structure to another binary tree, we can say that trees are isomorphic.

Find InOrder predecessor in binary search tree (BST) in java (examples)

Predecessor of any node is the previous node of binary traversal. Let us take an example to understand the inorder predecessor in binary search tree. We have already discussed about the inorder successor. The logical flow of inorder predecessor and inorder successor is same. If we understand any one of them, other can be solved in similar manner.

Delete all nodes of a binary tree in java (recursive/ DFS/ example)

To delete binary tree, we have to delete all the nodes in the tree. We have discussed about deleting the node in binary search tree in separate post. Before we discuss about deleting tree, we will start from very small tree, then we will use this foundation to delete the whole tree. Suppose we have tree of 3 nodes

Scroll to Top