Remember the bit shift operation? « and »? Well if we left shift a number, we are multiplying that number by a power of two. For example, 1 = 0001. If we left shift it by 1, we’ll have 1 « 1 = 0010 = 2 = 1 * 2^1. If we left shift it by 2, we’ll have 1 « 2 = 0100 = 4 = 1 * 2^2. Thus, the general formula is:
[Read More]
Find largest Binary Search Tree in a Binary Tree
Find the largest Binary Search Tree in a given Binary Tree.
…Solution will be posted soon.
Update : Solution
Traverse the tree. From each node to the parent, return the following
set of values.
- If BST, size of the current BST or -1 if the tree is not.
- Minval & Maxval of the subtree and maxbstsize seen so far (
probably using references)
So in each node check the following:
Convert Binary tree to Binary Search Tree conversion in place
Write code to find nth largest number from a stream of numbers.
Write code to find nth largest number from a stream of numbers.
Solution:
Use a n size min heap.
Create a n-size min-heap. nth largest will be always at the root of the heap
Given a stream of characters, find the kth non-repeated character at any given time.
Given a stream of characters, find the kth non-repeated character at any given time.
Solution:
- Use an array of size 256, and update the count of each characters as arrives
- Use another array of same size, to keep track of order of the characters appeared in the stream, update this stream only if count of that character is 0 in the first array before updating.
When you need to find the kth non-repeating character, start traversing the second array and get the count, You can get the kth character whose count is 1 in the first array
[Read More]
Find all the pythagorean triplets in an array of integer
Given an array a of n integers find all possible Pythagorean triplets from the array.
A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2. this formula is derived from Pythagoras theorem: _“For every right angle triangle with side lengths a, b, and c=> a_2 + b2 = c2“.
Solution:
Conceptually it is similar to Find Non duplicate pairs that sum to S and 3 SUM problem
[Read More]
3 sum problem
Problem Statement: Problem
Given a set S of n integers find all possible subsets(a,b,c) such that a + b + c = 0.
Making more generic:
Given a set S of n integers find all possible subsets(a,b,c) such that a + b + c = T.
Solution Brute force approach is of O(n^3) but we can solve it in O(n^2) by using the approach in 2 sum problem approach.
[Read More]
Maximum value in a Sliding Window
Problem Statement
Question: A long array A[] is given to you. There is a sliding window of size w, which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sliding window moves rightwards by one position.
Example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.
Window position Max \--------------- ----- \[1 3 -1\] -3 5 3 6 7 3 1 \[3 -1 -3\] 5 3 6 7 3 1 3 \[-1 -3 5\] 3 6 7 5 1 3 -1 \[-3 5 3\] 6 7 5 1 3 -1 -3 \[5 3 6\] 7 6 1 3 -1 -3 5 \[3 6 7\] 7 Input: A long array A[], and a window width w
[Read More]