Interview Question for Software Engineer / Developers


Country: Russia
Interview Type: In-Person




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

I would suggest a DFS. Let me know if you find an error or a better solution

public class NodeCollect {

	public Iterable<Node> getAllNodes(final Node root) {
		List<Node> retVal = new LinkedList<Node>();
		getAllNodes(root, retVal);
		return retVal;
	}

	private void getAllNodes(final Node currentRoot, final List<Node> retVal) {
		if (currentRoot != null) {
			retVal.add(currentRoot);
			if (currentRoot.getChildren() != null) {
				for (Node current : currentRoot.getChildren()) {
					getAllNodes(current, retVal);
				}
			}
		}
	}

	public static class Node {
		private Iterable<Node> _children;

		public Iterable<Node> getChildren() {
			return _children;
		}

		public void setChildren(final Iterable<Node> children) {
			_children = children;
		}
	}
}

- Scavi December 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

firstly, I implemented this variant, but interviewer said that there must be only 1 function, not 2 or more functions that call one another.
And second his restriction: there should be only 1 parameter in the function: Node node.

- zr.roman December 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mhh not sure if I understand the requirement regarding the one function because the second one is private.
But if you really need only 1 method, Akhil wrote a solution which is basically comparable to my solution if you put retVal as instance variable and place the code of the private method into the public method.
The disadvantage of such a solution is that two threads accessing the getAllNodes method in parallel might change the result, no?

- Scavi December 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

right, but multi threading was not the issue of the task.

- zr.roman December 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try recursion method as below:

package com.returnallnodes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Node {

	Iterator<Node> children;

	public static Iterator<Node> getAllNodes(Node root){
		
		List<Node> allNodes=new ArrayList<Node>();
		if(root!=null){
			while(root.children.hasNext()){
				Iterator<Node> childNodes=getAllNodes(root.children.next());
				while(childNodes.hasNext()){
					allNodes.add(childNodes.next());
				}
			}
		}
		return allNodes.iterator();
	}
}

- Akhil December 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there are circular links, the program will go into an infinte loop and run out of memory or hit a stack limit.
You should keep a hash of all visited nodes so that you can avoid nodes that have already been visited.

- kaustabhd@plumgrid.com December 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#.
variant 1 (recursive) (DFS)

private static IEnumerable<Node> Get1( Node node ) {

            var result = new HashSet<Node> { node };

            if ( node.Children == null ) { return result; }

            result.UnionWith( node.Children );

            foreach ( var child in node.Children ) {
                result.UnionWith( Get1( child ) );
            }
            return result;
        }

- zr.roman December 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#.
variant 2 (recursive) (DFS)

private static IEnumerable<Node> Get2( Node node ) {

    var result = new List<Node> { node };

    if ( node.Children == null ) { return result; }

    foreach ( var child in node.Children ) {
        var tmpRes = result;
        result = (List<Node>)new AggFunc( // AggFunc = Func<IEnumerable<Node>>;
            () => { tmpRes.AddRange( Get2( child ) ); return tmpRes; } )();
    }
    return result;
}

- zr.roman December 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#.
variant 3 (recursive) (DFS) (using Linq extension methods)

static private IEnumerable<Node> Get3( Node node ) {
                       
    var result = new List<Node> { node };

    if ( node.Children == null ) { return result; }

    return node.Children.Aggregate(result, (current, child) => 
        (List<Node>)new AggFunc( // AggFunc = Func<IEnumerable<Node>>;
            () => { current.AddRange( Get3( child ) ); return current; } )() );
}

- zr.roman December 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#.
variant 4 (recursive) (DFS) (using Linq extension methods)

static private IEnumerable<Node> Get4( Node node ) {
                       
    var res = new List<Node> { node };
    if ( node.Children == null ) { return res; }
    return node.Children.Aggregate( res, (curr, child) => curr.Concat( Get4( child ) ).ToList() );
}

- zr.roman December 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c#.
variant 5 (iterative) (BFS)

static private IEnumerable<Node> Get5( Node node ) {

    var res = new List<Node>();
    var currList = new List<Node> { node };

    while ( currList.Count > 0 ) {

        var tmpList = new List<Node>();

        for ( int i = 0; i < currList.Count; i++ ) {
            var children = currList[ i ].Children;
            if ( children == null ) { continue; }
            tmpList.AddRange( children );
        }
        res.AddRange( currList );
        currList = tmpList;
    }
    return res;
}

- zr.roman December 05, 2015 | 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