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

# Search Interview Questions

2679 questions in repository.
There are more than 200 unanswered questions.
Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

Algorithm - Interview Questions and Answers for 'Algorithm' - 55 question(s) found - Order By Newest

Q1. Can you provide some implementation of a Dictionary having large number of words ? Solution
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 (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,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 :

java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure     Dell EMC   Intuit   Corporate Brokers  PWC India  Yahoo  Oracle        frequent

Try 1 Question(s) Test

Q2. What is the difference between Graph's Breadth first and Depth First algorithm ?Algorithm2017-07-23 10:12:58
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 :

graph traversal  breadth first vs depth first        frequent

Q3. In a Linked list with sorted numbers, insert a new numbers while maintaining the sort order.Algorithm2017-07-25 14:59:01
Ans.

Node start = null;

class Node{
Integer body;
Node nextNode;

Node(Integer value){
body = value;
}
}

private void insertInMiddle(Integer value){
if(start == null) {
start = new Node(value);
return;
}

Node newNode = new Node(value);

break;
}
}
}

private void traverse(){
}
}

public static void main(String[] args){

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 :

Q4. What are the pre-requisite for the collection to perform Binary Search ?
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 :

java   collections   search algorithm   search   binary search   at&t      intermediate

Q5. Write an algorithm / Java Program to show duplicates in an array of n elements?Algorithm2017-01-19 15:37:26
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 {
}
}

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 :

coding  code     Deloitte  Bosch

Q6. What is a binary tree ?Algorithm2017-01-19 15:38:04
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 :

binary tree     Deloitte

Q7. Write a program for LinkedList, with method to append node and traversing the list ?Algorithm2017-07-25 14:31:33

Node start = null;

class Node {
Integer body;
Node nextNode;

Node(Integer value) {
body = value;
}
}

if (start == null) {
start = new Node(value);
return;
}

}

}

private void traverse() {
}
}

public static void main(String[] args) {

ll.traverse();

}
}

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

Q8. Write a Program for Graph Depth First Traversal using Apache Commons MultiMapAlgorithm2017-07-23 10:01:14
Ans. import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
private static Set<Integer> alreadyVisited = new HashSet();

static{
}

public static void main(String[] args){
ArrayList visited = new ArrayList();

Integer startNode = 1;

}

return;
}
System.out.println(integer);
}
}

}

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

graph traversal  depth first algorithm

Q9. Which sorting algorithm is used by Collections.sort() in Java ?Core Java
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 :

java   algorithm   collections   collections.sort   sorting algorithm      expert

Try 1 Question(s) Test

Do you think these are the Best Java Frameworks ?

 OpenXava SPRING MVC Apache Stripes Check everything that is Best in Java Click Here

Q10. How to determine if the linked list has a cycle in it ?

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

linked list   data structure   algorithm   java   ebay

Q11. Find the repeating number using O(n) time and constant space.Algorithm

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

algorithm   program   code   coding  makemytrip.com

Q12. Explain Travelling SalesMan Problem ?Algorithm2016-11-21 15:21:21

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

American Express

Q13. Write an Algorithm for Graph Traversal ? The Graph has a loop.Algorithm2016-11-30 15:42:32
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 {
System.out.println(element);
}
}

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

graph traversal algorithm  graph traversal algorithm using recursion     Overstock.com      intermediate

Q14. Flatten a Binary Tree to Linked ListAlgorithm2016-12-13 14:49:54

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

Myntra

Q15. Explain various Searching and Sorting Algorithms ?Algorithm2017-01-11 16:05:54
Ans. a

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

Q16. Write an algorithm / java program for Heap Sort ?Algorithm2017-01-19 15:36:18

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

sorting  heap sort     Deloitte        frequent

Q17. Write any sorting algorithm.Algorithm2017-01-25 13:41:50

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

Algorithm  Sorting Algorithm     Colgate Palmolive  LDS Church  TPG Axon Capital Management      basic        frequent

Q18. How to create list that will sort it's elements dynamically?Algorithm2017-02-21 12:47:22

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

General Electric (GE)      intermediate

Q19. Write program to create a linked list and perform different operations on it.Algorithm2017-02-24 14:18:01

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

Linkedlist  data structures  algorithm     Kony Labs      basic

Do you think these are the Best Java Frameworks ?

 OpenXava SPRING MVC Apache Stripes Check everything that is Best in Java Click Here

Q20. Explain bubble sort.Algorithm2017-03-03 14:28:51
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 :

sorting  bubble sort     Amazon

Q21. Write code to check if a Binary tree is symmetricalAlgorithm2017-03-03 14:29:12

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

binary tree     Amazon

Q22. Write a function that returns the depth of a tree.Algorithm2017-03-03 14:38:46

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

Amazon

Q23. Write code to find the node where two linked lists intersect.Algorithm2017-03-03 14:39:39

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

Amazon

Q24. Check if tic tac toe has a winnerAlgorithm2017-03-03 14:40:05

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

Microsoft

Q25. Write program to balance a binary treeAlgorithm2017-03-03 14:40:54

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

Q26. Given a graph, find if it represents a treeAlgorithm2017-03-03 14:41:06

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

Q27. Explain different sorting algorithms and Big O of eachAlgorithm2017-03-03 14:41:45

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

Microsoft

Q28. Write a program to find loop in a linked listAlgorithm2017-03-03 14:42:17

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

Microsoft

Q29. How to convert a bst to doubly linked listAlgorithm2017-03-03 14:42:30

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

Amazon

Do you think these are the Best Java Frameworks ?

 OpenXava SPRING MVC Apache Stripes Check everything that is Best in Java Click Here

Q30. How do we implement a weight round robin algorithm in load balancing ?Algorithm2017-03-19 19:31:41

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

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

1 -> 2
1 -> 3
1 -> 6
2 -> 4
3 -> 5

Algorithm2017-07-23 10:30:20
Ans. 1 -> 2 -> 3 -> 6 -> 4 - > 5

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

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

1 -> 2
1 -> 3
1 -> 6
2 -> 4
3 -> 5
Algorithm2017-07-23 10:30:48
Ans. 1 -> 2 -> 4 -> 3 -> 5 -> 6

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

depth first traversal  graph traversal

Q33. What will happen in a graph traversal if we don't have a check for cycleAlgorithm2017-07-23 10:32:30
Ans. It will result in never ending loop

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

graph traversal

Q34. Describe how you could use a single array to implement three stacksAlgorithm2018-05-24 08:28:08

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

Data Structure

Q35. 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) timeAlgorithm2018-05-24 08:30:09

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

Data Stricture   Big O Notation  time complexity

Q36. 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 | isEmptyAlgorithm2018-05-24 08:33:37

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

Data Structures

Q37. How different is the Search algorithm when we query Cassandra Tables and When we query Oracle Tables ?Cassandra2017-08-18 09:40:40
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 :

cassandra vs oracle  nosql vs relational  search algorithm

Q38. Find Max from Stack in O(1) complexityAlgorithm2018-01-21 17:13:23

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

complexity  stack  data structure     EPAM

Q39. Explain Binary SearchAlgorithm2018-02-02 08:37:31

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

search  binary search     Nagarvision      basic        frequent

Do you think these are the Best Java Frameworks ?

 OpenXava SPRING MVC Apache Stripes Check everything that is Best in Java Click Here

Q40. Find all paths of length L in an acyclic graphAlgorithm2018-02-10 12:04:08

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

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Delivered by FeedBurner

## 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.