Problem A binary tree is a mirror image of itself if its left and right subtrees are identical mirror images i.e., the binary tree is symmetrical. This is best explained with a few examples.
Example 1 / \ 2 2 ```TRUE 1
/ \
2 2
\
3
1
/ \
2 2
/ \ / \
4 3 3 4
1
/ \
2 2
/ \ / \
[Read More]
Given two strings str1 and str2, write code to find out if all non-unique elements in str2 are in str1?
Problem Given two strings str1 and str2, write code to find out if all non-unique elements in str2 are in str1?
Solution Method 1 - Using hashset on str2 and boolean array or hashset on str2
Break down the problem into two tasks.
i) Finding the non-unique i.e. repeating elements in str 1. Hashing can be used for this purpose.
ii) Finding if all the characters hashed in the first string are available in the second string.
[Read More]
Given a binary tree with extra field "next pointer" in node, populate the next pointer to the nodes inorder successor
Problem : Given a Binary tree where each node has an extra field (node * next), write a function that puts the inorder successor of that node in the next pointer.
This is how our binary tree node looks like :
struct node { int data; struct node\* left; struct node\* right; struct node\* next; } Initially, all next pointers have NULL values. Your function should fill these next pointers so that they point to inorder successor.
[Read More]
Amazon Questions - Set 1
Write code to find Nth last node in a linked list. Solution Given a binary tree build a vertical sum array. Solution Given two strings str1 and str2, write code to find out if all non-unique elements in str2 are in str1? Solution Given two lists of strings return a list of strings that is an intersection of both of the lists.
Analyze running time and space complexity.
Give Test Case scenarios.
[Read More]
Replace all spaces in a string with “%20″ in c-style string
Problem Write a C program that will replace all spaces with ‘%20′
Example
Input: “Mr John Smith "
Output: “Mr%20John%20Smith
Solution The algorithm is as follows:
Count the number of spaces during the first scan of the string. Parse the string again from the end and for each character:
If a space is encountered, store “%20”.
Else, store the character as it is in the newly shifted location.
[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]