Oracle Interview Question for Software Engineer / Developers


Team: Oracle
Country: United States
Interview Type: In-Person




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

Breadth First Search. Use a Queue for nodes that you want to visit next as well as a Set of nodes you have currently seen so that you don't re-visit any nodes. If you find the node, spit out true, otherwise if you search through all the available paths and haven't found the node, spit out false...

public static boolean pathExistFromAToB(Graph graph, Node a, Node b) {
        ArrayDeque<Node> unvisitedNodes = new ArrayDeque<>();
        HashSet<Node> seenNodes = new HashSet<>();
        
        unvisitedNodes.add(a);
        
        while (!unvisitedNodes.isEmpty()) {
            Node focusNode = unvisitedNodes.pollFirst();
            for(Node neighbor : focusNode.neighbors) {
                if( ! seenNodes.contains(neighbor) ) {
                    seenNodes.add(neighbor);
                    unvisitedNodes.add(neighbor);
                    if( neighbor == b ) {
                        return true;
                    }
                }
            } 
        }
        return false;
    }

- Hyland March 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void searchDFS(GNode node, int value)
    {
        Stack<GNode> stack = new Stack<GNode>();
        stack.push(node);
        boolean found = false;
        while(!stack.empty()){
                GNode current = stack.peek();
                if(current.value == value){
                    found  =true;
                    break;
                }else{
                    GNode next = current.getNextNotVisited();
                    if(next == null){
                        current.visited =true;
                        stack.pop();
                    }else{
                        stack.push(next);
                    }
                }
        }
        if(found){
            while(!stack.empty())
                System.out.print(stack.pop().value + " <- ");
        }else{
            System.out.println("No found");
        }
    }

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Depending on how the getNextNotVisited() method. The Complexity should be worse case O(n)

What about the avg? O(n) ??

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The complexity should be O(E) as we will pass through all the edges.

- kr.neerav January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

should be O(n+m) where n is the number of nodes and m is the number of edges, worst case you visit all nodes and edges

- Dave January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are not marking the node as visited when you push it on stack..does't that mean the node would be used again and again, and will end up in stack multiple times ?

- Anonymous January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You make a node visited only when you have visited all Its childrens. when you are pushing to the stack It is on a state like "exploring" or "visiting" but not visited at all.
try it yourself here is the complete code:

//Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
import java.util.*;
public class Graph{
	public static void main(String [] args){
		GNode startPoint = new GNode(100);
		Random random = new Random();
		
		for(int w = 0; w < 5; w ++){
			startPoint.addGNode(random.nextInt(100));
		}
		List<GNode> secondLevel = startPoint.getNodes();
		for(int i = 0; i < secondLevel.size(); i++){
			GNode node = secondLevel.get(i);
			for(int z = 0; z < 5; z ++){
				GNode n = new GNode(random.nextInt(100),node.value);
				node.getNodes().add(n);
				for(int c = 0; c< 5; c++)
				n.getNodes().add(new GNode(random.nextInt(100),n.value));
			}
			
		}
		
		
		
		printBFSGraph(startPoint);
		int target = random.nextInt(100);
		System.out.println("target: " + target	);
		searchDFS(startPoint,target);
	}
	
	public static void printBFSGraph(GNode node){
		Queue<GNode> q = new LinkedList<GNode>();
		q.add(node);
		while(q.peek() != null){
			GNode current = q.poll();
			System.out.println("[ {"+current.parent+"},"+ current.value +" ] ");
			List<GNode> childrens = current.getNodes();
			for(GNode ne : childrens){
				q.add(ne);
			}
		}
	}
	public static void searchDFS(GNode node, int value)
	{
		Stack<GNode> stack = new Stack<GNode>();
		stack.push(node);
		boolean found = false;
		while(!stack.empty()){
				GNode current = stack.peek();
				if(current.value == value){
					found  =true;
					break;
				}else{
					GNode next = current.getNextNotVisited();
					if(next == null){
						current.visited =true;
						stack.pop();
					}else{
						stack.push(next);
					}
				}
		}
		if(found){
			while(!stack.empty())
				System.out.print(stack.pop().value + " <- ");
		}else{
			System.out.println("No found");
		}
	}
	public static void searchBFS(GNode node, int value){
		Queue<GNode> queue = new LinkedList<GNode>();
		queue.add(node);
		while(queue.peek() != null)
		{
			
		}
	}

	static class GNode{
		public int value;
		public int parent;
		public boolean visited;
		public List<GNode> nodes;
		public GNode(int val){
			this.value = val;
			this.parent = -666;
		}
		public GNode(int val,int parent){
			this.value =val;
			this.parent = parent;
		}
		public void addGNode(int value){
			if(nodes == null){
				nodes = new ArrayList<GNode>();
				
			}
			
			nodes.add(new GNode(value,this.value));
		}
		public List<GNode> getNodes(){
			if(nodes == null){
				nodes = new ArrayList<GNode>();
			}
			return nodes;
		}
		public GNode getNextNotVisited(){
			for(GNode node:nodes){
				if(!node.visited)return node;
			}
			return null;
		}
	}
}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please ignore the searchBFS, I'm still working on it. the one that does the actual work is called:

searchDFS(startPoint,target);

Note: Sorry about the mess I did on the main method, I was just trying to create a random Graph. Not the best way to do it, but It works for testing proposes.

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To test whether or not two vertices are connected, both BFS and DFS suffice.

- Murali Mohan January 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The route it is when a can be reached from b and visa versa? So in that case you can find out strongly connected components - substets of the graph where each node u and v are reacheble one from another. And simply check wheter two vertexes belong to tha same SCC.

- glebstepanov1992 January 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Graph {

public void bfs(Node start){

Queue<Node> queue = new LinkedList<Node>();
queue.add(start);

while (queue.isEmpty()){
Node current = queue.poll();

for (Edge e : current.adj){
if (e.node.dest==Node.INFINITY){
e.node.dest = current.dest + 1;
e.node.pre = current;
queue.add(e.node);
}
}
}
}

public boolean isConnected(Node from , Node to){
if (to==null){
return false;
}else{
if (from ==to){
return true;
}else{
if (isConnected(from,to.pre)){
return true;
}
}
return false;
}
}




class Node{

public static final int INFINITY = Integer.MAX_VALUE;

Node pre;

List<Edge> adj;

int dest = INFINITY;

}


class Edge{
Node node;
int cost;
}

}

- Scott January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Graph {
	
	public void bfs(Node start){
		
		Queue<Node> queue = new LinkedList<Node>();
		queue.add(start);
		
		while (queue.isEmpty()){
           Node current = queue.poll();
           
           for (Edge e : current.adj){
        	   if (e.node.dest==Node.INFINITY){
        		   e.node.dest = current.dest + 1;
        		   e.node.pre = current;
        		   queue.add(e.node);
        	   }
           }
		}
	}
	
	public boolean isConnected(Node from , Node to){
		if (to==null){
			return false;
		}else{
			if (from ==to){
				return true;
			}else{			 	
			  if (isConnected(from,to.pre)){
				  return true;
			  }
			}			
			return false;
		}
	}
	
	
	
	
	class Node{
		
		public static final int INFINITY = Integer.MAX_VALUE;
		
		Node pre;
		
		List<Edge> adj;
		
		int dest = INFINITY;
		
	}
	
	
	class Edge{
		Node node;
		int cost;
	}

}

- Scott January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi

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

Yes, you can do either DFS or BFS and you can find if there is a path between a and b.
Only caveat is, this is a directed graph. Suppose graph is B -> A and if you do a BFS/DFS starting with A you won't be able to reach B.
So you have to do DFS/BFS twice, once starting with A and other starting with B.

- ajit@ajitk.in April 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean checkRoute(int startNode, int endNode){
		boolean result = false;
		
		if(startNode == endNode)
			return true;
		
		while(true){
			
			if(dagGraph.get(startNode) == null){
				return false;
			}else{
				Queue<Integer> tempQueue = new LinkedList<Integer>();
				
				tempQueue = dagGraph.get(startNode);
				while(!tempQueue.isEmpty()){
					result = result || checkRoute(tempQueue.poll(),endNode);
				}
			}
			return result;
		}
		
	}

- Sandeep November 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean checkRoute(int startNode, int endNode){
		boolean result = false;
		
		if(startNode == endNode)
			return true;
		
		while(true){
			
			if(dagGraph.get(startNode) == null){
				return false;
			}else{
				Queue<Integer> tempQueue = new LinkedList<Integer>();
				
				tempQueue = dagGraph.get(startNode);
				while(!tempQueue.isEmpty()){
					result = result || checkRoute(tempQueue.poll(),endNode);
				}
			}
			return result;
		}
		
	}

- Sandeep November 15, 2016 | 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