Interview Question

  • -
    0
    of 0 votes
    1
    Answer

    How to find a place to insert new element effictively in a max heap implemented using tree and we dont know the index or no of element in tree.

    - ninja on July 25, 2012 in India Report Duplicate | Flag
    Algorithm

Country: India


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

//O(n) approach
1> count the no. of node in  Max Heap.
2> Increase the Size by one and find the binary representation of  Size.
3> Ignore  the MSB bit and start from MSB+1 bit .
    3.1> If  0 come then move to left otherwise right.
     3.2> Simultaneously you can find  if  (root->data > new_data) then swap the content
4>This will go  continue  till you reach the LSB of Binary Representation.
5>And That is The place where new node shoud be inserted

- Anonymous on July 26, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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