DS Interview Q

download DS Interview Q

of 63

Transcript of DS Interview Q

  • 7/27/2019 DS Interview Q

    1/63

    Data Structure

    MCA 2014 (Pass out Batch)

    1

  • 7/27/2019 DS Interview Q

    2/63

  • 7/27/2019 DS Interview Q

    3/63

    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

    3

  • 7/27/2019 DS Interview Q

    4/63

  • 7/27/2019 DS Interview Q

    5/63

    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 offactorial, Fibonacci number series are the examples of recursive algorithms.

    5

  • 7/27/2019 DS Interview Q

    6/63

    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.

    6

  • 7/27/2019 DS Interview Q

    7/63

    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.

    7

  • 7/27/2019 DS Interview Q

    8/63

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

    8

  • 7/27/2019 DS Interview Q

    9/63

    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.

    9

  • 7/27/2019 DS Interview Q

    10/63

    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

  • 7/27/2019 DS Interview Q

    11/63

    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

  • 7/27/2019 DS Interview Q

    12/63

    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 elementexists, 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

  • 7/27/2019 DS Interview Q

    13/63

    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

  • 7/27/2019 DS Interview Q

    14/63

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

  • 7/27/2019 DS Interview Q

    15/63

    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 keyand

    number of occurrence as value. At the end of traversal you can find all

    duplicate numbers, for which occurrence is more than one.

    15

    http://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.htmlhttp://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.htmlhttp://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.html
  • 7/27/2019 DS Interview Q

    16/63

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

  • 7/27/2019 DS Interview Q

    17/63

    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

  • 7/27/2019 DS Interview Q

    18/63

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

    2. Assignment

    3. Primitive

    4. Structured

    Ans 3

    18

  • 7/27/2019 DS Interview Q

    19/63

    DS Interview Questions

    The maximum degree of any vertex in a simple graph with n vertices is

    n-1n+1

    2n-1

    n

    N-1

    19

  • 7/27/2019 DS Interview Q

    20/63

    DS Interview Questions

    Which of the following operations is performed more efficiently by

    doubly linked list than by singly linked list?

    1. Deleting a node whose location in given

    2. Searching of an unsorted list for a given item

    3. Inverting a node after the node with given location

    4. Traversing a list to process each node

    ans1

    20

  • 7/27/2019 DS Interview Q

    21/63

    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

  • 7/27/2019 DS Interview Q

    22/63

  • 7/27/2019 DS Interview Q

    23/63

    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

  • 7/27/2019 DS Interview Q

    24/63

    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 theroots left subtree.

    - Otherwise, insert it into the roots right subtree.

    24

  • 7/27/2019 DS Interview Q

    25/63

    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

  • 7/27/2019 DS Interview Q

    26/63

    DS Interview Questions

    Explain quick sort and merge sort algorithms.

    Quicksort 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 greaterthan 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

  • 7/27/2019 DS Interview Q

    27/63

    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

  • 7/27/2019 DS Interview Q

    28/63

    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 elementexists, 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

  • 7/27/2019 DS Interview Q

    29/63

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

  • 7/27/2019 DS Interview Q

    30/63

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

  • 7/27/2019 DS Interview Q

    31/63

    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

  • 7/27/2019 DS Interview Q

    32/63

    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

  • 7/27/2019 DS Interview Q

    33/63

    DS Interview Questions

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

    Backtracking

    33

  • 7/27/2019 DS Interview Q

    34/63

    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

  • 7/27/2019 DS Interview Q

    35/63

    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

  • 7/27/2019 DS Interview Q

    36/63

    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 ResolutionTechniques and the methods used in each of the type?

    Open addressing (closed hashing), The methods used include: Overflowblock.

    Closed addressing (open hashing), The methods used include: Linked list,

    Binary tree.

    36

  • 7/27/2019 DS Interview Q

    37/63

    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 storedin leaf nodes.

    37

  • 7/27/2019 DS Interview Q

    38/63

    DS Interview Questions

    What is the heap?

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

    38

  • 7/27/2019 DS Interview Q

    39/63

    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

  • 7/27/2019 DS Interview Q

    40/63

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

    http://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.htmlhttp://java67.blogspot.com/2013/02/10-examples-of-hashmap-in-java-programming-tutorial.htmlhttp://java67.blogspot.com/2013/02/10-examples-of-hashmap-in-java-programming-tutorial.htmlhttp://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.htmlhttp://javarevisited.blogspot.com/2013/02/how-to-get-key-from-value-in-hashtable.html
  • 7/27/2019 DS Interview Q

    41/63

    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

  • 7/27/2019 DS Interview Q

    42/63

  • 7/27/2019 DS Interview Q

    43/63

    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 -128to +127.

    43

  • 7/27/2019 DS Interview Q

    44/63

    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

  • 7/27/2019 DS Interview Q

    45/63

    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

  • 7/27/2019 DS Interview Q

    46/63

    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

  • 7/27/2019 DS Interview Q

    47/63

    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

  • 7/27/2019 DS Interview Q

    48/63

    DS Interview Questions

    Difference between calloc and malloc ?

    malloc: allocate n bytes

    calloc: allocate m times n bytes initialized to 0

    48

    i i

  • 7/27/2019 DS Interview Q

    49/63

    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 I i Q i

  • 7/27/2019 DS Interview Q

    50/63

    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 I i Q i

  • 7/27/2019 DS Interview Q

    51/63

    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 I i Q i

  • 7/27/2019 DS Interview Q

    52/63

    DS Interview Questions

    Define pre-order traversal?

    i)Process the root node

    ii)Process the left subtree

    iii)Process the right subtree

    52

    DS I t i Q ti

  • 7/27/2019 DS Interview Q

    53/63

    DS Interview Questions

    Define post-order traversal?

    i)Process the left subtree

    ii)Process the right subtree

    iii)Process the root node

    53

    DS I t i Q ti

  • 7/27/2019 DS Interview Q

    54/63

    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

  • 7/27/2019 DS Interview Q

    55/63

    DS I t i Q ti

  • 7/27/2019 DS Interview Q

    56/63

    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 I t i Q ti

  • 7/27/2019 DS Interview Q

    57/63

    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 I t i Q ti

  • 7/27/2019 DS Interview Q

    58/63

    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 I t i Q ti

  • 7/27/2019 DS Interview Q

    59/63

    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

  • 7/27/2019 DS Interview Q

    60/63

    Can a stack be described as a pointer? Explain.

    A stack is represented as a pointer. The reason is that, it hasa 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

  • 7/27/2019 DS Interview Q

    61/63

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

    one pass?

    This is another frequently asked linked list interviewquestion. 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

    http://javarevisited.blogspot.sg/2012/12/how-to-find-middle-element-of-linked-list-one-pass.htmlhttp://javarevisited.blogspot.sg/2012/12/how-to-find-middle-element-of-linked-list-one-pass.htmlhttp://javarevisited.blogspot.sg/2012/12/how-to-find-middle-element-of-linked-list-one-pass.htmlhttp://javarevisited.blogspot.sg/2012/12/how-to-find-middle-element-of-linked-list-one-pass.htmlhttp://javarevisited.blogspot.sg/2012/12/how-to-find-middle-element-of-linked-list-one-pass.html
  • 7/27/2019 DS Interview Q

    62/63

    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

  • 7/27/2019 DS Interview Q

    63/63

    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