DashDash
BAN USER- -2of 2 votes
AnswersHow to print a variable 1000 times without using loops and recurssion
- DashDash in India| Report Duplicate | Flag | PURGE
Software Engineer / Developer - 0of 0 votes
AnswersWe have
- DashDash in India for Development
char *p = "abc";
I know we cant do p[0] = 'a'. What is the reason behind it?| Report Duplicate | Flag | PURGE
Software Engineer / Developer C++ - 0of 0 votes
AnswersWhat is the next number in the series
- DashDash in India
2,4,8,16,24...| Report Duplicate | Flag | PURGE
Goldman Sachs Applications Developer Brain Teasers - 0of 0 votes
Answersvoid copystring(char* dest, char *source)
- DashDash
{
while(*source != NULL)
{
*dest = *source;
dest++;
source++;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char input[10] = "hello";
char *dest;
dest = &input[1];
copystring(dest, input);
return 0;
}
What is the output of the program...| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer C++
Here is what I think :-
1) Create a root X (Create a threaded binary tree)
2) Take a bit which represents the direction in which we have to add node
3) If bit is 0 then travel left else travel right
4) for ABC add to the left of X. (ABC right will be pointing to X)
5) DEF will be added to left of ABC since bit is still 0
6) GEH will be added to the right of ABC
7) We have XYZ which will be added to the right with bit set to 1
8) Now we will travel right and add nodes....
Take 2 pointers, move one pointer 2 nodes and other 1 node
In case of 4 nodes
Pointer 1 => pointing to 2 node
Pointer 2 => pointing to 4th node
In case of 5 nodes
Pointer 1 => pointing to 3rd node
Pointer 2 => pointing to NULL.
Point pointer 2 to pointer 1->next
Create 2 suffix tree(1 for inclusion and other for exclusion) with all these inputs in reverse order
eg
Suffix tree input will be
moc. [this is for .com]
moc.tset. [this is for test.com]
ni.oc
Reverse the input and check for its presence in this suffix tree and calculate the length matched as well.
Search for string in suffix tree will be taking O(n) time, n being the length of the input to be searched
Take a hash table where the key and the color as the key.
Now traverse every square on the board and insert this into the hash. If already there then increase the freq in the hash.
Once the value of any hash is 4 then return true
Time Complexity : O(NXN)
Space Complexity : O(NxN)
Here is the explanation of the code
1) Take first element of the preorder and search in the inorder
2) Now left of the elements in the inorder are left of this element and the right ones are to the right of it. Call recursively and pass high and low positions.
for left it will be : low, pos-1
for right it will be : pos+1, high
Tree* Createnode(int *inorder, int *preorder, int index, int high, int low, int length)
{
int element = preorder[index];
int pos = search(element, inorder, low, high);
if(pos >= 0)
{
Tree *temp = new Tree();
temp->data = element;
temp->left = Createnode(inorder, preorder, index+1, pos-1, low, length);
temp->right = Createnode(inorder, preorder, index+1, high, pos+1, length);
return temp;
}
else
{
if(index < length && low <= high)
return Createnode(inorder, preorder, index+1, high, low, length);
else
return NULL;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int inorder[] = {11,3,6,13,9,15,8,14};
int preorder[] = {6,3,11,8,9,13,15,14};
Tree *root = NULL;
root = Createnode(inorder, preorder, 0, 7, 0, 8);
return 0;
}
Hi Anonymous,
I may have missed out this one but the method is not absolutely WRONG, as you said. Did you ever tried this algorithm to find what could be the possible error
If you slightly tweak the algorithm and increment the freq of the node if the element belongs to the left of the node then it will work fine, try it our
You do not do things in your mind and then come up random solutions
k, Adding to the previous approach
After traversing the array we can save the pointer to the last inserted element let say j
Now again create a heap starting with j+1 element
This can be done till the end of the array
The max sum heap will be the one
for 1,2,10,12,8,9,11
one heap is 2,10,12
other will be 8,9,11
therefore 8,9,11
Hi Mayank,
I said min heap, having pointer the max element in the heap
for 1,5,2,8,7,6,11
Initially : 1,5,2 max = 5
next 8 => (remove 1) 2,5,8 max = 8
next 7 => which is less than 8 therefore do nothing
next 6 => which is less than 8 therefore do nothing
next 11 => (remove 2) 5,8,11 max =11
Seq = 5,8,11
An array as any card can be arranged anywhere
- DashDash September 26, 2010