Algorithm - Interview Questions and Answers for 'Algorithm' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

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.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

next 30
 Q11. Find the repeating number using O(n) time and constant space.Algorithm
Ans. http://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     algorithm   program   code   coding  makemytrip.com


 Q12. Explain Travelling SalesMan Problem ?Algorithm
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 Companies


 Q13. Write an Algorithm for Graph Traversal ? The Graph has a loop.Algorithm
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      intermediate


 Q14. Flatten a Binary Tree to Linked ListAlgorithm
Ans. https://www.geeksforgeeks.org/flatten-a-binary-tree-into-linked-list/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q15. Write an algorithm / java program for Heap Sort ?Algorithm
Ans. https://www.geeksforgeeks.org/heap-sort/

 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        frequent


 Q16. Write any sorting algorithm.Algorithm
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        frequent


 Q17. How to create list that will sort it's elements dynamically?Algorithm
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      intermediate


 Q18. Write program to create a linked list and perform different operations on it.Algorithm
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      basic


 Q19. Explain bubble sort.Algorithm
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 Companies


 Q20. Write code to check if a Binary tree is symmetricalAlgorithm
 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 Companies


 Q21. Write a function that returns the depth of a tree.Algorithm
 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 Companies


 Q22. Write code to find the node where two linked lists intersect.Algorithm
 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 Companies


 Q23. Check if tic tac toe has a winnerAlgorithm
 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 Companies


 Q24. Write program to balance a binary treeAlgorithm
 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     


 Q25. Given a graph, find if it represents a treeAlgorithm
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 V-1 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 Companies


 Q26. Explain different sorting algorithms and Big O of eachAlgorithm
 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 Companies


 Q27. Write a program to find loop in a linked listAlgorithm
 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 Companies


 Q28. How to convert a bst to doubly linked listAlgorithm
 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 Companies


 Q29. How do we implement a weight round robin algorithm in load balancing ?Algorithm
 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 balancer


 Q30. Given the list of adjacent nodes, What will be the Breadth First path

1 -> 2
1 -> 3
1 -> 6
2 -> 4
3 -> 5
      
Algorithm
Ans. 1 -> 2 -> 3 -> 6 -> 4 - > 5

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     breadth first traversal  graph traversal


 Q31. Given the list of adjacent nodes, What will be the Depth First path

1 -> 2
1 -> 3
1 -> 6
2 -> 4
3 -> 5
Algorithm
Ans. 1 -> 2 -> 4 -> 3 -> 5 -> 6

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     depth first traversal  graph traversal


 Q32. What will happen in a graph traversal if we don't have a check for cycleAlgorithm
Ans. It will result in never ending loop

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     graph traversal


 Q33. Describe how you could use a single array to implement three stacksAlgorithm
 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     Data Structure


 Q34. How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) timeAlgorithm
 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     Data Stricture   Big O Notation  time complexity


 Q35. Write a program to sort a stack in ascending order You should not make any assumptions about how the stack is implemented The following are the only functions that should be used to write this program: push | pop | peek | isEmptyAlgorithm
 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     Data Structures


 Q36. How different is the Search algorithm when we query Cassandra Tables and When we query Oracle Tables ?Cassandra
Ans. Relational Database uses linear search if we don't query using indices. Performance for linear search is O(n). If indices have been used , relational database uses binary search. Performance for binary search is O(Log n).

Casandra uses hash search. Performance for Hash Search is O(1).

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     cassandra vs oracle  nosql vs relational  search algorithm


 Q37. Find Max from Stack in O(1) complexityAlgorithm
Ans. Create one extra field called MAX O(1)

when you an element to the stack check these two condition
1. Stack is empty, then MAX = element
2. Stack is not empty then check if the element is greater than MAX then MAX = element


when getMax fuction is called, then return MAX

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     complexity  stack  data structure     Asked in 1 Companies


 Q38. Explain Binary SearchAlgorithm
 This question was recently asked at 'Nagarvision'.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  binary search     Asked in 1 Companies      basic        frequent


 Q39. Find all paths of length L in an acyclic graphAlgorithm
 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     


 Q40. Given a linked list with 0 to n node references , Flaten the Linked listAlgorithm
 This question was recently asked at 'One Click Retail'.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 Companies


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: