Interview Questions and Answers for 'Algor' - 61 question(s) found - Order By Newest Basic and Very Frequently asked. Ans. Polymorphism means the condition of occurring in several different forms.
Polymorphism in Java is achieved in two manners
1. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas
2. Dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding. Sample Code for overloading Sample Code for overriding Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  polymorphism  object oriented programming (oops)  oops concepts  oops concepts Asked in 108 Companies Basic   frequent Try 2 Question(s) TestAdvanced level question frequently asked in US based companies. Recently asked in EMC and Intuit. Q2. 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 : Like Discuss Correct / Improve  java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure Asked in 6 Companies   frequent Try 1 Question(s) Test Q3. In a Linked list with sorted numbers, insert a new numbers while maintaining the sort order. Algorithm
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 Q4. 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 : Like Discuss Correct / Improve  graph traversal  breadth first vs depth first   frequent Q5. When do you get ClassCastException?
or
What is ClassCastException ?
Ans. As we only downcast class in the hierarchy, The ClassCastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   class casting   classcastexception   exception   error Asked in 4 Companies intermediate   frequent Try 2 Question(s) Test Q6. 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 : Like Discuss Correct / Improve  java   collections   search algorithm   search   binary search   at&t intermediate Q7. Explain various Searching and Sorting Algorithms ? Algorithm
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   frequent Q8. 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 {
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 Companies 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 Companies Q10. Write a program for LinkedList, with method to append node and traversing the list ? Algorithm
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 Q11. 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 : Like Discuss Correct / Improve  java   algorithm   collections   collections.sort   sorting algorithm expert Try 1 Question(s) Test Q12. How to determine if the linked list has a cycle in it ?
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 Q13. 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 Q14. 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 Q15. 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 Q16. Flatten a Binary Tree to Linked List Algorithm
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 Q17. 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 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 Q19. 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 Q20. 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 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 Q22. Write code to check if a Binary tree is symmetrical 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  binary tree Asked in 1 Companies Q23. 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 Q24. 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 Q25. Check if tic tac toe has a winner Algorithm
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 Q26. Write program to balance a binary tree 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   Q27. 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 : Like Discuss Correct / Improve   Asked in 1 Companies Q28. Explain different sorting algorithms and Big O of each Algorithm
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 Q29. Write a program to find loop in a linked list Algorithm
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 Q30. How to convert a bst to doubly linked list 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