Technical blog and complete tutorial on popular company interview questions with detailed solution and Java program on Data structure, Algorithms, Time and space complexity, Core Java, Advanced Java, Design pattern, Database, Recursion, Backtracking, Binary Tree, Linked list, Stack, Queue, String, Arrays etc asked in companies like Google, Amazon, Microsoft, Facebook, Apple etc. for beginners and professionals.
Monday, 31 October 2016
Sunday, 30 October 2016
Search in a row wise and column wise sorted matrix
Find number in sorted matrix
Given an n x n matrix, where every row and column is sorted in increasing order. Given a number k, how to decide whether this k is in the matrix. OR Search number in a row wise and column wise sorted matrix.
Lets understand the problem statement graphically and it will be more clear,
Given an n x n matrix, where every row and column is sorted in increasing order. Given a number k, how to decide whether this k is in the matrix. OR Search number in a row wise and column wise sorted matrix.
Lets understand the problem statement graphically and it will be more clear,
Friday, 28 October 2016
Find middle element of a linked list
Find middle element of a linked list in Java
Given a singly linked list find middle of the linked list.
Find Nth node from last in a linked list
Lets understand the problem statement graphically and it will be more clear,
Given a singly linked list find middle of the linked list.
Find Nth node from last in a linked list
Lets understand the problem statement graphically and it will be more clear,
Thursday, 27 October 2016
Convert Sorted Linked List to balanced BST
Convert Sorted Linked list to balanced Binary Search Tree
Convert sorted list to binary search tree
Lets simplify the question statement, Given a singly Linked List where elements are sorted in ascending order convert it to a height balanced BST.
A Binary Search Tree is called balanced if the height of left subtree and height of right subtree of Root differ by at most 1.
Lets understand the problem statement graphically and it will be more clear,
Convert sorted list to binary search tree
Lets simplify the question statement, Given a singly Linked List where elements are sorted in ascending order convert it to a height balanced BST.
A Binary Search Tree is called balanced if the height of left subtree and height of right subtree of Root differ by at most 1.
Lets understand the problem statement graphically and it will be more clear,
Monday, 24 October 2016
Sorted Array to Balanced Binary Search Tree (BST)
Convert Sorted Array to Balanced Binary Search Tree
Given a sorted array, create a Balanced Binary Search Tree using array elements. A Binary Search Tree is called Balanced if, the height of left subtree and height of right subtree of Root differ by at most 1.
Lets understand the problem statement graphically and it will be more clear,
Given a sorted array, create a Balanced Binary Search Tree using array elements. A Binary Search Tree is called Balanced if, the height of left subtree and height of right subtree of Root differ by at most 1.
Lets understand the problem statement graphically and it will be more clear,
Saturday, 22 October 2016
Saturday, 15 October 2016
Merge two sorted arrays in Java
Merge two sorted arrays Java
Merge two sorted arrays in java.
Lets simplify problem statement, given two sorted arrays, you need to merge it in one array such that merged array should also be sorted.
Lets understand the problem statement graphically and it will be more clear,
Merge two sorted arrays in java.
Lets simplify problem statement, given two sorted arrays, you need to merge it in one array such that merged array should also be sorted.
Lets understand the problem statement graphically and it will be more clear,
Merge two sorted arrays in Java |
Friday, 14 October 2016
Clone linked list with next and random pointer
Clone linked list with next and random pointer
You are given a Doubly Link List with one pointer of each node pointing to the next node just like in a singly link list. The second pointer however can point to any node in the list and not just the previous node.
Lets understand the problem statement graphically and it will be more clear,
You are given a Doubly Link List with one pointer of each node pointing to the next node just like in a singly link list. The second pointer however can point to any node in the list and not just the previous node.
Lets understand the problem statement graphically and it will be more clear,
Thursday, 13 October 2016
Tuesday, 11 October 2016
Serialize and Deserialize a Binary Tree
Serialize and Deserialize a Binary Tree
Design an algorithm to serialize and deserialize given Binary Tree. Serialization is to store tree in a File/String, so that it can be later restored.
Deserialization is reading tree back from file.
Lets understand the problem statement graphically and it will be more clear,
We need to Serialize given Binary Tree into String(So that we can write it to File or transfer it).
Also, we need to read the Serialized String and Deserialize it back to original Binary Tree.
Design an algorithm to serialize and deserialize given Binary Tree. Serialization is to store tree in a File/String, so that it can be later restored.
Deserialization is reading tree back from file.
Lets understand the problem statement graphically and it will be more clear,
We need to Serialize given Binary Tree into String(So that we can write it to File or transfer it).
Also, we need to read the Serialized String and Deserialize it back to original Binary Tree.
Sunday, 9 October 2016
Trapping Rain Water between Towers
How Much Water Can A Bar Graph Hold
Given an bar graph, encoded as an array of non-negative integers, calculate the units of water that this bar graph can hold.
Let us put in technical terms,
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Lets understand the problem statement graphically and it will be more clear,
Case 1:
Input = [2, 0, 2]
Output = 2
Case 2:
Input = [3, 0, 0, 2, 0, 4]
Output = 10
Case 3:
Input = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output = 6
Given an bar graph, encoded as an array of non-negative integers, calculate the units of water that this bar graph can hold.
Let us put in technical terms,
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Lets understand the problem statement graphically and it will be more clear,
Case 1:
Input = [2, 0, 2]
Output = 2
Case 2:
Input = [3, 0, 0, 2, 0, 4]
Output = 10
Case 3:
Input = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output = 6