Algorithm - Interview Questions and Answers for 'Algorithm' - 59 question(s) found - Order By Rating Q1. What is the logic behind the shuffle in Music player ?
or
Explain Music player shuffle algorithm. Algorithm
This question was recently asked at 'ASG Sweden'.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 Q2. What are the disadvantages of binary search trees? Algorithm
This question was recently asked at '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  binary search tree Asked in 1 Companies Q3. Write a Program to print all nodes of the data structure ? Algorithm
This question was recently asked at 'Overstock.com'.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  algorithm Asked in 1 Companies Q4. Write a Program to find depth of node binary tree ? Algorithm
Ans. public static int getLevel(Node root, int key) {
int levelOfNode = getLevelUtil(root, key, 1);
return levelOfNode;
}
public static int getLevelUtil(Node root, int key, int level) {
if (root == null) return 0;
if (root.data == key) return level;
int left = getLevelUtil(root.left, key, level 1);
int right = getLevelUtil(root.right, key, level 1);
if (left != 0) return left;
else return right;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q5. How to calculate distance between 2 points using latitude longitude ? Algorithm
This question was recently asked at 'Trimble'.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 Q6. Explain In place sorting algorithm ? Algorithm
This question was recently asked at 'Reddit,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  sorting algorithm   in place sorting algorithm Asked in 2 Companies Q7. Given a max-heap represented as an array, Return the n th largest element without modifying the heap. 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   Q8. What is the ideal situation / case for using heap sort ? 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  Ans. https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  array  sorting Asked in 16 Companies basic   frequent Q10. What is the Big O notation for Fibonacci series - both iterative as well as recursive ? Algorithm
This question was recently asked at 'Canopy Tax'.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  Big o Notation   Asked in 1 Companies expert Q11. What is the difference between dynamic programming and the brute force algorithm? 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   Q12. what is parallel array sorting? Algorithm
Ans. Java provides a new additional feature in Array class which is used to sort array elements in parallel.New methods have been added to the java.util.Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.
The methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q13. Which data structure would you use to implement LRU cache and Why ? Can you tell me the time complexity for it ? Algorithm
Ans. We can use LinkedHashMap as it will give time complexity of O(1) for lookup and We can maintain the insertion order to identify the Last used elements for eviction. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  LRU Cache Asked in 1 Companies Q14. What is the difference between time and space complexity ? 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   Q15. Given a sorted array, write an algorithm to create 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   Q16. Write a method that returns maximum depth of a tree ? Algorithm
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  tree  data structure  maximum depth of tree Asked in 1 Companies Q17. 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 | isEmpty 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  Data Structures Q18. 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) time 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  Data Stricture   Big O Notation  time complexity Q19. Describe how you could use a single array to implement three stacks 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  Data Structure Q20. Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. 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   Q21. What is the runtime complexity of appending strings to string object and Why ? Algorithm
Ans. O(n^2) because each time a string is appended a new String is created and it runs through all the letters to copy them over. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q22. Write code to see if a Linked List has cycle in it ?
or
Write code using Floyd Algorithm to see if a linked list has cycle in it ? What is it's comlexity ? Algorithm
Ans. public class LinkedList {
Node start = null;
Node head = null;
class Node {
Integer body;
Node nextNode;
Node(Integer value) {
body = value;
}
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addNodeToEnd(5);
ll.addNodeToEnd(10);
ll.addNodeToEnd(15);
ll.traverse();
if(checkIfLoop(l1)){
System.out.println("There is a Loop");
} else {
System.out.println("No Loop");
}
}
private boolean checkifLoop(Test l1) {
Node slow = start;
Node fast = start;
Node faster = start;
while(slow != null ) {
fast = slow.nextNode;
faster = fast.nextNode;
if(slow == fast || slow == faster) {
return true;
}
slow = slow.nextNode;
}
return false;
}
}
Complexity of this algorithm is O(n) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  LinkedList  Cyclic LinkedList Q23. Given a linked list with 0 to n node references , Flaten the Linked list Algorithm
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 Q24. Find all paths of length L in an acyclic graph 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   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 Q26. Find Max from Stack in O(1) complexity Algorithm
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 Q27. 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 Q28. 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 Q29. 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 Q30. What will happen in a graph traversal if we don't have a check for cycle Algorithm
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