Uploaded by akash25274kumar

Data structures and algorithms

advertisement
1, define data structure ?
Ans: A data structure can be defined as a particular method of organizing a large amount of data
so that it can be used efficiently. A data structure can be used to store data in the form of a stack,
queue etc.
2, What is linear data structure? Give example
Ans: Linear data structures are easy to implement because computer memory is arranged in a
linear way. Its examples are array, stack, queue, linked list, etc.
3,List the non-primitive data types?
Ans:Non-primitive data types in Java are not predefined. They are created by the programmer.
Array, structure, union, link list, stacks, queue
4, How data structures are classified?
Ans: A data structure is a way of organizing the data. So we can classify data structures as
shown into primitive or standard data structures and non-primitive or user-defined data
structures.
5, What is time and space complexity of algorithm?
Ans:Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run
as a function of the length of the input. Similarly, Space complexity of an algorithm quantifies the
amount of space or memory taken by an algorithm to run as a function of the length of the input.
6, Give example for time and space complexity of algorithm?
Ans:When we analyse an algorithm, we use a notation to represent its time
complexity and that notation is Big O notation. For Example: time complexity
for Linear search can be represented as O(n) and O(log n) for Binary
search (where, n and log(n) are the number of operations).
Space complexity includes both Auxiliary space and space used by input. For example, if
we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space
would be a better criterion than Space Complexity.
7, List the various operations that can be performed on data structure.
Ans: The possible operations on the linear data structure are: Traversal, Insertion, Deletion,
Searching, Sorting and Merging.
8, List out the application areas of data structures.?








Ans:Application of Arrays:
Application of Linked Lists:
Application of Stack:
Application of Queue:
Application of Graph:
Application of Tree:
Application of Hash Tables:
Application of Heap:
9, What is dynamic memory allocation?
Ans;Dynamic Memory Allocation is a process in which we allocate or deallocate a block of
memory during the run-time of a program. There are four functions malloc(), calloc(), realloc()
and free() present in <stdlib. h> header file that are used for Dynamic Memory Allocation in our
system.
10,Define pointers?
Ans: Pointers can be defined as the special variables which have a capability to store address of
any variable. They are used in C++ programs to access the memory and manipulate the data
using addresses.
Pointers are a very important feature of C++ Programming as it allows to access data using
their memory addresses and not directly using their variable names.
11,Compare static and dynamic memory allocation?
Ans:The memory is allocated at the runtime. In static memory allocation, while
executing a program, the memory cannot be changed. In dynamic memory
allocation, while executing a program, the memory can be changed. Static memory
allocation is preferred in an array.
12,Write the memory allocation functions?
Ans:The C malloc() function stands for memory allocation. It is a function
which is used to allocate a block of memory dynamically. It reserves memory
space of specified size and returns the null pointer pointing to the memory
location. The pointer returned is usually of type void.
13,Compare
calloc and malloc functions?
Ans:malloc() function creates a single block of memory of a specific size. calloc()
function assigns multiple blocks of memory to a single variable.
14,Mention the advantages of recursion?
Ans: In short and simple terms, a recursive function is one which calls itself. Advantage: It can
reduce time complexity and has a relaxation on the number of iterations( we can run a
variable number of loops ). It is easy to implement.
15, Differentiate between linear and nonlinear data structure?
Ans: LINEAR DATA STRUCTURE.
In linear data structure, data elements are sequentially connected and each element is
traversable through a single run.

NON LINEAR DATA STRUCTURE

In non-linear data structure, data elements are hierarchically connected and are present
at various levels. In linear data structure, all data elements are present at a single level.
16,What is linear search?
Ans:In computer science, a linear search or sequential search is a method for finding an
element within a list. It sequentially checks each element of the list until a match is found or
the whole list has been searched.
17,what is binary search?
Ans:In computer science, binary search, also known as half-interval search, logarithmic search,
or binary chop, is a search algorithm that finds the position of a target value within a
sorted array. Binary search compares the target value to the middle element of the array.
18,Define entity?
Ans:Overview. An entity in DBMS (Database management System) is a real-world
thing or a real-world object which is distinguishable from other objects in the real
world. For example, a car is an entity. An attribute of an entity gives us information
about the characteristic features of an entity.
19,What is file?
Ans:A file is a collection of data stored on mass storage (e.g., disk or tape) Why on
mass storage? The data is subdivided into records (e.g., student in- formation). Each
record contains a number of fields (e.g., name, GPA). One (or more) field is the key field
(e.g., name).
20,What are asymptotic notations?
Ans:Asymptotic notation is a mathematical notation that is used to analyze the time
complexity and the runtime of an algorithm for a large input. For example if we
want to compare the runtimes of the bubble sort algorithm and merge sort algorithm, we
can use asymptotic notations to do this comparison.
21,Define
recursion?
Ans: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 α.
22,What is a Fibonacci sequence?
Ans: the Fibonacci numbers are the numbers in the following integer sequence, called
the Fibonacci sequence, and characterized by the fact that every number after the
first two is the sum of the two preceding ones: 1,1,2,3,5,8,13,21,34,55,89,144,...
23,Mention the parameters for efficiency of sorting algorithm.?
Ans:Efficiency
of sorting algorithms
Sorting algorithms are usually judged by their efficiency. In this case, efficiency refers to the
algorithmic efficiency as the size of the input grows large and is generally based on the number of
elements to sort. Most of the algorithms in use have an algorithmic efficiency of either O(n^2)
or O(n*log(n)).
24,List the different sorting algorithms?
Ans:Some of the most common sorting algorithms are:



Selection sort.
Bubble sort.
Insertion sort.


Merge sort.
Quick sort.
25, What is Bubble Sort?
1. Ans:Bubble sort is a basic algorithm for arranging a string of numbers or other
elements in the correct order. The method works by examining each setMention the
important characteristics of Insertion Sort
26,Mention the space complexity of Bubble Sorting?
Ans:The space complexity for Bubble Sort is O(1), because only a single additional memory
space is required i.e. for temp variable. Also, the best case time complexity will be O(n), it is
when the list is already sorted.
27, What is Insertion Sort?
Ans:Insertion sort is a simple sorting algorithm that builds the final sorted array (or list)
one item at a time. It is much less efficient on large lists than more advanced algorithms such
as quicksort, heapsort, or merge sort.
28, Mention the important characteristics of Insertion Sort?
Ans:mportant Characteristics of Insertion Sort:
It is efficient for smaller data sets, but very inefficient for larger lists. Insertion Sort
is adaptive, that means it reduces its total number of steps if given a partially sorted list,
hence it increases its efficiency. Its space complexity is less.
29, Mention the worst case complexity of Insertion Sort?
Ans:The worst-case (and average-case) complexity of the insertion sort algorithm is O(n²).
Meaning that, in the worst case, the time taken to sort a list is proportional to the square of the
number of elements in the list. The best-case time complexity of insertion sort algorithm is O(n)
time complexity.
30,Compare quick sort and merge sort?
Ans:
Basis for comparison
Quick Sort
Merge Sort
Efficiency
Inefficient for larger arrays
More efficient
Sorting method
Internal
External
Stability
Not Stable
Stable
Preferred for
for Arrays
for Linked Lists
31,What are the various factors to be considered in deciding a sorting algorithm?






Ans:Simplicity. ...
Running time. ...
Memory consumption. ...
Parallel processing. ...
Stability. ...
Assumptions about input data.
32, What is the main idea behind insertion sort?
Ans:Insertion sort is based on the idea that one element from the input elements is
consumed in each iteration to find its correct position i.e, the position to which it belongs in
a sorted array.
33, What is the main idea behind selection sort?
Ans:The Selection sort algorithm is based on the idea of finding the minimum or maximum
element in an unsorted array and then putting it in its correct position in a sorted array.
34, What is the purpose of quick sort?
Ans:The sorting algorithm is used for information searching and as Quicksort is the fastest
algorithm so it is widely used as a better way of searching. It is used everywhere where a stable
sort is not needed. Quicksort is a cache-friendly algorithm as it has a good locality of reference
when used for arrays.
35, What is merge sort?
Ans:Merge sort. An example of merge sort. First, divide the list into the smallest unit (1 element),
then compare each element with the adjacent list to sort and merge the two adjacent lists.
Finally, all the elements are sorted and merged.
36, What are the advantages of linked list?
Ans:Advantages :
 Size is not an issue as compared to arrays.
 Addition/Deletion of an element from the list at any index which is an O(1)
operation in Lists as compared to Arrays.
 They can be used as underlying dat.
37, Define doubly linked list?
Ans:Each node consists of a data value, a pointer to the next node, and a pointer
to the previous node.
38, State the properties of LIST abstract data type?
Ans: In computer science, a list or sequence is an abstract data type that represents a finite
number of ordered values, where the same value may occur more than once.
39, What is the circular linked list?
Ans: A circular linked list is a variation of a linked list in which the last node points to the first
node, completing a full circle of nodes. In other words, this variation of the linked list doesn't have
a null element at the end.
40, Define Stack?
Ans:A stack is a linear data structure that follows the principle of Last In First Out
(LIFO). This means the last element inserted inside the stack is removed first. You can think
of the stack data structure as the pile of plates on top of another. Stack representation
similar to a pile of plate.
41, What are the two operations of Stack?
Ans: Push, which adds an element to the collection, and. Pop, which removes the most
recently added element that was not yet removed.
42, Define Queue?
Ans: A queue is an important data structure in programming. A queue follows the FIFO (First In
First Out) method and is open at both of its ends. Data insertion is done at one end rear end or
the tail of the queue while deletion is done at the other end called the front end or the head of the
queue.
43, What are the types of queue?
Ans: There are four different types of queues: Simple Queue. Circular Queue. Priority Queue.
44, What are the applications of queue?
Ans: Managing requests on a single shared resource such as CPU scheduling and disk
scheduling. Handling hardware or real-time systems interrupts. Handling website traffic. Routers
and switches in networking.
45, What is a Priority Queue?
Ans: A priority queue is a special type of queue in which each element is associated with a
priority value. And, elements are served on the basis of their priority. That is, higher priority
elements are served first. However, if elements with the same priority occur, they are served
according to their order in the queue.
46, What are the different ways to implement list?
Ans: There are two general-purpose List implementations — ArrayList and LinkedList . Most of
the time, you'll probably use ArrayList , which offers constant-time positional access and is just plain
fast. It does not have to allocate a node object for each element in the List , and it can take
advantage of System.
47, What are the advantages in the array implementation of list?
Ans:Advantages :



Size is not an issue as compared to arrays.
Addition/Deletion of an element from the list at any index which is an O(1) operation
in Lists as compared to Arrays.
They can be used as underlying dat.
48, . What are the postfix and prefix forms of the expression?
Ans: Prefix: An expression is called the prefix expression if the operator appears in the
expression before the operands. Simply of the form (operator operand1 operand2). Postfix: An
expression is called the postfix expression if the operator appears in the expression after the
operands.
49, State the advantages of circular lists?
Ans:Advantages of Circular Linked Lists:
 Any node can be a starting point. ...
 Useful for implementation of queue. ...
 Circular lists are useful in applications to repeatedly go around the list. ...
 Circular Doubly Linked Lists are used for implementation of advanced data structures
like Fibonacci Heap.
50, Differentiate between arrays and lists?
Ans:
LISTS
ARRAYS
A list is an ordered data structure with
An array is a collection of elements of a similar
elements,separated by comma and enclosed
data type array element store in a contgous
with square brackets…
memory location…
51, What are push and pop operations?
Ans:Push operation refers to inserting an element in the stack. Since there's only one
position at which the new element can be inserted — Top of the stack, the new element is
inserted at the top of the stack. POP Operation. Pop operation refers to the removal of an
element.
52, What are enqueue and dequeue operations?
Ans: We know that queue is a First–In, First–Out (FIFO) data structure in which elements are
removed in the same order in which they were added to the queue. In an enqueue operation,
items are added to the rear of the queue, while in dequeue operation, items are removed from
the front of the queue.
53, Distinguish between stack and queue?
Ans: The primary difference between Stack and Queue Data Structures is that Stack follows LIFO
while Queue follows FIFO data structure type. LIFO refers to Last In First Out. It means that
when we put data in a Stack, it processes the last entry first.
54, Give the applications of priority queues?
Ans: It is used in prim's algorithm. It is used in data compression techniques like Huffman code. It is
used in heap sort. It is also used in operating system like priority scheduling, load balancing and
interrupt handling.
55, What are the features of stacks?
Ans:The Characteristics of Stacks are:
 It is a data structure which follows LIFO (Last-in-First-Out)
 The insertion and deletion happens at the same end i.e. from the top of the stack.
 Stack is implemented through Array or Linked list .
 Stack is used in many algorithms like Tower of Hanoi, tree traversals.
56, Define Circular Queue.
Ans: A circular queue is the extended version of a regular queue where the last element is
connected to the first element. Thus forming a circle-like structure. Circular queue representation.
The circular queue solves the major limitation of the normal queue.
57, How insertion is done in doubly linked list?
Ans: Create a new node. allocate memory for newNode. assign the data to newNode . ...
Set prev and next pointers of new node. point next of newNode to the first node of the doubly linked
list. ... Make new node as head node. Point prev of the first node to newNode (now the previous
head is the second node)
58, List any four applications of stack?





Ans:Evaluation of Arithmetic Expressions.
Backtracking.
Delimiter Checking.
Reverse a Data.
Processing Function Calls.
59, List the various operations performed on the Queue?
ans:Basic Operations of Queue
 Enqueue: Add an element to the end of the queue.
 Dequeue: Remove an element from the front of the queue.
 IsEmpty: Check if the queue is empty.
 IsFull: Check if the queue is full.
 Peek: Get the value of the front of the queue without removing it.
60.Give the differences between linear search and binary search techniques?
Ans: Linear search is a search that finds an element in the list by searching the element sequentially
until the element is found in the list. On the other hand, a binary search is a search that finds the
middle element in the list recursively until the middle element is matched with a searched element.
61,Define tree?
Ans: tree can be defined as a non- linear data structure consisting of a root node and others nodes
present at different levels forming a hierarchy. A tree essentially has one node called its root and
one or more nodes adjacent below, connected to it. A tree with no nodes is called an empty tree…
62,Define leaf. Give example?
Ans:In a tree data structure, the node which does not have a child is called as LEAF Node. In
simple words, a leaf is a node with no child. In a tree data structure, the leaf nodes are also called
as External Nodes. External node is also a node with no child.
63,What is meant by complete binary tree?
Ans:A complete binary tree is a binary tree in which all the levels are completely filled except
possibly the lowest one, which is filled from the left. A complete binary tree is just like a full
binary tree, but with two major differences. All the leaf elements must lean towards the left.
64,What is an ordered tree?
Ans:A tree where the children of every node are ordered, that is, there is a first child, second
child, third child, etc. Note: An unordered tree may be thought of as a recursive bag, while an
ordered tree is a recursive list.
65,What is a Binary tree?
Ans:Binary tree is a special tree data structure. In a binary tree, each node can have at most 2
children. In a binary tree, nodes may be arranged in any random order.
66, What are the applications of binary tree?
Ans:





Implementing routing table in router.
Data compression code.
Implementation of Expression parsers and expression solvers.
To solve database problem such as indexing.
Expression evaluation.
67,What is meant by traversing?
Ans:Traversing a data structure means: "visiting" or "touching" the elements of the
structure, and doing something with the data. (Traversing is also sometimes called
iterating over the data structure).
68,What are the different types of traversing?
Ans:There are three common ways to traverse them in depth-first order: in-order, pre-order
and post-order.
69,What are the two methods of binary tree implementation?
Ans:
70,Define preorder traversal?
Ans:Start with root node 30 . print 30 and recursively traverse the left subtree. next node is 20.
now 20 have subtree so print 20 and traverse to left subtree of 20 . next node is 15 and 15 have
subtree so print 15 and traverse to left subtree of 15.
71,Define postorder traversal?
Ans:Postorder traversal is a kind of traversal in which we first traverse the left subtree in a
postorder manner, then traverse the right subtree in a postorder manner and at the end visit
the root node. For example, in the following tree: The postorder traversal will be
7→5→4→20→60→30→10.
72,Define inorder traversal?
Ans:Process all nodes of a tree by recursively processing the left subtree, then processing
the root, and finally the right subtree. Also known as symmetric traversal.
73,What is the length of the path in a tree?
Ans:The path length of a tree is the sum of the levels of all the tree's nodes. The path length can
have simple recursive definition as follows. The path length of a tree with N nodes is the sum of the
path lengths of the subtrees of its root plus N-1.
74,Define Graph?
Ans:Graphs in data structures are used to represent the relationships between objects. Every
graph consists of a set of points known as vertices or nodes connected by lines known as edges.
The vertices in a network represent entities.
75,Define path in a graph?
Ans:In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a
sequence of vertices which, by most definitions, are all distinct (and since the vertices are
distinct, so are the edges).
Download