k.87.sharma
BAN USER- 0of 0 votes
AnswersYou have a rabbit who wants to cross a river by jumping over the various rocks in it. All the rocks are in a straight line and the distance between them is also given. Your rabbit can only perform jumps of specific lengths. You have to output the minimum number of jumps required to cross the river (if possible). Rabbits can jump both in forward and backward direction.
- k.87.sharma in India
The number of rocks is M and the number of possible jump lengths is N.
For e.g. M=4 , N=2,
distance between m1-m2= 1
m2-m3= 2
m3-m4 =1
rabbit can perfrom jump of length 3 and 1.
output= 2 (minimum jump to cross the river)| Report Duplicate | Flag | PURGE
Amazon SDE-2 Algorithm - 0of 0 votes
AnswersGiven a ternary string, you have to count the total number of contiguous substrings (contigious set of characters), that you can form from this given string such that they comprise of either only one or two different characters.
- k.87.sharma in United States
Please note that a unique substring will be decided by its starting and ending indices. So, a substring 'ab' with starting and ending indices being 1 and 2 respectively should be considered different from a substring 'ab' with starting or ending indices (or both) other than 1 and 2 respectively.
For example:
input ternary string - aabc
output - 8
The above string comprises of the following substrings that have either one or two of the characters - a, a, b, c, aa, ab, bc and aab. So the final answer is a total of eight substrings.
input ternary string - abc
output - 5
The above string comprises of the following substrings that have either one or two of the characters - a, b, c, ab and bc. So the final answer is a total of five substrings.
input ternary string - baaccb
output - 16
The above string comprises of the following substrings that have either one or two of the characters - b, a, a, c, c, b, aa, cc, ba, ac, cb, baa, aac, acc, ccb and aacc. So the final answer is a total of sixteen substrings| Report Duplicate | Flag | PURGE
Amazon Algorithm - 0of 0 votes
Answer2 classes are given. class A and class B.
- k.87.sharma in United States
class A is having the object of class B and class A's method is calling class B's method. how would you write junit test for class A's method ?
I said that declare class B's object and call B's method but he said in that case class B's method can do something because of which test can fail. You only have to test class A.| Report Duplicate | Flag | PURGE
- 0of 0 votes
Answersint isBST2(struct node* node) {
- k.87.sharma in India
return(isBSTUtil(node, INT_MIN, INT_MAX));
}
/*
Returns true if the given tree is a BST and its
values are >= min and <= max.
*/
int isBSTUtil(struct node* node, int min, int max) { if (node==NULL) return(true);
// false if this node violates the min/max constraint if (node->data<min || node->data>max) return(false);
// otherwise check the subtrees recursively,
// tightening the min or max constraint
return
isBSTUtil(node->left, min, node->data) &&
isBSTUtil(node->right, node->data+1, max)
);
Change the BSTutil function such that if root==null return false.
After changes the code should tell correctly if binary tree is BSt or not.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer - 0of 0 votes
Answersgiven a binary tree and a value. Print all those path which sum up to that value. path need not to start from root but can contain root.
- k.87.sharma in India
for .e.g
_________________5___________________
__________6____________7_____________
_____1_________2
in this tree for sum 9 the path is 1,6,2
for sum 8 the path is 6,2
for sum 20 path is 2,6,5,7| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 0 votes
AnswersThere is a bus stand. We have given arrival time of the buses and halt of every bus. timings can overlap. We have to find minimum no of platform on the bus stand, so that no bus has to wait to occupy platform.
- k.87.sharma in India| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 0 votes
AnswersIn finding a loop in circular link list algorithm can we move the fast pointer by 3x, 4x or greater speed? if yes, when to use which one. How to find after how many cycle both the pointers will meet.
- k.87.sharma in India| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm
if N=3 its not necessary the length of jump would be 4. It can be anything.
- k.87.sharma June 26, 2014