Microsoft Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

confusing..

use BST and no recursion, no extra memory is allowed. - your BST will cost O(n) extra space

- Anonymous December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

good question. It comes down to solving inorder without recursion and no extra space.

Threaded binary tree, basically..

- aatish.mandelecha December 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The essence of the answer (assuming no extra memory apart from that used for the BST).

1. Take the first character of the string and make it the root
2. For every nth character, check if it is in the BST
i. If it is in BST, increment the counter.
ii. If it is not, insert it in the BST depending on its ASCII number
3. In the end do an inorder traversal of the BST

The main step is the second step, at worst you would do n searches of O(logn) and n insertions of O(logn) giving you the overall complexity of O(nlogn).

- C December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you are dealing with ASCII number, it not O(lg n), but lg (256) = 8. You search at most 8 times!
Overall < 8n operations.

- kulang December 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry guys...no extra memory while contructing the BST. yes, definitely the BST is extra memory, but no more memory excpet this.

- newlifeseattle December 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do an in place sort using the string (assuming its mutable)... voila

- gus January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assuming 1 variable to hold the count, just one.

the bst will hold all the unique characters in the string.

so we parse the bst ( iterative inorder) and for each character we come across, we check the string for its occurrence and increment the count and spit the count alongside the character and move on to next character and reset the count -> 0

- fragzilla January 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, I agree with aatish.mandelecha, it has to be a threaded binary tree, and when you are inserting a duplicate entry, add it to the left subtree say X so that it will fall to the rightmost node in the right subtree of X. This will make that node immediate predecessor of the duplicate. Now you can print using inorder traversal using threads.

- TripleH January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What I can think of is to add a count variable while declaring the struct for the node of the tree and then construct the binary tree and whenever the current letter from the string is already there in the tree, just increment the count of that node.

Do the inorder Traversal iteratively and print out the count along with the letter

- Anonymous January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What I can think of is to add a count variable while declaring the struct for the node of the tree and then construct the binary tree and whenever the current letter from the string is already there in the tree, just increment the count of that node.

Do the inorder Traversal iteratively and print out the count along with the letter

- Nishant January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we need extra memory besides BST to keep track of the number of ocurrence(correct me if I am wrong). Here is my implementation without using recursion.

void PrintTree(node* root)
{
node* stack[256] = {0};
int top = 0;

if(!root)
return;

node* t = root;

while(t)
{
while(t)
{
stack[top++] = t;
t = t->left;
}

t = stack[--top];
if(a[t->data] > 0)
{
printf("%c", t->data);
printf("%d", a[t->data]);
a[t->data] = 0;
}

while(!t->right && top>0)
{
t = stack[--top];

if(a[t->data] > 0)
{
printf("%c", t->data);
printf("%d", a[t->data]);
a[t->data] = 0;
}
}

if(a[t->data] > 0)
{
printf("%c", t->data);
printf("%d", a[t->data]);
a[t->data] = 0;
}
t = t->right;
}
}

- dullboy May 12, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More