Vdopia Interview Question


Country: United States
Interview Type: Phone Interview




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

Can be done through recursion. Below is the sample code. Haven't compiled it, so pardon if there are any syntax error.

class Treenode {
  private:
     int data_;
     Treenode *left_;
     Treenode *right_;
  public:
     int get_data(void) {return data_;}
     Treenode* get_left(void) {return left_;}
     Treenode* get_right(void) {return right_;}
};

int main() {
  vector<list*> vector_of_lists;
  Treenode *root; //Input the tree root node here
  /*Assuming the hieght of the tree is h*/
  for(int i=0; i<(2*h+1); i++) {
    vector_of_lists[i] = new list<int>;
  }
  print_vertical_list(root, h);
  for(int i=0; i<(2*h+1); i++) {
    list<int>::iterator it;
    for(it = vector_of_lists[i]->begin(); it != vector_of_lists[i]->end(); it++) {
      cout << *it << " ";
    }
    cout << endl;
  }
}


void 
print_vertical_list(TreeNode *root, int position) {
  if(root != NULL) {
    vector_of_lists[position]->push_back(root->get_data());
    print_vertical_list(root->get_left(), postion-1);
    print_vertical_list(root->get_right(), position+1);
  }
}

Time complexity is O(n) and Space complexity is O(n)

- Prakash May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain the logic you have applied ?? This code looks so cluttered.

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is what I thought

Let say he starts from root and index = 0. Reduce index by 1 if he goes to left and increase index by 1 when goes to right. and enter into a hash with index as the key.
Now print the hash taking the min index and traversing to the max index.

- DashDash May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was this a question from a company?

- Gayle L McDowell May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this question was asked by a company, Vdopia in the first technical round of telephonic interview.

- Achilles May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start with level = 0 , for every left traverst decrese the level by -1 and for every right traversal increast the level by +1, taking level as the key insert element into the hasttable

- Anonymous October 28, 2012 | 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