Adobe Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

#include <algorithm>
#include <vector>

using namespace std;

struct node
{
    int val;
    node *left;
    node *right;
};

void insert(node **root, int temp)
{
    if(*root == NULL)
    {
        (*root) = new node;
        (*root) -> val = temp;
        (*root) -> left = NULL;
        (*root) -> right = NULL;
    }
    else if((*root) -> val > temp)
        insert(&(*root)->left, temp);
    else
        insert(&(*root)->right, temp);
}
void inorder(node *root, vector<int> &v)
{
    if(root != NULL)
    {
        inorder(root->left, v);
        v.push_back(root->val);
        inorder(root->right, v);
    }
}
int main()
{
    int n;
    cin >> n;

    struct node *root = NULL, *ptr = NULL;
    int a[100];
    for(int i=0; i<n; i++)
    {
        int temp; cin >> temp;
        a[i] = temp;
        insert(&root, temp);
    }
    vector<int>v;

    inorder(root, v);
    vector<int>::iterator it = v.begin();

    for(int j=0; j<n; j++)
    {

        it = find(v.begin(), v.end(), a[j]);
        if( it == v.end()-1)
        {
            a[j] = -1;
            break;
        }
        it++;
        a[j] = *it;
    }
    for(int k=0; k<n; k++)
        cout << a[k] << " ";
    return 0;
}

- Vamsi July 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Need more modifications, algo is correct though.

- PV July 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong Answer.

- PP July 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a BST while iterating over the array from right to left, maintain the values of greater element while inserting (i.e the point from where the left child is added)

- Ashutosh March 21, 2020 | 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