Ooyala Interview Question: Given an object hierarchy (li... | Glassdoor

Interview Question

Software Engineering Interview Mountain View, CA

Given an object hierarchy (like in JavaScript), write a

  function that returns the depth of an object in this tree which is passed to the function.

Interview Answer

2 Answers


Perfect syntax was not required

Interview Candidate on Dec 30, 2010

Depth of an element in a tree is just this:

    public static int depthOfX(SeraTree t, int x){
        return depthOfX( t, x, 0);
    public static int depthOfX(SeraTree t, int x, int level){
        if ( t == null ) return -1;
        if (t.value == x) return level;
        return Math.max(depthOfX(t.left,x,level), depthOfX(t.right,x,level));

Mario R. on Mar 30, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.