Data Structures Important Short Questions
Data Structures Important Short Questions
Short Questions:
1. Declare a structure of a node of a binary tree?
class Node
{
int key;
Node left, right;
Merging is the process of taking two smaller sorted lists and combining them together
into a single, sorted, new list.
10. Why Dijkstra’s algorithm is used? For which kind of graph this algorithm is not
used?
Dijkstra’s Algorithm is used for solving single source shortest path problems. In this
algorithm, a single node is fixed as a source node and shortest paths from this node to
all other nodes in graph is found.
Dijkstra’s Algorithm cannot be applied on graphs having negative weight function
because calculation of cost to reach a destination node from the source node becomes
complex.
11. If a strictly binary tree has 7 leaves, calculate total number of nodes in the tree?
A strictly binary tree with N leaves always contains 2N – 1 nodes. So,
2 ( 7 ) = 14
12. What is worst time complexity of bubble and insertion sort?
The worst time complexity of bubble and insertion sort is O(N2).
13. How many references need to maintained in stack and why?
14. Define an advantage of doubly linked list over singly linked list?
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be deleted is
given.
15. Write down the structure of a node of doubly linked list?
Algorithm analysis deals with the execution or running time of various operations
involved. The running time of an operation can be defined as the number of computer
instructions executed per operation.
20. What are the criteria of algorithm analysis?
An algorithm must satisfy the following criteria:
1. Input: these are the values that are supplied externally to the algorithm.
2. Output: these are the results that are produced by the algorithm.
3. Definiteness: each step must be clear and unambiguous.
4. Finiteness: the algorithm must terminate after a finite number of step.
5. Effectiveness: each step must be feasible i.e. it should be partially possible to
perform the step.
21. What are the asymptotic notations?
Asymptotic analysis refers to calculate the running time complexity of an algorithm.
Following are the commonly used asymptotic notations:
Ο Notation Ω Notation θ Notation
22. Give some example of greedy algorithm?
An algorithm is designed to achieve optimum solution for a given problem. In greedy
algorithm approach, decisions are made from the given solution domain. As being
greedy, the closest solution that seems to provide an optimum solution is chosen.
Examples
Most networking algorithms use the greedy approach. Here is a list of few of them −
Travelling Salesman Problem
Prim's Minimal Spanning Tree Algorithm
Kruskal's Minimal Spanning Tree Algorithm
Dijkstra's Minimal Spanning Tree Algorithm
Graph - Map Coloring
Graph - Vertex Cover
Knapsack Problem
Job Scheduling Problem
23. What is stack? Why do we use stack?
Stack is a data structure in which access is restricted to the most recently inserted item.
It behaves very much like the common stack of bills, stack of plates, or stack of
newspapers.
we can use the stack to reverse the order of things. What makes the stack useful are the
many applications for which we need to access only the most recently inserted item. An
important use of stacks is in compiler design. A stack can also be used to check for
unbalanced symbols.
24. What is shell sort?
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.
This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the
far right and has to be moved to the far left.
25. How breadth first traversal works?
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and
uses a queue to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.
26. What is an AVL tree?
An AVL tree is a binary search tree with the additional balance property that, for any
node in the tree, the height of the left and right subtrees can differ by at most 1. As usual,
the height of an empty subtree is –1.
27. What is minimum spanning tree (MST)?
The minimum spanning tree is a connected subgraph of G that spans all vertices at
minimum cost. A minimum spanning tree exists only if the subgraph of G is connected.
28. What is interpolation search technique?
Interpolation search is an improved variant of binary search. This search algorithm works
on the probing position of the required value. For this algorithm to work properly, the data
collection should be in a sorted form and equally distributed.
Interpolation search has a better Big-Oh bound on average than does binary search, but
has limited practicality and a bad worst case.
29. What is recursion?
Some computer programming languages allow a module or function to call itself. This
technique is known as recursion. In recursion, a function α either calls itself directly or
calls a function β that in turn calls the original function α. The function α is called
recursive function.
30. What is adjacency list?
An adjacency list (or adjacency structure) for a graph (V, E) is a list that contains one
element for each vertex in the graph and in which each vertex list element contains a list
of the vertices that are adjacent to its vertex. The secondary list for each vertex is called
its edge list.
31. Define full binary tree?
A binary tree is said to be full if all its leaves are at the same level and every interior
node has two children.
32. What is linked list?
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection
to another link. Linked list is the second most-used data structure after array.
33. What operations can be performed on queues?
Here we shall try to understand the basic operations associated with queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
34. What is binary search?
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer. For this algorithm to work
properly, the data collection should be in the sorted form.
35. What is selection sort?
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted part at
the left end and the unsorted part at the right end. Initially, the sorted part is empty and
the unsorted part is the entire list.
36. What is graph?
A graph consists of a set of vertices and a set of edges that connect the vertices. That is,
G = (V, E), where V is the set of vertices and E is the set of edges.
37. What is tree?
A tree is a nonlinear data structure that models a hierarchical organization. Tree
represents the nodes connected by edges. A tree is a pair (r, S), where r is a node and S
is a set of disjoint trees, none of which contains r.
38. What is heap in data structure?
Heap is a special case of balanced binary tree data structure where the root-node key is
compared with its children and arranged accordingly. If α has child node β then
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap.
39. What is recursive function?
Some computer programming languages allow a module or function to call itself. This
technique is known as recursion. In recursion, a function α either calls itself directly or
calls a function β that in turn calls the original function α. The function α is called
recursive function.
40. What is tower of hanio?
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and
more than one rings. These rings are of different sizes and stacked upon in an
ascending order, i.e. the smaller one sits over the larger one. There are other variations
of the puzzle where the number of disks increase, but the tower count remains the same.
41. What is Fibonacci series?
Fibonacci series generates the subsequent number by adding two previous numbers.
Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be
taken as 0, 1 or 1, 1 respectively.
Fibonacci series satisfies the following conditions − Fn = Fn-1 + Fn-2
42. What is hashing?
Hash table supports only a subset of the operations allowed by binary search trees.
The implementation of hash tables is frequently called hashing, and it performs
insertions, deletions, and finds in constant average time.
Hashing is a technique to convert a range of key values into a range of indexes of an
array.
43. What is abstract data type?
An abstract data type (ADT) is a specification of only the behavior of instances of that
type. Such a specification may be all that is needed to design a module that uses the
type.
44. Define O-notation?
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running
time. It measures the worst case time complexity or the longest amount of time an
algorithm can possibly take to complete.
45. When is binary search best applied?
A binary search is an algorithm that is best applied to search a list when the elements
are already in order or sorted.
46. List out the advantages of linked list?
a) Linked list is a dynamic data structure so it can grow and shrink at runtime by
allocating and de-allocating memory.
b) Insertion and deletion of nodes are really easier.
c) No Memory Wastage.
d) Implementation.
e) Memory Usage.
f) Traversal.
g) Reverse Traversing.
47. Define PUSH and POP operations?
Pushing and popping applies to the way data is stored and retrieved in a stack. A push
denotes data being added to it, meaning data is being “pushed” into the stack. On the
other hand, a pop denotes data retrieval, and in particular refers to the topmost data
being accessed.
48. What is FIFO?
FIFO is short for First-in, First-out, and is used to represent how data is accessed in a
queue. Data has been inserted into the queue list the longest is the one that is removed
first.
49. What is merge sort?
Merge sort takes a divide-and-conquer approach to sorting data. In a sequence of data,
adjacent ones are merged and sorted to create bigger sorted lists. These sorted lists are
then merged again to form an even bigger sorted list, which continuous until you have
one single sorted list.
The merge sort applies the divide-and-conquer strategy to sort a sequence. First it
subdivides the sequence into subsequences of singletons. Then it successively merges
the subsequences pairwise until a single sequence is re-formed. Each merge preserves
order, so each merged subsequence is sorted. When the final merge is finished, the
complete sequence is sorted.
50. Define a complete binary tree?
A complete binary tree is either a full binary tree or one that is full except for a segment
of missing leaves on the right side of the bottom level.
51. Define leaves node tree?
Nodes with no children are called leaves.
52. What is spanning tree?
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected.
53. What is max heap?
Where the value of the root node is greater than or equal to either of its children.
54. What is the running time of merge and bubble sort algorithms?
The running time of merge-sort and quicksort is O(N log N).
The running time of bubble sort is O(n2).
55. Define Java Primitive.
The java programming language features eight primitive data types which are: int, byte,
short, long, float, double, boolean and char.
56. Differentiate between Array and Linklist.
When dealing with arrays, data is stored and retrieved using an index that actually refers
to the element number in the data sequence. This means that data can be accessed in
any order. Array can use contiguous memory location. A linked list is a sequence of
nodes in which each node is connected to the node following it. This forms a chain-like
link of data storage.
57. Write them in descending order O(N), O(NlogN), O(logN), O(N3).
O(N3), O(NlogN), O(N), O(logN).
58. Consider Recursive algorithm that calculate factorial of n what is the base
Condition?
If(n=1) return 1;
59. Translate prefix into infix a). -/+*abcde b). /-ab*c+de
In general, algorithms that involve efficient data structure is applied in the following
areas: numerical analysis, operating system, A.I., compiler design, database
management, graphics, and statistical analysis, to name a few.
62. Are linked list consider linear or non-linear data structure? Give reason?
Logically linked list is a linear data structure because each node has a link to its next
one. but physically it is not necessarily true because memory allocation to nodes may or
may not be sequential.
63. What is the minimum number of nodes that a binary tree can have?
A binary tree can have a minimum of zero nodes, which occurs when the nodes have
NULL values. Furthermore, a binary tree can also have 1 or 2 nodes.
64. What are dynamic data structure?
Dynamic data structures are structures that expand and contract as a program runs. It
provides a flexible means of manipulating data because it can adjust according to the
size of the data.
65. Why to use PREFIX and POSTFIX notations when we have simple INFIX notation?
66. There are 8, 15, 13 and 14 nodes in different trees. Which one of them can form a
full binary tree? Why?
67. How do you insert a new item binary search tree?
Assuming that the data to be inserted is a unique value (that is, not an existing entry in
the tree), check first if the tree is empty. If it's empty, just insert the new item in the root
node. If it's not empty, refer to the new item's key. If it's smaller than the root's key, insert
it into the root's left subtree, otherwise, insert it into the root's right subtree.
68. Diff. between linear and non-linear data structure?
Linear data structure is a structure wherein data elements are adjacent to each other.
Examples of linear data structure include arrays, linked lists, stacks and queues. On the
other hand, nonlinear data structure is a structure wherein each data element can
connect to more than two adjacent data elements. Examples of non-linear data structure
include trees and graphs.
69. Diff. file structure from storage structure?
Basically, the key difference is the memory area that is being accessed. When dealing
with the structure that resides the main memory of the computer system, this is referred
to as storage structure. When dealing with an auxiliary structure, we refer to it as file
structures.
70. What is an order list?
An ordered list is a list in which each node’s position in the list is determined by the value
of its key component, so that key values from an increasing sequence, as the list is
traversed.
71. What is data abstraction?
Data abstraction is a powerful tool for breaking down complex data problems into
manageable chunks. This is applied by initially specifying the data objects involved and
the operations to be performed on these data objects without being overly concerned
with how the data objects will be represented and stored in memory.
72. How does a selection sort work?
The selection sort is a fairly intuitive sorting algorithm,, though not necessarily efficient.
To perform this, the smallest element is first located and switched with the element at
subscript zero, thereby placing the smallest element in the first position.
The smallest element remaining in the subarray is then located next with subscripts 1
through n-1 and switched with the element at subscript 1, thereby placing the second
smallest element in the second position. The steps are repeated in the same manner till
the last element.
73. In what data structure are pointer applied?
Pointers that are used in linked list have various applications in data structure. Data
structures that make use of this concept include the Stack, Queue, Linked List and
Binary Tree.
Long Questions:
a). Write a method to add a node as a last node in singly linked list. The linked list
may be empty at the start. 3. Java code for Insertion sort
4. Application in computer Science of Stack and Queue.
b). Write a function to display all the nodes in singly linked list recursively. The
function is passed the reference of appropriate node to take start.
1. a). Draw a binary search tree by inserting the following numbers.
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18.
b). Convert following infix expression to postfix expression.
[(A+B)/(C+D)↑(E/F)]+(G+H)/I
AB+CD+/EF/↑GH+I/+
2. a). A BST has following in order and pre order traversal. Make the tree and write
down its post order traversal.
In order: badefc
Pre order: abcdef
b). Write a function that search a number within an array using binary search
recursively. In result of successful search, the index of array is returned and -1
otherwise?
3. a). Write down a method that accept two input array and merge them in a third
array.
b). Dry run quick sort algorithm on following data by showing each step.
6, 4, 2, 9, 3, 5
4. a). Suppose there are seven keys with number as 25, 3, 51, 37, 30, 79 and 5. There
are 10 hash address available. Try to accommodate them in available slots using
linear problem technique if collision take place.
The hashing function used is:
h(k) = key % 5
b). Sort the following list using Heap Sort technique, display each step.
20, 12, 25, 6, 10, 15, 13.
3. Write Recursive algorithm
public static int fib(int n) {
if (n < 2) {
return n; // basis
}
return fib(n-1) + fib(n-2); // recursive part
}
public static void main(String[] args) {
for (int n = 0; n < 16; n++) {
System.out.println("fib(" + n + ") = " + fib(n)); } }