Salesforce Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public void bfs() {
        // BFS uses Queue data structure
        Queue queue = new LinkedList();
        queue.add(this.rootNode);
        System.out.println(this.rootNode);
        visited.put(rootNode, true);
        while (!queue.isEmpty()) {
            Node node = (Node) queue.remove();
            Node child = null;
            while ((child = getUnvisitedChildNode(node)) != null) {
                visited.put(child, true);
                System.out.println(child);
                queue.add(child);
            }
        }
        // Clear visited property of nodes
        clearNodes();
    }

    public void bfsRecursive(Queue queue) {
        // BFS uses Queue data structure
        if (queue.isEmpty()) return;
        Node node = (Node) queue.remove();
        System.out.println(node);
        visited.put(node, true);
        for (Node child: node.child) {
            queue.add(child);
        }
        bfsRecursive(queue);
    }

    public void dfs() {
        // DFS uses Stack data structure
        Stack stack = new Stack();
        stack.push(this.rootNode);
        visited.put(rootNode, true);
        System.out.println(rootNode);
        while (!stack.isEmpty()) {
            Node node = (Node) stack.peek();
            Node child = getUnvisitedChildNode(node);
            if (child != null) {
                visited.put(child, true);
                System.out.println(child);
                stack.push(child);
            } else {
                stack.pop();
            }
        }
        clearNodes();
    }

    public void dfsRecursive(Stack stack) {
        // DFS uses Stack data structure
        if (stack.empty()) return;
        Node node = (Node) stack.peek();
        if (visited.get(node)==null) System.out.println(node);
        visited.put(node, true);
        Node child = getUnvisitedChildNode(node);
        if (child != null) {
            stack.push(child);
        } else {
            stack.pop();
        }
        dfsRecursive(stack);
    }

- george.maggessy April 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no need of visited stuff.Its a tree.it doesn't have cycles

- Klaus July 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even though recursive solution should be avoided as much as possible since for large tree they are bound fail with stack overflow, here is the pseudo code for recursive solutions:

dfs_recursive (Node n){
	if n==null
	   return;
	end-if

	if n has children
	   for each entry c in n->children
		dfs_recursive(c);
	   end-for
	end-if
	print n; //post order DFS
}
	

bfs_pre_recursive {
    Q <-- initialize a queue
    Node marker <-- initialize a node
    Q.enqueue(root);
    Q.emqueue(marker);
    bfs_recursive(Q, marker);
}

bfs_recursive (Queue Q, Node MARKER) {
     while true
         Node n=Q.dequeue();
         If n is MARKER
		break;
	 end-if

         print n
         if n has children
	     for each entry c in n->children
		 Q.enqueue(c);
	     end-for
	 end-if
     end-while

     if Q is not empty
	Q.enque(MARKER);
	bfs_recursive(Q, MARKER);
     end-if	
}

- buckCherry November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dfs(node n){
visit n;
foreach i child of n{
dfs(i);
}
}

- santosh.b March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void BFS(TNode root){
		Queue<TNode> q = new LinkedList<TNode>();
		q.add(root);
		recursiveBFS(q);
	}
	public static void recursiveBFS(Queue<TNode> q){
		if(q.isEmpty())
			return;
		TNode t = q.poll();
		System.out.println(t.data);
		for(Tnode temp : t.childrenList)
                         q.add(temp);
		recursiveBFS(q);
	}

- Charlie March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for DFS : Use stack
For BFS : user queue

- DashDash April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

class Node{
 int data;
 Node children; // Linked list of children
}
Node DFS(int data, Node root){
 if(root == null)
  return(null);
 if(root.data == data);
  return(root);
 Node child = root.children;
 while(child != null){
  if(child.data == data)
    return(child);
  Node returnVal = DFS(data, child);
  if(returnVal != null)
    return(returnVal);
  child = child.next();
 }
return(null);
}
Node BFS(int data, Node root){
 LinkedList<Node> queue = new LinkedList<Node>();
 queue.addLast(root);
 return(BFSRecursive(data, queue));
}
Node BFSRecursive(data, queue){
 Node node = queue.getFirst();
 if(node != null)
  return(null);
 if(node.data == data)
  return(Node);
 Node child = node.children;
 while(child != null){
  if(child.data == data)
   return(child);
  queue.addLast(child);
  child = child.next();
 }
return(BFSRecursive(data,queue));
}

- CodeSpace October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure why you're using data as input param and checking if a node's value is same as data.

- buckCherry November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

data represents the value we are searching for in the tree. we compare it with the node's data to see if the node contains the data we are looking for and then return if it does.

- CodeSpace November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks CodeSpace for the response.
Unfortunately I've given a vote-down because the algorithm didn't consider a list of children (i.e. Node child = root.children; is incorrect as it should be something like List<Node> children=root.children)

- buckCherry November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@buckCherry
Yes you are correct. The class definition of Node will need to be changed.

- CodeSpace November 07, 2012 | Flag


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