Posts

[Adobe]Search for an element in unsorted array

The successive elements of an unsorted array differ by either + 1 or -1. How will u search for an integer k in this array ? eg: 4,5,4,4,5,6,7,8,7 for each element 'n' the next element is always n+1 or n-1.

Maximum sum 2-d subarray

Image
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle . A sub-rectangle is any contiguous sub-array of size or greater located within the whole array. As an example, the maximal sub-rectangle of the array: is in the lower-left-hand corner: and has the sum of 15.

[Amazon]Two unsorted array form same BST

Given two unsorted array, check if both will create the same bst. eg: 2, 1, 4, 0 and 2, 1, 0, 4 will both form same BST. 2 / \ 1 4 / 0 but, 2,1,4,0 and 2,0,1,4 will not.

[AMAZON] Subtree with largest sum

Given a binary tree, every node has a int value, return the root node of subtree with the largest sum up value.

[AMAZON] Vertical sum of nodes

Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7   The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7

[Amazon]Flatten a Linked-List

You are given a singly link-list such that each node of this list is also a head of another link list of the same type. So, suggest an algorithm to flatten the linked-list in single level link-list. struct node { void *data; /* could be anything */ struct node *next; struct node *down; };  

Implement tail utility

How will you implement tail utility in c/c++ without using inbuilt tail command. Note: tail -n <filename> will output the last 'n' lines of the file.

Find the Least Length Path Which Sums to S

Given a BST and a number 'N' there can be several paths from root of the bst to leaves such that sum of data of all the nodes will make number 'N'. Suggest an efficient algorithm to find out the path of least length.

[MS]Form Largest Number from given number

Write a function to make the largest number from the digits of a given number. Example. number: 32441 Output: 44321

Convert a Binary Tree To hold Children Sum Property

Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node. For example, the below tree doesn’t hold the children sum property, convert it to a tree that holds the property. 50 / \ / \ 7 2 / \ /\ / \ / \ 3 5 1 30

Connect nodes of a binery tree at same Level

Write a function to connect all the adjacent nodes at the same level in a binary tree. Structure of the given Binary Tree node is like following. struct node {    int data;    struct node* left;    struct node* right;    struct node* nextRight; } Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node. You can use only constant extra space. Consider this example for your solution: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ 8 9 10 11  nextRight for node with value 9 will be 10 and not NULL.

perfect cubes

It is possible, to find integers greater than 1 that satisfy the "perfect cube" equation a^3 = b^3 + c^3 + d^3 (e.g. a quick calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to find all sets of numbers {a,b,c,d} which satisfy this equation for a <= N.

Euclidean Distance

You are given a multiset of points on the plane with integer coordinates. Find the maximum distance between two points from this multiset.

Pair of Numbers

Let's assume that we have a pair of numbers ( a ,  b ). We can get a new pair ( a  +  b ,  b ) or ( a ,  a  +  b ) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number k , that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n .

Convert to Sum Tree

Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0. For example, the following tree     10 / \ -2 6 / \ / \ 8 -4 7 5   should be changed to 20(4-2+12+6) / \ 4(8-4) 12(7+5) / \ / \ 0 0 0 0

[AMAZON]All combination of balanced parenthesis

Write a function to generate all possible n pairs of balanced parentheses. For example, if n=1 {} for n=2 {}{} {{}}

Playing Card Game

A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number a i , and the bottom contains number b i , then when the player is playing the card, he gets a i points and also gets the opportunity to play additional b i cards. After the playing the card is discarded.The round ends when the counter reaches zero or the player runs out of cards. We want him to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

[AMAZON]Find two nodes which sums equal to 'K'

Given a binary search tree of n nodes, find two nodes whose sum is equal to a given number k in O(n) time and constant space.

Number of distinct decoding

Two people want to send coded messages to each other. One possible encoding scheme is to simply replace each letter with its numerical value in the alphabet and then string all the digits together without spaces. Determine the number of possible ways to decode a message encoded this way. eg: the message encoded as 25114 can be decoded as BEAAD, BEAN, YAD, YAN, ‘YKD’ and ‘BEKD’, so there are 6 different decoding of given string value.

[Inmobi]Maximum sum of increasing sequence

Variation of LIS: Given an integer array, how will you find out the increasing subsequence which gives the largest sum. For example, 50,23,1,67,30 in this subsequence a few possible increasing subsequences are 1)    23,30 2)    23,67 2)    50,67 3)    1,67 4)    1,30 5)    67 6)    30 but 50, 67 gives the maximum sum. How to find it Note: In the above scenario however if we have 128 at index 0 then it will have the max sum although it is not forming longest LIS.