Amazon Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

The tree will look something like this

class Tree {
   private: 
      
        // Tree Structure related.
        List <TreeNode *> _children;
        TreeNode * _parent;
        
        // Locking related.
        bool _locked = false;
        uint _numDescendantsLocked = 0;
};

The IsLocked method will just check the flag;

bool Tree::IsLocked() {
    return _locked;
}

The Lock method would walk up the path and increment the _numDescendantsLocked variable.

Something like

bool Tree::Lock() {
    // Any descendants locked?
    if (_numDescendantsLocked > 0) { return false;}
  
    // Check if any ancestor is locked.  
    Tree *parent = _parent;
    while (parent) {
        if (parent->IsLocked()) {
            return false;
        }
        parent = parent->_parent;
    }
    // Can lock now.
    parent = _parent;
    while(parent) {
        parent->_numDescendantsLocked++;
        parent = parent->_parent;
    }
    _locked = true;
    return true;
}

Unlocked will walk up the path and decrement the _numDescendantsLocked count.

Methods can be written better etc, but you get the idea.

- Anonymous September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So you mean to say you are not using the conventional tree. Rather your tree nodes will have an extra variable which will store the pointer to its parent.

- Anshul Zunke September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First, there is no such thing as a "conventional tree". Second, read the question. It clearly expects you to come up with some structure yourself.

- Anonymous September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is the "n" in o(log n) ??is it size of the tree or the ary of the tree??

- ari September 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Clarification is needed. Does unLock require you to remove locks from ancestors and descendants? isLocked returns false even if ancestor/descendants are locked?

- Anonymous September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No requirement to remove locks from ancestor or descendants
IsLocked - just returns true or false based on the node to checked

- nr September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think above complexities are possible. This can be done only in
IsLock - O(log n)
Lock - O(n)
unLock - O(log n)

- Anonymous September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Islock is possible in O(1)
Considering the general tree data structure with a father field and a locked boolean variable

Lock (node, root)
{
   p=node;
   do{
      while(p!=NULL)
       {  push(p);
          if(islock(p))
            {print cant be lockd and return false}
          p=p->child;
       }
     p=pop(stack);
     p=p->next;
    }while(p!=NULL or not empty stack)
   p=node;
   if(p!=root)
     {
       p=p->father;
        if(islock(p))
            {print cant be lockd and return false}
     }
     node->locked=true;
     return true;
}

O(log n)

- laila September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Initially I locked node 40. And then I want to lock the root node 50, then it should verify all nodes below ROOT right?
                 50
            20        60
         10    30         70
              25  35
                 32  40

- Anonymous September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it should verify all the nodes below Root because the number you locking is the root. The point is the way it is doing it. O(5 * log (n) ) would be the case

- laila September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry... U r right... In that case it depends on the location of the node that needs to be locked. higher the hierarchy of the resource, complexity increases

- laila September 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whats the problem with the counter for the number of locked descendants? In that case, even if you lock 40 before locking 50, you still know that a descendant of 50 is locked.

- Anonymous November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class NodeLockUnlock:
    def __init__(self, d):
        self.val = d
        self.left = self.right = self.parent = None
        self.Count_of_locked_descendants = 0
        self.isLocked = False

    def isLock(self):
        return self.isLocked

    def _increase_lock_count_ancestor(self):
        node = self
        while node:
            node.Count_of_locked_descendants += 1

    def _decrease_lock_count_ancestor(self):
        node = self
        while node:
            node.Count_of_locked_descendants -= 1

    def lock(self):
        cur = self
        while cur and not cur.isLocked:
            cur = cur.parent
        if cur == None and self.Count_of_locked_descendants == 0:
            self.isLocked = True
            self._increase_lock_count_ancestor()
            return True
        return False

    def unLock(self):
        self.isLocked = False
        self._decrease_lock_count_ancestor()
        return True

- sumitgaur.iiita May 12, 2017 | 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