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

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Algorithms For Interviews By Adnan Aziz


   




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

Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit.
  Q1. Can you provide some implementation of a Dictionary having large number of words ? Solution
Admin
info@buggybread.com
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 :   

   Like      Discuss      Correct / Improve     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 are the pre-requisite for the collection to perform Binary Search ?
Admin
info@buggybread.com
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      intermediate


 Q3. 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 {
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     Deloitte


 Q4. 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 :   

   Like      Discuss      Correct / Improve     graph traversal  breadth first vs depth first        frequent


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

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  Algorithm


 Q6. 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;

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 algorithm


 Q7. Which sorting algorithm is used by Collections.sort() in Java ?Core Java
Admin
info@buggybread.com
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      expert

Try 1 Question(s) Test


 Q8. How to determine if the linked list has a cycle in it ?
admin
info@buggybread.com
Ans. http://stackoverflow.com/questions/494830/how-to-determine-if-a-linked-list-has-a-cycle-using-only-two-memory-locations

 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   ebay


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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



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

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          American Express


 Q11. 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 {
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     Overstock.com      intermediate


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

 This question was recently asked at 'Myntra'.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          Myntra


 Q13. 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 :   

   Like      Discuss      Correct / Improve          HeadStrong


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

 This question was recently asked at 'Deloitte'.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     sorting  heap sort     Deloitte        frequent


 Q15. What is a binary tree ?Algorithm2017-01-19 15:38:04

 This question was recently asked at 'Deloitte'.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     Deloitte


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

 This question was recently asked at 'Colgate Palmolive,LDS Church,TPG Axon Capital Management'.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     Algorithm  Sorting Algorithm     Colgate Palmolive  LDS Church  TPG Axon Capital Management      basic        frequent


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

 This question was recently asked at 'General Electric (GE)'.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          General Electric (GE)      intermediate


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

 This question was recently asked at 'Kony Labs'.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     Linkedlist  data structures  algorithm     Kony Labs      basic


 Q19. Explain bubble sort.Algorithm2017-03-03 14:28:51

 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     sorting  bubble sort     Amazon



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



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

 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     Amazon


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

 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          Amazon


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

 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          Amazon


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

 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          Microsoft


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

 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 treeAlgorithm2017-03-03 14:41:06

 This question was recently asked at 'Google'.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          Google


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

 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          Microsoft


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

 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          Microsoft


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

 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          Amazon


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

 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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. 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 :   

   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
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 :   

   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 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 :   

   Like      Discuss      Correct / Improve     graph traversal


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

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  Algorithm


 Q34. 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 :   

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


 Q35. Write a Program for selection sort.2017-07-27 11:59:53

Ans. public class SelectionSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      
      for(int i=0;i<a.length;i++){
         for(int j=0;j<i;j++){
            if(a[j] > a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
            }
         }
      }
      
      for(int i=0;i<a.length;i++){
         System.out.println(a[i]);
      }
   }
}

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

   Like      Discuss      Correct / Improve     sorting algorithm  selection sort


 Q36. Write a program for bubble sort.2017-07-27 12:00:59

Ans. public class BubbleSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      
      for(int i=0;i<a.length-1;i++){
         for(int j=i;j<a.length;j++){
            if(a[j] < a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
            }
         }
      }
      
      for(int i=0;i<a.length;i++){
         System.out.println(a[i]);
      }
   }
}

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

   Like      Discuss      Correct / Improve     sorting algorithm  Bubble sort


 Q37. Binary Search requires that the collection should beAlgorithm
a. Sorted in Ascending Order
b. Sorted in Descending Order
c. Sorted in any Order
d. Unsorted

Ans.c. Sorted in any Order



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: