You are on page 1of 63

Data Structure

MCA 2014 (Pass out Batch)

DS Interview Questions
What are the limitations of arrays and how can you overcome the limitations of arrays?

i)Arrays are of fixed size. ii)Data elements are stored in continuous memory locations which may not be available always. iii)Adding and removing of elements is problematic because of shifting the locations.
Limitations of arrays can be solved by using the linked list.

DS Interview Questions
What is data structure?

The logical and mathematical model of a particular organization of data is called data structure. There are two types of data structure Linear Nonlinear
Linear data structure: A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached. Ex: Arrays, Linked Lists Non-Linear data structure: Every data item is attached to several other data items in a way that is specific for reflecting relationships. The data items are not arranged in a sequential structure. Ex: Trees, Graphs

DS Interview Questions
What is an iterative algorithm?

The process of attempting for solving a problem which finds successive approximations for solution, starting from an initial guess. The result of repeated calculations is a sequence of approximate values for the quantities of interest.

DS Interview Questions
What is an recursive algorithm?

Recursive algorithm is a method of simplification that divides the problem into sub-problems of the same nature. The result of one recursion is the input for the next recursion. The repletion is in the self-similar fashion. The algorithm calls itself with smaller input values and obtains the results by simply performing the operations on these smaller values. Generation of factorial, Fibonacci number series are the examples of recursive algorithms.

DS Interview Questions
What is a spanning Tree?

A spanning tree is a tree associated with a network. All the nodes of the graph appear on the tree once. A minimum spanning tree is a spanning tree organized so that the total edge weight between nodes is minimized.

DS Interview Questions
What is precision?

Precision refers the accuracy of the decimal portion of a value. Precision is the number of digits allowed after the decimal point.

DS Interview Questions
What is the difference between a Stack and an Array?

Stack Stack is a dynamic object whose size is constantly changing as items are pushed and popped . Stack may contain different data types. Stack is declared as a structure containing an array to hold the element of the stack, and an integer to indicate the current stack top within the array. Stack is a ordered collection of items. Array Array is an ordered collection of items. Array is a static object. It contains same data types. Array can be home of a stack i.e. array can be declared large enough for maximum size of the stack.

DS Interview Questions
What are the disadvantages array implementations of linked list?

The no of nodes needed cant be predicted when the program is written. The no of nodes declared must remain allocated throughout its execution.

DS Interview Questions
What do you mean by recursive definition?

The definition which defines an object in terms of simpler cases of itself is called recursive definition.

10

DS Interview Questions
Whether Linked List is linear or Non-linear data structure?

According to Access strategies Linked list is a linear one. According to Storage Linked List is a Non-linear one.

11

DS Interview Questions
How would you sort a linked list?

Step 1: Compare the current node in the unsorted list with every element in the rest of the list. If the current element is more than any other element go to step 2 otherwise go to step 3. Step 2: Position the element with higher value after the position of the current element. Compare the next element. Go to step1 if an element exists, else stop the process. Step 3: If the list is already in sorted order, insert the current node at the end of the list. Compare the next element, if any and go to step 1 or quit.

12

DS Interview Questions
What do you mean by free pool?

Pool is a list consisting of unused memory cells which has its own pointer.

13

DS Interview Questions
In an integer array, there is 1 to 100 number, out of one is duplicate, how to find ?

This is a rather simple data structures question, especially for this kind of. In this case you can simply add all numbers stored in array, and total sum should be equal to n(n+1)/2. Now just subtract actual sum to expected sum, and that is your duplicate number. Of course there is a brute force way of checking each number against all other numbers, but that will result in performance of O(n^2) which is not good. By the way this trick will not work if array have multiple duplicates or its not numbers forming arithmetic progression.

14

DS Interview Questions
How do you find duplicates in array if there is more than one duplicate?

This is a way of solving this problem is using a Hashtable or HashMap data structure. You can traverse through array, and store each number as key and number of occurrence as value. At the end of traversal you can find all duplicate numbers, for which occurrence is more than one.

15

DS Interview Questions
How to find middle element of linked list in one pass?

Since many programmer know that, in order to find length of linked list we need to first traverse through linkedlist till we find last node, which is pointing to null, and then in second pass we can find middle element by traversing only half of length. They get confused when interviewer ask him to do same job in one pass. In order to find middle element of linked list in one pass you need to maintain two pointer, one increment at each node while other increments after two nodes at a time, by having this arrangement, when first pointer reaches end, second pointer will point to middle element of linked list.

16

DS Interview Questions
How to find if linked list has loop Or Tell how to check whether a linked list is circular ?

If we maintain two pointers, and we increment one pointer after processing two nodes and other after processing every node, we are likely to find a situation where both the pointers will be pointing to same node. This will only happen if linked list has loop. Create two pointers, each set to the start of the list. Update each as follows:
while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) ??????{ print (\circular\n\); } }

17

DS Interview Questions
An ADT is defined to be a mathematical model of a user-defined type along with the collection of all ____________ operations on that model.

1. 2. 3. 4.

Cardinality Assignment Primitive Structured

Ans 3

18

DS Interview Questions
The maximum degree of any vertex in a simple graph with n vertices is n-1 n+1 2n-1 n

N-1

19

DS Interview Questions
Which of the following operations is performed more efficiently by doubly linked list than by singly linked list?

1. 2. 3. 4.

Deleting a node whose location in given Searching of an unsorted list for a given item Inverting a node after the node with given location Traversing a list to process each node

ans1

20

DS Interview Questions
Does the Minimal Spanning tree of a graph give the shortest distance between any 2 specified nodes?

- No, it doesnt. - It assures that the total weight of the tree is kept to minimum. - It doesn't imply that the distance between any two nodes involved in the minimum-spanning tree is minimum

21

DS Interview Questions
If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

- A heterogeneous linked list contains different data types in its nodes. We can not use ordinary pointer to connect them. - The pointer that we use in such a case is type. void pointer as it is a generic pointer type and capable of storing pointer to any

22

DS Interview Questions
Which data structure is applied when dealing with a recursive function?

- A recursive function is a function that calls itself based on a terminating condition. - It uses stack. - Using LIFO, a call to a recursive function saves the return address. This tells the return address to the calling function after the call terminates.

23

DS Interview Questions
You want to insert a new item in a binary search tree. How would you do it?

- Let us assume that the you want to insert is unique. - First of all, check if the tree is empty. - If it is empty, you can insert the new item in the root node. - If it is not empty, refer to the new items key. - If the data to be entered is smaller than the roots key, insert it into the roots left subtree. - Otherwise, insert it into the roots right subtree.

24

DS Interview Questions
Why is the isEmpty() member method called?

- The isEmpty() member method is called during the dequeue process. It helps in ascertaining if there exists any item in the queue which needs to be removed. - This method is called by the dequeue() method before returning the front element.

25

DS Interview Questions
Explain quick sort and merge sort algorithms. Quick sort employs the divide and conquer concept by dividing the list of elements into two sub elements. The process is as follows: 1. Select an element, pivot, from the list. 2. Rearrange the elements in the list, so that all elements those are less than the pivot are arranged before the pivot and all elements those are greater than the pivot are arranged after the pivot. Now the pivot is in its position. 3. Sort the both sub lists sub list of the elements which are less than the pivot and the list of elements which are more than the pivot recursively. Merge Sort: A comparison based sorting algorithm. The input order is preserved in the sorted output. Merge Sort algorithm is as follows: 1. The length of the list is 0 or 1, and then it is considered as sorted. 2. Other wise, divide the unsorted list into 2 lists each about half the size. 3. Sort each sub list recursively. Implement the step 2 until the two sub lists are sorted. 4. As a final step, combine (merge) both the lists back into one sorted list.
26

DS Interview Questions
Is it possible to insert different type of elements in a stack? How?

Different elements can be inserted into a stack. This is possible by implementing union / structure data type. It is efficient to use union rather than structure, as only one items memory is used at a time.

27

DS Interview Questions
How would you sort a linked list?

Step 1: Compare the current node in the unsorted list with every element in the rest of the list. If the current element is more than any other element go to step 2 otherwise go to step 3. Step 2: Position the element with higher value after the position of the current element. Compare the next element. Go to step1 if an element exists, else stop the process. Step 3: If the list is already in sorted order, insert the current node at the end of the list. Compare the next element, if any and go to step 1 or quit.

28

DS Interview Questions
What is the use of space complexity and time complexity?

The space complexity defines the storage capacity for the input data. It defines the amount of memory that is required to run a program to completion. The complexity like this depends on the size of the input data and the function that is used for the input size 'n'. The time complexity deals with the amount of time required by a program to complete the whole process of execution. The time complexity allows creating optimized code and allowing user to calculate it before writing their own functions. The time complexity can be made such that a program can be optimized on the basis of the chosen method.

29

DS Interview Questions
What is the difference between B tree and Binary search tree?

Binary tree consists of only fixed number of keys and children, whereas B tree consists of variable number of keys and children. Binary tree keys are stored in decreasing order, whereas B tree consists of the keys and children in non-decreasing order. Binary tree doesn't consists of associated child properties whereas B tree consists of keys has an association with all the nodes with the keys that are less than or equal to the preceding key. Binary tree doesn't have minimization factor concept, whereas B tree has the concept of minimization factor where each node has minimum number of allowable children.

30

DS Interview Questions
Minimum number of queues needed to implement the priority queue?

Two. One queue is used for actual storing of data and another for storing priorities.

31

DS Interview Questions
Convert the expression ((A + B) * C - (D - E) ^ (F + G)) to equivalent Prefix and Postfix notations.

Prefix Notation: - * +ABC ^ - DE + FG Postfix Notation: AB + C * DE - FG + ^ -

32

DS Interview Questions
What is the type of the algorithm used in solving the 8 Queens problem?

Backtracking

33

DS Interview Questions
In an AVL tree, at what condition the balancing is to be done?

If the 'pivotal value' (or the 'Height factor') is greater than 1 or less than -1.

34

DS Interview Questions
Classify the Hashing Functions based on the various methods by which the key value is found.

Direct method, Subtraction method, Modulo-Division method, Digit-Extraction method, Mid-Square method, Folding method, Pseudo-random method.

35

DS Interview Questions
What are the types of Collision Resolution Techniques and the methods used in each of the type? What are the types of Collision Resolution Techniques and the methods used in each of the type?

Open addressing (closed hashing), The methods used include: Overflow block. Closed addressing (open hashing), The methods used include: Linked list, Binary tree.

36

DS Interview Questions
In RDBMS, what is the efficient data structure used in the internal storage representation?

B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes searching easier. This corresponds to the records that shall be stored in leaf nodes.

37

DS Interview Questions
What is the heap?

The heap is where malloc(), calloc(), and realloc() get memory.

38

DS Interview Questions
How to reverse String?

public static String reverse(String str) { StringBuilder strBuilder = new StringBuilder(); char[] strChars = str.toCharArray();

for (int i = strChars.length - 1; i >= 0; i--) { strBuilder.append(strChars[i]); }

return strBuilder.toString(); }

39

DS Interview Questions
How do you find duplicates in array if there is more than one duplicate?

Sometime this is asked as follow-up question of earlier datastrucutre interview question, related to finding duplicates in Array. One way of solving this problem is using a Hashtable or HashMap data structure. You can traverse through array, and store each number as key and number of occurrence as value. At the end of traversal you can find all duplicate numbers, for which occurrence is more than one. In Java if a number already exists in HashMap then calling get(index) will return number otherwise it return null. this property can be used to insert or update numbers in HashMap.

40

DS Interview Questions
When can you tell that a memory leak will occur?

A memory leak occurs when a program loses the ability to free a block of dynamically allocated memory.

41

DS Interview Questions
What is the difference between NULL AND VOID pointer?

NULL can be value for pointer type variables. VOID is a type identifier which has not size. NULL and void are not same. Example: void* ptr = NULL;

42

DS Interview Questions
What is impact of signed numbers on the memory?

Sign of the number is the first bit of the storage allocated for that number. So you get one bit less for storing the number. For example if you are storing an 8-bit number, without sign, the range is 0-255. If you decide to store sign you get 7 bits for the number plus one bit for the sign. So the range is -128 to +127.

43

DS Interview Questions
Is Pointer a variable?

Yes, a pointer is a variable and can be used as an element of a structure and as an attribute of a class in some programming languages

44

DS Interview Questions
How memory is reserved using a declaration statement ?

Memory is reserved using data type in the variable declaration. A programming language implementation has predefined sizes for its data types. For example, in C# the declaration int i; will reserve 32 bits for variable i. A pointer declaration reserves memory for the address or the pointer variable, but not for the data that it will point to. The memory for the data pointed by a pointer has to be allocated at runtime. The memory reserved by the compiler for simple variables and for storing pointer address is allocated on the stack, while the memory allocated for pointer referenced data at runtime is allocated on the heap.

45

DS Interview Questions
How do you assign an address to an element of a pointer array ?

We can assign a memory address to an element of a pointer array by using the address operator, which is the ampersand (&), in an assignment statement such as ptemployee[0] = &projects[2];

46

DS Interview Questions
Which file contains the definition of member functions?

Definitions of member functions for the Linked List class are contained in the LinkedList.cpp file.

47

DS Interview Questions
Difference between calloc and malloc ?

malloc: allocate n bytes calloc: allocate m times n bytes initialized to 0

48

DS Interview Questions
What are the major data structures used in the following areas : RDBMS, Network data model & Hierarchical data model. 1. RDBMS Array (i.e. Array of structures) 2. Network data model Graph 3. Hierarchical data model Trees.

49

DS Interview Questions
Define leaf?

In a tree any node which has out degree o is called a terminal node or a leaf.

50

DS Interview Questions
What are the different types of traversing?

The different types of traversing are i)Pre-order traversal-yields prefix from of expression. ii)In-order traversal-yields infix form of expression. iii)Post-order traversal-yields postfix from of expression.

51

DS Interview Questions
Define pre-order traversal?

i)Process the root node ii)Process the left subtree iii)Process the right subtree

52

DS Interview Questions
Define post-order traversal?

i)Process the left subtree ii)Process the right subtree iii)Process the root node

53

DS Interview Questions
What's the major distinction in between Storage structure and file structure and how?

The expression of an specific data structure inside memory of a computer system is termed storage structure in contrast to a storage structure expression in auxiliary memory is normally known as a file structure.

54

DS Interview Questions
What do you mean by: Syntax Error, Logical Error, Run time Error?

Syntax Error-Syntax Error is due to lack of knowledge in a specific language. It is due to somebody does not know how to use the features of a language.We can know the errors at the time of compilation. logical Error-It is due to the poor understanding of the requirement or problem. Run time Error-The exceptions like divide a number by 0,overflow and underflow comes under this.

55

DS Interview Questions
What actions are performed when a function is called?

When a function is called arguments are passed local variables are allocated and initialized transferring control to the function

56

DS Interview Questions
When is a binary search algorithm best applied?

- It is best applied to search a list when the elements are already in order or sorted. - The list here is searched starting in the middle. If that middle value is not the correct one, the lower or the upper half is searched in the similar way.

57

DS Interview Questions
What are the standard ways in which a graph can be traversed?

i. The depth-first traversal ii. The breadth-first traversal:

58

DS Interview Questions
You want to insert a new item in a binary search tree. How would you do it?

- Let us assume that the you want to insert is unique. - First of all, check if the tree is empty. - If it is empty, you can insert the new item in the root node. - If it is not empty, refer to the new items key. - If the data to be entered is smaller than the roots key, insert it into the roots left subtree. - Otherwise, insert it into the roots right subtree.

59

Can a stack be described as a pointer? Explain.

A stack is represented as a pointer. The reason is that, it has a head pointer which points to the top of the stack. The stack operations are performed using the head pointer. Hence, the stack can be described as a pointer.

60

How to find 3rd element from end in a linked list in one pass?

This is another frequently asked linked list interview question. This question is exactly similar to finding middle element of linked list in single pass. If we apply same trick of maintaining two pointers and increment other pointer, when first has moved upto 3rd element, than when first pointer reaches to the end of linked list, second pointer will be pointing to the 3rd element from last in a linked list.

61

DS Interview Questions
If h is any hashing function and is used to hash n keys in to a table of size m, where n<=m, the expected number of collisions involving a particula r key x is : (A)less than 1. (B)less than n. ( c) less than m. (D)less than n/2 Ans:A

62

DS Interview Questions
If h is any hashing function and is used to hash n keys in to a table of size m, where n<=m, the expected number of collisions involving a particula r key x is : (A)less than 1. (B)less than n. ( c) less than m. (D)less than n/2 Ans:A

63

You might also like