Data Structure and Algorithms Questions and Answer
Data Structure and Algorithms Questions and Answer
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.
5. Tables: The data is saved in the form of rows and columns. These
are comparable to records in that the outcome or alteration of data
is mirrored across the whole table.
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.
Examples of linear data structures are Arrays, Stack, Strings, Queue, and Linked List.
4. What are some applications of Data Structures?
In terms of data structure interview questions, this is one of the most frequently
asked question.
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.
First, the first row of the array is entirely stored in memory, followed by the second
row of the array, and so on until the final row.
This is one of the most frequently asked data structure interview questions where
the interviewer expects you to give a thorough answer. Try to explain as much as
possible rather than finishing your answer in a sentence!
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.
Some implementations are stacks and queues, graphs, directory of names, dynamic
memory allocation, and performing arithmetic operations on long integers.
9. Are linked lists considered linear or non-linear Data Structures?
Linked lists are considered both linear and non-linear data structures depending
upon the application they are used for. When used for access strategies, it is
considered as a linear data-structure. When used for data storage, it is considered a
non-linear data structure.
10. 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 need speed when iterating through all the elements in the
sequence
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:
A music playlist with next and previous navigation buttons
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.
A problem can be solved in more than one way using several solution algorithms.
Algorithm analysis provides an estimation of the required resources of
an algorithm to solve a specific computational problem. The amount of time and
space resources required to execute is also determined.
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 stack is a linear data structure that operates on the same concept, in that
components in a stack are added and deleted only from one end, referred to as the
TOP. As a result, a stack is known as a LIFO (Last-In-First-Out) data structure
because the piece that was put last is the first to be removed.
1. PUSH: The push action inserts a new element into the stack. The
new feature is placed at the top of the stack. However, before
inserting the value, we must first verify if TOP=MAX–1, since if so,
the stack is filled, and no more insertions are possible. An
OVERFLOW message is printed if an attempt is made to put a value
into an existing stack.
In this type of data structure interview questions, you can also discuss your
experience and situations using queue. 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.
The front is used to get the value of the first data item but does not
remove it
In this data structure interview questions, try giving various advantages, along with
examples, if possible. It will show the interviewer your domain expertise. Generally,
both heap and stack are part of memory and used in Java for different needs:
Heap is more flexible than the stack because memory space can be
dynamically allocated and de-allocated as needed
Heap memory is used to store objects in Java, whereas stack
memory is used to store local variables and function call
Expression evaluation
Backtracking
Memory management
In terms of data structure interview questions, this is one of the most frequently
asked question.
The acronyms stand for Pushing and Popping operations performed on a stack.
These are ways data is stored and retrieved.
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.
30. What is an asymptotic analysis of an algorithm?
Quicksort is the name of a sorting algorithm. The method selects a pivot element
and rearranges the array elements so that all items less than the pivot chosen
element go to the left side of the pivot and all elements more significant than the
pivot element move to the right side.
Merge Sort is a sorting algorithm as well. The algorithm divides the array into two
halves, sorts them recursively, and then combines the two sorted halves. The goal of
points that are closest together is to identify the nearest pair of points in an x-y plane
collection of points. The issue may be solved in O(n2) time by computing the
distances between each pair of locations and comparing them to determine the
shortest distance.
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.