DSL Pract (Oral) Question
DSL Pract (Oral) Question
The data structure is the way data is organized (stored) and manipulated for retrieval and access. It also
defines the way different sets of data relate to one another, establishing relationships and forming
algorithms.
A data structure is linear if all its elements or data items are arranged in a sequence or a linear order. The
elements are stored in a non-hierarchical way so that each item has successors and predecessors except
the first and last element in the list.
12 Can you tell how linear data structures differ from non-linear data structures?
▪ If the elements of a data structure result in a sequence or a linear list then it is called a linear data
structure. Whereas, traversal of nodes happens in a non-linear fashion in non-linear data structures.
▪ Lists, stacks, and queues are examples of linear data structures whereas graphs and trees are
the examples of non-linear data structures.
Numerical analysis, operating system, AI, compiler design, database management, graphics, statistical
analysis, and simulation.
14. What is the difference between file structure and storage structure?
The difference lies in the memory area accessed. Storage structure refers to the data structure in the
memory of the computer system, whereas file structure represents the storage structure in the auxiliary
memory.
It’s a linear data structure or a sequence of data objects where elements are not stored in adjacent
memory locations. The elements are linked using pointers to form a chain. Each element is a separate
object, called a node. Each node has two items: a data field and a reference to the next node. The entry
point in a linked list is called the head. Where the list is empty, the head is a null reference and the last
node has a reference to null.
A linked list is a dynamic data structure, where the number of nodes is not fixed, and the list has the
ability to grow and shrink on demand.
• We deal with an unknown number of objects or don’t know how many items are in the list
• We need constant-time insertions/deletions from the list, as in real-time computing where time
predictability is critical
• The algorithm requires a data structure where objects need to be stored irrespective of their physical
address in memory
Some implementations are stacks and queues, graphs, directory of names, dynamic memory allocation,
and performing arithmetic operations on long integers.
17. What are the advantages of a linked list over an array? In which scenarios do we use Linked
List and when Array?
Insertion and deletion of nodes is an easier process, as we only update the address present in the next
pointer of a node. It’s expensive to do the same in an array as the room has to be created for the new
elements and existing elements must be shifted.
As a linked list is a dynamic data structure, there is no need to give an initial size as it can grow and
shrink at runtime by allocating and deallocating memory. However, the size is limited in an array as the
number of elements is statically stored in the main memory.
3. No wastage of memory
As the size of a linked list can increase or decrease depending on the demands of the program, and
memory is allocated only when required, there is no memory wasted. In the case of an array, there is
memory wastage. For instance, if we declare an array of size 10 and store only five elements in it, then
the space for five elements is wasted.
4. Implementation
Data structures like stack and queues are more easily implemented using a linked list than an array.
• When we want to insert items in the middle of the list, such as when implementing a priority queue
Some scenarios in which we use array over the linked list are:
• When we know the number of elements in the array beforehand, so we can allocate the correct
amount of memory
• When we need speed when iterating through all the elements in the sequence
• When memory is a concern; filled arrays use less memory than linked lists, as each element in the
array is the data but each linked list node requires the data as well as one or more pointers to the
other elements in the linked list
In summary, we consider the requirements of space, time, and ease of implementation to decide whether
to use a linked list or array.
It is a complex type (double-ended LL) of a linked list in which a node has two links, one that connects to
the next node in the sequence and another that connects to the previous node. This allows traversal
across the data elements in both directions.
Examples include:
• The undo and redo functionality on a browser, where you can reverse the node to get to the previous
page
All of the elements in a one-dimension array can be referenced using an indexed loop as the array
subscript so that the counter runs from 0 to the array size minus one.
They are collections of data in memory that expand and contract to grow or shrink in size as a program
runs. This enables the programmer to control exactly how much memory is to be utilized.
Examples are the dynamic array, linked list, stack, queue, and heap.
An algorithm is a step by step method of solving a problem or manipulating data. It defines a set of
instructions to be executed in a certain order to get the desired output.
The time complexity of an algorithm quantifies the amount of time taken for an algorithm to run as a
function of the length of the input. The space complexity quantifies the amount of space or memory taken
by an algorithm, to run as a function of the length of the input.
A stack is an abstract data type that specifies a linear data structure, as in a real physical stack or piles
where you can only take the top item off the stack in order to remove things. Thus, insertion (push) and
deletion (pop) of items take place only at one end called top of the stack, with a particular order: LIFO
(Last In First Out) or FILO (First In Last Out).
• Syntax parsing
• String reversal
• Parenthesis checking
• Backtracking
A queue is an abstract data type that specifies a linear data structure or an ordered list, using the
First In First Out (FIFO) operation to access elements. Insert operations can be performed only at one
end called REAR and delete operations can be performed only at the other end called FRONT.
• As waiting lists for a single shared resource in a printer, CPU, call center systems, or image uploads;
where the first one entered is the first to be processed
• In the asynchronous transfer of data; or example pipes, file IO, and sockets
• To maintain the playlist in media players (to add or remove the songs)
• The front is used to get the value of the first data item but does not remove it
• Expression evaluation
• Backtracking
• Memory management
The acronyms stand for Pushing and Popping operations performed on a stack. These are ways data is
stored and retrieved.
• PUSH is used to add an item to a stack, while POP is used to remove an item.
• PUSH takes two arguments, the name of the stack to add the data to and the value of the entry to be
added. POP only needs the name of the stack.
• When the stack is filled and another PUSH command is issued, you get a stack overflow error, which
means that the stack can no longer accommodate the last PUSH. In POP, a stack underflow error
occurs when you’re trying to POP an already empty stack.
A single sorting algorithm can’t be considered best, as each algorithm is designed for a particular data
structure and data set. However, the QuickSort algorithm is generally considered the fastest because it
has the best performance for most inputs.
• Cache-efficient: It linearly scans and linearly partitions the input. This means we can make the most of
every cache load.
• Can skip some swaps: As QuickSort is slightly sensitive to input that is in the right order, it can skip
some swaps.
Merge sort is a divide-and-conquer algorithm for sorting the data. It works by merging and sorting
adjacent data to create bigger sorted lists, which are then merged recursively to form even bigger sorted
lists until you have one single sorted list.
Selection sort works by repeatedly picking the smallest number in ascending order from the list and
placing it at the beginning. This process is repeated moving toward the end of the list or sorted subarray.
Scan all items and find the smallest. Switch over the position as the first item. Repeat the selection sort
on the remaining N-1 items. We always iterate forward (i from 0 to N-1) and swap with the smallest
element (always i).
35. What are the advantages of binary search over a linear search?
In a sorted list:
• A binary search is more efficient than a linear search because we perform fewer comparisons. With
linear search, we can only eliminate one element per comparison each time we fail to find the value
we are looking for, but with the binary search, we eliminate half the set with each comparison.
• Binary search runs in O(log n) time compared to linear search’s O(n) time. This means that the more
of the elements present in the search array, the faster is binary search compared to a linear search.
Dynamic memory allocation stores simple structured data types at runtime. It has the ability to combine
separately allocated structured blocks to form composite structures that expand and contract as needed,
thus helping manage data of data blocks of arbitrary size, in arbitrary order.
Stack data structure is used in recursion due to its last in first out nature. Operating system
maintains the stack in order to save the iteration variables at each function call
PUSH and POP operations specify how data is stored and retrieved in a stack.
PUSH: PUSH specifies that data is being "inserted" into the stack.
POP: POP specifies data retrieval. It means that data is being deleted from the stack.
40. Write the steps involved in the insertion and deletion of an element in the stack.
Push:
o Increment the variable top so that it can refer to the next memory allocation
o Copy the item to the at the array index value equal to the top
Pop:
An expression in which operators follow the operands is known as postfix expression. The
main benefit of this form is that there is no need to group sub-expressions in parentheses or
to consider operator precedence.
42. How are the elements of a 2D array are stored in the memory?
There are two techniques by using which, the elements of a 2D array can be stored in the
memory.
o Row-Major Order: In row-major ordering, all the rows of the 2D array are stored
into the memory contiguously. First, the 1st row of the array is stored into the
memory completely, then the 2nd row of the array is stored into the memory
completely and so on till the last row.
43 Calculate the address of a random element present in a 2D array, given base address as BA.
Row-Major Order: If array is declared as a[m][n] where m is the number of rows while n
is the number of columns, then address of an element a[i][j] of the array stored in row
major order is calculated as,
Address(a[i][j]) = B. A. + (i * n + j) * size
Column-Major Order: If array is declared as a[m][n] where m is the number of rows while
n is the number of columns, then address of an element a[i][j] of the array stored in column
major order is calculated as
o Queues are widely used as waiting lists for a single shared resource like a printer,
disk, CPU.
o Queues are used in the asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO, sockets.
o Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
o Queues are used to maintain the playlist in media players to add and remove the
songs from the play-list.
o Memory Wastage: The space of the array, which is used to store queue elements,
can never be reused to store the elements of that queue because the elements can
only be inserted at front end and the value of front might be so high so that, all the
space before that, can never be filled.
o Array Size: There might be situations in which, we may need to extend the queue
to insert more elements if we use an array to implement queue, It will almost be
impossible to extend the array size, therefore deciding the correct array size is
always a problem in array implementation of queue.
46. What are the scenarios in which an element can be inserted into the circular queue?
o If (rear + 1)%maxsize = front, the queue is full. In that case, overflow occurs and
therefore, insertion can not be performed in the queue.
o If rear != max - 1, the rear will be incremented to the mod(maxsize) and the new
value will be inserted at the rear end of the queue.
o If front != 0 and rear = max - 1, it means that queue is not full therefore, set the
value of rear to 0 and insert the new element there.
47.What is a dequeue?
Dequeue (also known as double-ended queue) can be defined as an ordered set of elements
in which the insertion and deletion can be performed at both the ends, i.e. front and rear.
48. What is the minimum number of queues that can be used to implement a priority queue?
Two queues are needed. One queue is used to store the data elements, and another is used
for storing priorities.
o A null variable simply indicates an empty value, whereas void is used to identify
pointers as having no initial size.
o A binary search is an algorithm that is best applied to search a list when the elements are already
in order or sorted. The list is searched starting in the middle, such that if that middle value is not
the target search key, it will check to see if it will continue the search on the lower half of the list
or the higher half. The split and search will then continue in the same manner.
Merge sort, is a divide-and-conquer approach for sorting the 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 continues until you have one single sorted list.
A linear search refers to the way a target key is being searched in a sequential data structure. In this
method, each element in the list is checked and compared against the target key. The process is repeated
until found or if the end of the file has been reached.
The amount of memory to be allocated or reserved would depend on the data type of the variable being
declared. For example, if a variable is declared to be of integer type, then 32 bits of memory storage will
be reserved for that variable
A bubble sort is one sorting technique that can be applied to data structures such as an array. It works by
comparing adjacent elements and exchanges their values if they are out of order. This method lets the
smaller values “bubble” to the top of the list, while the larger value sinks to the bottom.
A linked list typically has two parts: the head and the tail. Between the head and tail lie the actual nodes.
All these nodes are linked sequentially.
Selection sort works by picking the smallest number from the list and placing it at the front. This process
is repeated for the second position towards the end of the list. It is the simplest sort algorithm.
Doubly linked lists are a special type of linked list wherein traversal across the data elements can be done
in both directions. This is made possible by having two links in every node, one that links to the next node
and another one that connects to the previous node.
Fibonacci search is a search algorithm that applies to a sorted array. It makes use of a divide-and-conquer
approach that can significantly reduce the time needed in order to reach the target element.
Recursive algorithm targets a problem by dividing it into smaller, manageable sub-problems. The output
of one recursion after processing one sub-problem becomes the input to the next recursive process
To find the target key in a linked list, you have to apply sequential search. Each node is traversed and
compared with the target key, and if it is different, then it follows the link to the next node. This traversal
continues until either the target key is found or if the last node is reached.
.