Search Interview Questions  More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. 

 
Algorithm  Interview Questions and Answers for 'Algorithm'  62 question(s) found  Order By Newest  
Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit.  
_{}
 
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search. Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList. Further level up, we can have linked Hashmaps like , hashmap { a ( key ) > hashmap (keyaa , value (hashmap(keyaaa,value) b ( key ) > hashmap (keyba , value (hashmap(keybaa,value) .................................................................................... z( key ) > hashmap (keyza , value (hashmap(keyzaa,value) } upto n levels ( where n is the average size of the word in dictionary.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java collections hashmap binary search search algorithm advanced architecture data structure Asked in 6 Companies frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
Try 1 Question(s) Test  
_{}
 
Ans. In Breadth first algorithm, all the adjacent nodes of the starting node is visited first and then the same rule is followed while moving inwards whereas In Depth first algorithm, all the nodes of a single traversal path are visited first till a cycle or an end is found. For example , given the following entries of adjacent nodes 1,2 1,3 1,6 2,4 2,5 3,6 The Breadth first path would be 1,2,3,6,4,5 and Depth first path would be 1,2,4,5,3,6  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve graph traversal breadth first vs depth first frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. public class LinkedList { Node start = null; Node head = null; class Node{ Integer body; Node nextNode; Node(Integer value){ body = value; } } private void insertInMiddle(Integer value){ head = start; if(start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while(head.body < value){ if(head.nextNode == null  head.nextNode.body >= value){ Node newNode = new Node(value); newNode.nextNode = head.nextNode; head.nextNode = newNode; break; } head = head.nextNode; } } private void traverse(){ head = start; while(head != null){ System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args){ LinkedList ll = new LinkedList(); ll.insertInMiddle(5); ll.insertInMiddle(10); ll.insertInMiddle(15); ll.insertInMiddle(7); ll.traverse(); } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve LinkedList Data structures AlgorithmCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
 
Ans. 1. Collection should have an index for random access. 2. Collection should have ordered elements.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java collections search algorithm search binary search at&t intermediateCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. int duplicateArray[] = { 1, 2, 2, 3, 4, 5, 6, 8, 9} Set unique = new HashSet(); for (int i = 0; i < duplicateArray.length; i) { if (unique.contains(duplicateArray[i])) { System.out.println(duplicateArray[i]); } else { unique.add(duplicateArray[i]); } } Complexity O(n) = nHashSet contains and add has O(n) = 1  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve coding code Asked in 2 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Binary tree is a tree in which each node has up to two children.Tree is a data structure composed of nodes.Each tree has a root node(not necessary in graph theory). The root node has zero or more child nodes.Each child node has zero or more child nodes, and so on.The tree cannot contain cycles.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve binary tree Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } private void addNodeToEnd(Integer value) { if (start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while (head.nextNode != null) { head = head.nextNode; } head.nextNode = new Node(value); } private void traverse() { head = start; while (head != null) { System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve LinkedList Data structures AlgorithmCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class Graph { private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create(); private static Set<Integer> alreadyVisited = new HashSet(); static{ adjacentDirectedNodesMap.put(1, 2); adjacentDirectedNodesMap.put(1, 3); adjacentDirectedNodesMap.put(1, 5); adjacentDirectedNodesMap.put(2, 4); adjacentDirectedNodesMap.put(4, 5); } public static void main(String[] args){ ArrayList visited = new ArrayList(); Integer startNode = 1; displayAdjacentNodes(startNode); } private static void displayAdjacentNodes(Integer integer){ if(alreadyVisited.contains(integer)){ return; } alreadyVisited.add(integer); System.out.println(integer); for(Integer adjacentNodes: adjacentDirectedNodesMap.get(integer)){ displayAdjacentNodes(adjacentNodes); } } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve graph traversal depth first algorithmCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. The sorting algorithm is a modified mergesort. This algorithm offers guaranteed n log(n) performance.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java algorithm collections collections.sort sorting algorithm expertCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
Try 1 Question(s) Test  
 
Ans. http://stackoverflow.com/questions/494830/howtodetermineifalinkedlisthasacycleusingonlytwomemorylocations  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve linked list data structure algorithm java ebayCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. http://www.geeksforgeeks.org/findduplicatesinontimeandconstantextraspace/  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve algorithm program code coding makemytrip.comCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. https://en.wikipedia.org/wiki/Travelling_salesman_problem  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Please not that all such questions can be easily answered through recursion. Simple recursive implementation could be traverse(root); void traverse(Element element){ if(element.hasNext()){ traverse(element.next()); } else { System.out.println(element); } } but this algo / code lead to endless loop if there is a loop in graph traversal. So you can keep a collection to keep track of which elements have laready been traversed static List<Elements> listOfAlreadyTraversedElements = new ArrayList<Elements>(); main(){ traverse(root); } void traverse(Element element){ if(element.hasNext()){ traverse(element.next()); } else { listOfAlreadyTraversedElements.add(element); System.out.println(element); } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve graph traversal algorithm graph traversal algorithm using recursion Asked in 1 Companies intermediateCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. https://www.geeksforgeeks.org/flattenabinarytreeintolinkedlist/  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'HeadStrong,ServiceNow'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Search Algorithm Sorting Algorithm Asked in 2 Companies basic frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. https://www.geeksforgeeks.org/heapsort/  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve sorting heap sort Asked in 1 Companies frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. https://javasearch.buggybread.com/InterviewQuestions/questionSearch.php?searchOption=label'  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Algorithm Sorting Algorithm Asked in 4 Companies basic frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Override the behavior of collection class to sort the list upon each element addition. Though it's not recommended as list are sorting heavy data structures. List<MyType> list = new ArrayList<MyType>() { public boolean add(MyType mt) { super.add(mt); Collections.sort(list, comparator); return true; } };  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 Companies intermediateCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. import java.util.*; class LinkedListSolution{ protected LinkedList list; public LinkedListSolution(){ list = new LinkedList(); } public Object pop() throws NoSuchElementException{ if(list.isEmpty()) throw new NoSuchElementException(); else return list.removeFirst(); } public void push(Object obj){ list.addFirst(obj); } public Object peek() throws NoSuchElementException{ if(list.isEmpty()) throw new NoSuchElementException(); else return list.getFirst(); } public boolean isEmpty(){ return list.isEmpty(); } public String toString(){ return list.toString(); } } class TestStack{ public static void main(String args[]){ LinkedListSolution s = new LinkedListSolution(); s.push("First"); s.push("Second"); s.push("Third"); System.out.println("Top: " s.peek()); s.push("Fourth"); while(!(s.isEmpty())) System.out.println(s.pop()); } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Linkedlist data structures algorithm Asked in 2 Companies basicCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. array is traversed from first element to last element. Here current element is compared with next element. If current element is greater than next element it is swapped.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve sorting bubble sort Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve binary tree Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Microsoft'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve CorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. We can simply find it by checking the criteria of a tree. A tree will not contain a cycle, so if there is any cycle in the graph, it is not a tree. We can check it using another approach, if the graph is connected and it has V1 edges, it could be a tree. Here V is the number of vertices in the graph  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Microsoft,ServiceNow'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 2 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Microsoft'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question is still unanswered. Can you please provide an answer.}  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve load balancing load balancerCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  