dspm

19
Data Structure & Programming Methodology 1. What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data. 2. List out the areas in which data structures are applied extensively? Compiler Design, Operating System, Database Management System, Statistical analysis package, Numerical Analysis, Graphics, Artificial Intelligence, Simulation 3. What are the major data structures used in the following areas : RDBMS, Network data model & Hierarchical data model. RDBMS – Array (i.e. Array of structures) Network data model – Graph Hierarchical data model – Trees 4. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type. 5. What is the data structures used to perform recursion? Stack. Because of its LIFO (Last In First Out) property it remembers its ‘caller’ so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.

Upload: amrendra-singh

Post on 14-Oct-2014

301 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: DSPM

Data Structure & Programming Methodology

1. What is data structure?A data structure is a way of organizing data that considers not only the items stored, but also their

relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data.

2. List out the areas in which data structures are applied extensively? Compiler Design, Operating System, Database Management System, Statistical analysis package, Numerical Analysis, Graphics, Artificial Intelligence, Simulation

3. What are the major data structures used in the following areas : RDBMS, Network data model & Hierarchical data model.

RDBMS – Array (i.e. Array of structures) Network data model – Graph Hierarchical data model – Trees

4. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to

connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.

5. What is the data structures used to perform recursion?

Stack. Because of its LIFO (Last In First Out) property it remembers its ‘caller’ so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.

6. A binary tree with 20 nodes has null branches?21Let us take a tree with 5 nodes (n=5)

Null Branches

Page 2: DSPM

It will have only 6 (ie,5+1) null branches. In general, A binary tree with n nodes has exactly n+1 null nodes.

7. How many different trees are possible with 10 nodes?1014For example, consider a tree with 3 nodes(n=3), it will have the maximum combination of 5 different (ie, 23

- 3 = 5) trees.

i ii iii iv v

In general:If there are n nodes, there exist 2n-n different trees.

8. List out few of the Application of tree data-structure? The manipulation of Arithmetic expression, Symbol Table construction, Syntax analysis.

9. List out few of the applications that make use of Multilinked Structures? Sparse matrix, Index generation.

10. What is the bucket size, when the overlapping and collision occur at same time?One. If there is only one entry possible in the bucket, when the collision occurs, there is no way to

accommodate the colliding value. This results in the overlapping of values.

11. Traverse the given tree using Inorder, Preorder and Postorder traversals.

A

B C

D E F G

H I J

Given tree:

Page 3: DSPM

Inorder : D H B E A F C I G J Preorder: A B D H E C F G I J Postorder: H D E B F I J G C A

12. There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a full binary tree?

15. In general:There are 2n-1 nodes in a full binary tree.By the method of elimination:

Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15.Note:

Full and Complete binary trees are different. All full binary trees are complete binary trees but not vice versa.

13. In the given binary tree, using array you can store the node 4 at which location?

At location 6

1 2 3 - - 4 - - 5

Root LC1 RC1 LC2 RC2 LC3 RC3 LC4 RC4

where LCn means Left Child of node n and RCn means Right Child of node n

14. Sort the given values using Quick Sort?

65 70 75 80 85 60 55 50 45

Sorting takes place from the pivot value, which is the first value of the given elements, this is marked bold. The values at the left pointer and right pointer are indicated using L and R respectively.

65 70L 75 80 85 60 55 50 45R

Since pivot is not yet changed the same process is continued after interchanging the values at L and R positions

1

2 3

4

5

Page 4: DSPM

65 45 75 L 80 85 60 55 50 R 70

65 45 50 80 L 85 60 55 R 75 70

65 45 50 55 85 L 60 R 80 75 70

65 45 50 55 60 R 85 L 80 75 70

When the L and R pointers cross each other the pivot value is interchanged with the value at right pointer. If the pivot is changed it means that the pivot has occupied its original position in the sorted order (shown in bold italics) and hence two different arrays are formed, one from start of the original array to the pivot position-1 and the other from pivot position+1 to end.

60 L 45 50 55 R 65 85 L 80 75 70 R

55 L 45 50 R 60 65 70 R 80 L 75 85

50 L 45 R 55 60 65 70 80 L 75 R 85

In the next pass we get the sorted form of the array.

45 50 55 60 65 70 75 80 85

15. For the given graph, draw the DFS and BFS?

BFS: A X G H P E M Y J

DFS: A X H P E Y M J G

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

A

HX

G P

E

Y

M J

The given graph:

Page 5: DSPM

25. 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…

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

27. Draw the B-tree of order 3 created by inserting the following data arriving in sequence – 92 24 6 7 11 8 22 4 5 16 19 20 78

28. Of the following tree structure, which is, efficient considering space and time complexities?(a) Incomplete Binary Tree(b) Complete Binary Tree (c) Full Binary Tree

(b) Complete Binary Tree. By the method of elimination:

Full binary tree loses its nature when operations of insertions and deletions are done. For incomplete binary trees, extra storage is required and overhead of NULL node checking takes place. So complete binary tree is the better one since the property of complete binary tree is maintained even after operations like additions and deletions are done on it.

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

30. Does the minimum spanning tree of a graph give the shortest distance between any 2 specified nodes?No.Minimal spanning tree assures that the total weight of the tree is kept at its minimum. But it doesn’t mean

that the distance between any two nodes involved in the minimum-spanning tree is minimum.

31. Convert the given graph with weighted edges to minimal spanning tree.

11 -

5 7 19 24

4 - 6 - 8 - 16 - 20 22 78 92

Page 6: DSPM

the equivalent minimal spanning tree is:

32. Which is the simplest file structure?(a) Sequential (b) Indexed (c) Random(a) Sequential

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

34. Draw a binary Tree for the expression :A * B - (C + D) * (P / Q)

1 3

2 4

5410

600

200

400

310

1421

2985

612

-

* *

A B + /

C PD Q

1

2

3

4 5

410 612200

310

Page 7: DSPM

Q 35.Define Arrays?

A.An array is a particular method of storing elements of indexed data. Elements of data are stored sequentially in blocks within the array. Each element is referenced by an index, or subscript.

The index is usually a number used to address an element in the array. For example, if you were storing information about each day in August, you would create an array with an index capable of addressing 31 values -- one for each day of the month

Q36.What is searching?A. The task of searching is one of most frequent operations in computer programming. It also provides anideal ground for application of the data structures so far encountered. There exist several basic variations ofthe theme of searching, and many different algorithms have been developed on this subject. The basicassumption in the following presentations is that the collection of data, among which a given element is tobe searched, is fixed.

Q37. What is Linear SearchA.When no further information is given about the searched data, the obvious approach is to proceedsequentially through the array in order to increase step by step the size of the section, where the desiredelement is known not to exist. This approach is called linear search. There are two conditions whichterminate the search:1. The element is found, i.e. ai = x.2. The entire array has been scanned, and no match was found.

Q38.What is Sorting?A. Sorting is generally understood to be the process of rearranging a given set of objects in a specific order. The purpose of sorting is to facilitate the later search for members of the sorted set. As such it is an almostuniversally performed, fundamental activity. Objects are sorted in telephone books, in income tax files, intables of contents, in libraries, in dictionaries, in warehouses, and almost everywhere that stored objects have to be searched and retrieved

Q39.What is an Ackermann Function?A.The Ackermann Function A is defined for all non-negative integer arguments m and n as follows:A(0, n) = n + 1A(m, 0) = A(m-1, 1) (m > 0)A(m, n) = A(m-1, A(m, n-1)) (m, n > 0)

Q40.What are various operations performed on a link list?A. There are three fundamental operations we need for any database: Insert: add a new record at a given point Delete: remove an old record Search: find a record with a given key

Q41.What is a binary Heap?

A.A binary heap is defined to be a binary tree with a key in each node such that:

1. All leaves are on, at most, two adjacent levels.2. All leaves on the lowest level occur to the left, and all levels except the lowest are completely filled.

3. The key in the root is all its children, and the left and right subtrees are again binary heaps. (This is a recursive definition)

Page 8: DSPM

Q42.What are Binary Search Trees?A.A binary search tree is a binary tree where each node contains a key such that: All keys in the left subtree precede the key in the root. All keys in the right subtree succeed the key in the root. The left and right subtrees of the root are again binary search trees.

Q43. What are AVL Trees?

A.An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1, and the left and right subtrees are again AVL trees.

Q44.What is Linear(Sequential) Search?

A.The simplest algorithm to search a dictionary for a given key is to test successively against each element.This works correctly regardless of the order of the elements in the list. However, in the worst case all elements might have to be tested.

Q45.What is Binary Search?

A.Binary Search is an incredibly powerful technique for searching an ordered list. It is familiar to everyone who uses a telephone book.The basic algorithm is to find the middle element of the list, compare it against the key, decide which half of the list must contain the key, and repeat with that half.

Q46.What are Two requirements to support binary search?

A. Two requirements to support binary search:

1.Random access of the list elements, so we need arrays instead of linked lists.

2.The array must contain elements in sorted order by the search key.

Q47.What is Asymptotic complexity?

A.Asymptotic complexity is a way of expressing the main component of the cost of an algorithm, using idealized units of computational work. Consider, for example, the algorithm for sorting a deck of cards, which proceeds by repeatedly searching through the deck for the lowest card. The asymptotic complexity of this algorithm is the square of the number of cards in the deck.

Q48.What is Big-O Notation?

A.Big-O is the formal method of expressing the upper bound of an algorithm's running time. It's a measure of the longest amount of time it could possibly take for the algorithm to complete.

More formally, for non-negative functions, f(n) and g(n), if there exists an integer n0 and a constant c > 0 such that for all integers n > n0, f(n) ≤ cg(n), then f(n) is Big O of g(n). This is denoted as "f(n) = O(g(n))".

Q49.What is Big-Omega Notation?

A.For non-negative functions, f(n) and g(n), if there exists an integer n0 and a constant c > 0 such that for all integers n > n0, f(n) ≥ cg(n), then f(n) is omega of g(n). This is denoted as "f(n) = Ω(g(n))".

Page 9: DSPM

This is almost the same definition as Big Oh, except that "f(n) ≥ cg(n)", this makes g(n) a lower bound function, instead of an upper bound function. It describes the best that can happen for a given data size.

Q50.What is Theta Notation?

A.For non-negative functions, f(n) and g(n), f(n) is theta of g(n) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). This is denoted as "f(n) = Θ(g(n))".

This is basically saying that the function, f(n) is bounded both from the top and bottom by the same function, g(n).

Q51. What is Little-O Notation?

A.For non-negative functions, f(n) and g(n), f(n) is little o of g(n) if and only if f(n) = O(g(n)), but f(n) ≠ Θ(g(n)). This is denoted as "f(n) = o(g(n))".

This represents a loose bounding version of Big O. g(n) bounds from the top, but it does not bound the bottom.

Q52.Define Little Omega Notation.

A.For non-negative functions, f(n) and g(n), f(n) is little omega of g(n) if and only if f(n) = Ω(g(n)), but f(n) ≠ Θ(g(n)). This is denoted as "f(n) = ω(g(n))".

Much like Little Oh, this is the equivalent for Big Omega. g(n) is a loose lower boundary of the function f(n); it bounds from the bottom, but not from the top.

Q53.What are Advantages Of using Array data structure?

A.

1.Index - Fast access to every element in the array using an index [], not so with linked list where elements in beginning must be traversed to your desired element.

2.Faster - In general, It is faster to access an element in an array than accessing an element in a linked-list.

Q54. What are Advantages Of using Linked List data structure?

A.1. Resize - Can easily resize the link-list by adding elements without affecting the majority of the other elements in the link-list.2. Insertion - Can easily insert an element in the middle of a linked-list, (the element is created and then you code pointers to link this element to the other element(s) in the link-list).

Q55.What ae Stacks?

A. A stack is a basic data structure that is used all throughout programming. The idea is to think of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it.

A stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data

Page 10: DSPM

Q56.Define Queues?

A.A queue is a basic data structure that is used throughout programming. You can think of it as a line in a grocery store. The first one in the line is the first one to be served.Just like a queue.A queue is also called a FIFO (First In First Out) to demonstrate the way it accesses data.

Q57.Define Trees.

A.A tree is a non-empty set, one element of which is designated the root of the tree while the remaining elements are partitioned into non-empty sets each of which is a subtree of the root.

Tree nodes have many useful properties. The depth of a node is the length of the path (or the number of edges) from the root to that node. The height of a node is the longest path from that node to its leaves. The height of a tree is the height of the root.

Q58.Give Preorder,postorder and inorderTraversal of following Tree.

A.

preorder: 50, 30, 20, 40, 90, 100inorder: 20, 30, 40, 50, 90, 100postorder: 20, 40, 30, 100, 90, 50levelorder: 50, 30, 90, 20, 40, 100

Q59.Give an Example of A typical Binary Search Trees.

A.A typical binary search tree is:

Page 11: DSPM

Q60.What are various Applications of Trees?

A.Trees are often used in and of themselves to store data directly, however they are also often used as the underlying implementation for other types of data structures such as [Hash Tables], [Sets and Maps]and other associative containers.

Q61. What is a Heap?A. A heap is an efficient semi-ordered data structure for storing a collection of orderable data.A heap can be thought of as a tree with parent and child.In order for a data structure to be considered a heap, it must satisfy the following condition (heap property):If A and B are elements in the heap and B is a child of A, then key(A) ≤ key(B).

Q62.What are graphs?

A.A graph is a collection of vertices (also called nodes) and edges (also called arcs) that connect these vertices. A graph is a generalization of the tree structure, where instead of a strict parent/child relationship between tree nodes, any kind of complex relationships between the nodes can be represented.

Q63.What is Adjacency Matrix Representation?

A.An adjacency matrix is one of the two common ways to represent a graph. The adjacency matrix shows which nodes are adjacent to one another. Two nodes are adjacent if there is an edge connecting them. In the case of a directed graph, if node j is adjacent to node i, there is an edge from i to j. In other words, if j is adjacent to i, you can get from i to j by traversing one edge. For a given graph with n nodes, the adjacency matrix will have dimensions of

.

Q64.What is Depth-First Search?

A.Start at vertex v, visit its neighbour w, then w's neighbour y and keep going until reach 'a dead end' then iterate back and visit nodes reachable from second last visited vertex and keep applying the same principle.

Q65.What is Breadth-First Search?

A.Breadth first search visits the nodes neighbours and then the univisited neighbours of the neighbours etc. If it starts on vertex a it will go to all vertices that have an edge from a. If some points are not reachable it will have to start another BFS from a new vertex.

Q66.What is a Graph?Informally, a graph is a finite set of dots called vertices (ornodes) connected by links called edges (or arcs). More formally: asimple graph is a (usually finite) set of vertices V and set ofunordered pairs of distinct elements of V called edges.

Page 12: DSPM

Q.67.What is a Connected Graph?A graph is connected if there is a path connecting every pair ofvertices. A graph that is not connected can be divided intoconnected components (disjoint connected subgraphs). For

example, this graph is made of three connected components.

Q68. Define complete Graph?A.A complete graph with n vertices (denoted Kn) is a graph with n vertices in which each vertex is connected to each of the others (with one edge between each pair of vertices).

Q.69.What is a Undirected graph?A. An undirected simple graph G is a tree if it satisfies one (andtherefore all) of the following equivalent conditions:G is connected and has no simple cyclesG has no simple cycles and, if any edge is added to G, thena simple cycle is formedG is connected and, if any edge is removed from G, then itis not connected anymoreAny two vertices in G can be connected by a unique simplepath.Q.70. Define Tree Structure?A.A tree structure is a way of representing the hierarchical natureof a structure in a graphical form. It is named a “tree structure”because the graph looks a bit like a tree, even though the tree isgenerally shown upside down compared with a real tree; that isto say with the root at the top and the leaves at the bottom.

Q71. What are properties of a Tree?

Page 13: DSPM

A. Trees have a number of interesting properties:The root node, i.e., the base node, is an ancestor of all theother nodes.In a tree structure there is one and only one path from anypoint to any other point.

Q72.Define String.

A. In programming contexts, the term string usually refers to asequence of characters.For Example,ABC is a string of three characters. Strings are more prevalent incomputing than is generally realized.In most cases, computer input is in the form of strings (e.g.commands entered at a terminal).

Q73. What are various operations that can be performed on Strings?A.Four string processing operations have achieved reasonablygeneral acceptance: concatenation,identification of substrings,pattern matching, and transformation of strings to replaceidentified substrings by other strings.Concatenation (sometimes called “catenation”) is the process ofappending one string to another to produce a longer string.Q74. What do u mean by Static Allocation ?A.Static allocation means allocation of storage before the programstarts and retention until the end.The locations of objects are basically decided at compile-time,although they might be relocated at load-time. This implies thesizes of the objects must be known then.Using only static allocation is restrictive, as sizes of datastructures can’t be dynamically varied, and procedures cannot berecursive.Q75. What do u mean by Stack Allocation?A.Stack allocation means run-time allocation and deallocation ofstorage in last-in/first-out order.Typically, stack allocation is performed on top of the main stack,but one can have a separate data stack for this purpose as well,as in Forth, or even multiple ones, as in the PostScript®language.Allocation and deallocation are typically fast, since they can bedone simply by adding or subtracting the size of the blockfrom the stack pointer.Q76. What is Heap Allocation?A.Heap (also known as free store, freestore, also known asdynamic allocation).The heap or free store is the memory(2) areamanaged by dynamic allocation.This use of heap is unconnected with the data structure used bythe heapsort algorithm Heap allocation or dynamic allocationmeans run-time allocation and deallocation of storage inarbitrary order.

Q77.What is a Singly-linked list?

A.The simplest kind of linked list is a singly-linked list (or slist for short), which contains a node having two fields, one is information field and another is link field. This link points to the next node in the list, and last node points to a null value

A singly linked list's node is divided into two parts. The first part holds or points to information about the node, and second part holds the address of next node. A singly linked list travels one way.

Page 14: DSPM

Q78.What is a Doubly-linked list?

A.A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

Q79.What is a Circular-linked list?

A.In a circularly-linked list, the first and final nodes are linked together. This can be done for both singly and doubly linked lists. To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. Viewed another way, circularly-linked lists can be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest, and in cases where you have one object in a list and wish to iterate through all other objects in the list in no particular order.

The pointer pointing to the whole list may be called the access pointer.

Q80.What are various Applications of linked lists?

A.Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations.

The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.

Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. This use of linked lists is easily outperformed by other data structures such as self-balancing binary search trees even on small data sets (see the discussion in associative array). However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set.

Q81, What are various Applications of Arrays?

A. Arrays are used to implement mathematical vectors and matrices, as well as other kinds of rectangular tables. In early programming languages, these were often the applications that motivated having arrays.

Because of their performance characteristics, arrays are used to implement other data structures, such as heaps, hash tables, deques, queues, stacks, strings, and VLists.

One or more large arrays are sometimes used to emulate in-program dynamic memory allocation, particularly memory pool allocation. Historically, this has sometimes been the only way to allocate "dynamic memory" portably.

Q82.What si a Deque?A. a deque (short for double-ended queue—usually pronounced deck) is an abstract list type data structure, also called a head-tail linked list, for which elements can only be added to or removed from the front (head) or back (tail).

Q.83.What is a priority Queue?

Page 15: DSPM

A. A priority queue is an abstract data type in computer programming that supports the following three operations:

InsertWithPriority: add an element to the queue with an associated priority GetNext: remove the element from the queue that has the highest priority, and return it (also known as

"PopElement(Off)", or "GetMinimum") PeekAtNext (optional): look at the element with highest priority without removing it

Q.84. What is radix sort?A.What is Radix sort is a sorting algorithm that sorts integers by processing individual digits. Because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers.

Q.85.What is selection sort?

A. Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations.

Q.86.Give the functioning of selection sort algo?

A. The algorithm works as follows:

1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for remainder of the list (starting at the second position)

Q.87.Give the functioning of merge sort algo?

a merge sort works as follows:

1. If the list is of length 0 or 1, then it is already sorted. Otherwise: 2. Divide the unsorted list into two sublists of about half the size. 3. Sort each sublist recursively by re-applying merge sort. 4. Merge the two sublists back into one sorted list.