Search Interview Questions  More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. 

 
Data Structure  Interview Questions and Answers for 'Data structure'  74 question(s) found  Order By Newest  
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.  
_{}
 
Ans. Underlying data structure for ArrayList is Array whereas LinkedList is the linked list and hence have following differences  1. ArrayList needs continuous memory locations and hence need to be moved to a bigger space if new elements are to be added to a filled array which is not required for LinkedList. 2. Removal and Insertion at specific place in ArrayList requires moving all elements and hence leads to O(n) insertions and removal whereas its constant O(1) for LinkedList. 3. Random access using index in ArrayList is faster than LinkedList which requires traversing the complete list through references. 4. Though Linear Search takes Similar Time for both, Binary Search using LinkedList requires creating new Model called Binary Search Tree which is slower but offers constant time insertion and deletion. 5. For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster.  
Sample Code for ArrayList Sample Code for LinkedList  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve collections java data structures arraylist linkedlist Asked in 61 Companies Basic frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
Try 1 Question(s) Test  
Frequently asked.  
_{}
 
Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements. LinkedList allows for constanttime insertions or removals using iterators, but only sequential access of elements 1. Retrieval  If Elements are to be retrieved sequentially only, Linked List is preferred. 2. Insertion  If new Elements are to be inserted in between other elements , Linked List is preferred. 3. Search  Binary Search and other optimized way of searching is not possible on Linked List. 4. Sorting  Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list. 5. Adding Elements  If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don't need consecutive memory location.  
Sample Code for ArrayList Sample Code for LinkedList  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java collections list arraylist linkedlist difference between architecture data structure ebay Asked in 2 Companies basic frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
Try 2 Question(s) Test  
Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit.  
_{}
 
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 (keyaa , value (hashmap(keyaaa,value) b ( key ) > hashmap (keyba , value (hashmap(keybaa,value) .................................................................................... z( key ) > hashmap (keyza , value (hashmap(keyzaa,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 frequentCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
Try 1 Question(s) Test  
_{}
 
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 AlgorithmCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. First Find the number of nodes in the linked list and take it as n.then find the data at n3 element.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve linkedlist Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
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 AlgorithmCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
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 deleteNode(Integer value) { head = start; while (head.nextNode != null) { if(head.nextNode.body == value){ head.nextNode = head.nextNode.nextNode; } 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.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); ll.deleteNode(10); ll.traverse(); } }  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve linkedlist delete node from linkedlistCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Data type: a set of values together with operations on that type Data structure: a physical implementation of a data type  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java oops data structure data typeCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
 
Ans. Class can be better seen as Data Type. This could be implemented as a Data Structure too in some cases. One thing worth understanding here is that Data type and Data structure are conceptual things. Class could be implementation of either of these.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve java oops class data type data structureCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
 
Ans. http://stackoverflow.com/questions/494830/howtodetermineifalinkedlisthasacycleusingonlytwomemorylocations  
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 ebayCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. BlockingQueue is a Queue that supports operations that wait for the queue to become nonempty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve blockingqueue collections java concurrent collections Asked in 10 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
 
Ans. [ Open Ended question ]  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve data structures open ended questions load balancerCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Array List works on Array and when we add an element in middle of the list, Array List need to update the index of all subsequent elements. I the capacity is full, it even may need to move the whole list to a new memory location . Linked List works on Double linked list algorithm and all it has to do is to adjust the address of the previous and next elements.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve arraylist vs linkedlist collections list Asked in 2 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 Full Text Search Data Structure Asked in 1 Companies expertCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Myntra,Compro Technologies'.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 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. https://qr.ae/TWK2Ok  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve circular queue priority queue queue data structures collectionsCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
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 basicCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Caprus IT'.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 delete a node from linked list Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 Search Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 linked listCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. We can merge two sorted array by using quick sort algorithm  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve arrays merge sorted arrays Asked in 2 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'Bind Software Innovations'.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 oding cod Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{This question was recently asked at 'MarkMonitor'.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 hashtable Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve data structure stack vs queue stack queue BasicCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. Graph contain cycles whereas Trees cannot.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve graph trees data structures tree vs graphCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 StructureCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 StructureCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 StructuresCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
Ans. pop method pulls the top element from the stack and then move the top to the next element whereas peek only get the top element but doesn't move the top.  
Help us improve. Please let us know the company, where you were asked this question :  
_{ Like Discuss Correct / Improve stackCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  
_{}
 
^{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 Tree Data structures Asked in 1 CompaniesCorrectionDuplicate of Another QuestionCompany where this question was AskedSuggestion}  