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

# Search Interview Questions

More than 3000 questions in repository.
There are more than 900 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' - 59 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     Asked in 6 Companies        frequent

Try 1 Question(s) Test

 Q2. In a Linked list with sorted numbers, insert a new numbers while maintaining the sort order. Algorithm
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 :

 Q3. What is the difference between Graph's Breadth first and Depth First algorithm ? Algorithm
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

 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. Explain various Searching and Sorting Algorithms ? Algorithm

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

Search Algorithm  Sorting Algorithm     Asked in 2 Companies      basic        frequent

 Q6. Write an algorithm / Java Program to show duplicates in an array of n elements? Algorithm
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     Asked in 2 Companies

 Q7. What is a binary tree ? Algorithm
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     Asked in 1 Companies

 Q8. Write a program for LinkedList, with method to append node and traversing the list ? Algorithm

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 :

 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

 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 ? Algorithm

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

 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 {
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     Asked in 1 Companies      intermediate

 Q14. Flatten a Binary Tree to Linked List Algorithm

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

 Q15. Write an algorithm / java program for Heap Sort ? Algorithm

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

sorting  heap sort     Asked in 1 Companies        frequent

 Q16. Write any sorting algorithm. Algorithm

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

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>() {
Collections.sort(list, comparator);
return true;
}
};

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

 Q18. Write program to create a linked list and perform different operations on it. Algorithm
Ans. import java.util.*;
}
public Object pop() throws NoSuchElementException{
if(list.isEmpty())
throw new NoSuchElementException();
else
return list.removeFirst();
}
public void push(Object 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[]){
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 :

 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 :

sorting  bubble sort     Asked in 1 Companies

 Q20. Write code to check if a Binary tree is symmetrical Algorithm

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

binary tree     Asked in 1 Companies

 Q21. Write a function that returns the depth of a tree. Algorithm

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

 Q22. Write code to find the node where two linked lists intersect. Algorithm

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

 Q23. Check if tic tac toe has a winner Algorithm

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

 Q24. Write program to balance a binary tree Algorithm

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

 Q25. Given a graph, find if it represents a tree Algorithm
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 :

 Q26. Explain different sorting algorithms and Big O of each Algorithm

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

 Q27. Write a program to find loop in a linked list Algorithm

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

 Q28. How to convert a bst to doubly linked list Algorithm

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

 Q29. How do we implement a weight round robin algorithm in load balancing ? Algorithm

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

 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 :