Interview Question
Interview Question
A variable is stored in memory based on the amount of memory that is needed. Following
are the steps followed to store a variable:
Using concepts like dynamic allocation ensures high efficiency and that the storage units can
be accessed based on requirements in real-time.
Q2. Can you explain the difference between file structure and storage structure?
File Structure: Representation of data into secondary or auxiliary memory say any device
such as a hard disk or pen drives that stores data which remains intact until manually deleted
is known as a file structure representation.
Storage Structure: In this type, data is stored in the main memory i.e RAM, and is deleted
once the function that uses this data gets completely executed.
The difference is that the storage structure has data stored in the memory of the computer system,
whereas the file structure has the data stored in the auxiliary memory.
A queue can be implemented using two stacks. Let q be the queue andstack1 and stack2 be the 2
stacks for implementing q. We know that stack supports push, pop, and peek operations and using
these operations, we need to emulate the operations of the queue - enqueue and dequeue. Hence,
queue q can be implemented in two methods (Both the methods use auxillary space complexity of
O(n)):
Here, the oldest element is always at the top of stack1 which ensures dequeue operation
occurs in O(1) time complexity.
Pseudocode:
enqueue(q, data):
deQueue(q):
Here, for enqueue operation, the new element is pushed at the top of stack1. Here, the
enqueue operation time complexity is O(1).
In dequeue, if stack2 is empty, all elements from stack1 are moved to stack2 and top
of stack2 is the result. Basically, reversing the list by pushing to a stack and returning the first
enqueued element. This operation of pushing all elements to a new stack takes O(n)
complexity.
Pseudocode:
enqueue(q, data):
dequeue(q):
If stack2 is empty:
A stack can be implemented using two queues. We know that a queue supports enqueue
and dequeue operations. Using these operations, we need to develop push, pop operations.
Let stack be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Then, stack ‘s’ can be
implemented in two ways:
This method ensures that the newly entered element is always at the front of ‘q1’ so that
pop operation just dequeues from ‘q1’.
‘q2’ is used as auxillary queue to put every new element in front of ‘q1’ while ensuring pop
happens in O(1) complexity.
Pseudocode:
push(s, data):
Enqueue data to q2
pop(s):
In pop operation, all the elements from q1 except the last remaining element, are pushed to
q2 if it is empty. That last element remaining of q1 is dequeued and returned.
Pseudocode:
push(s,data):
Enqueue data to q1
pop(s):
Step1: Dequeue every elements except the last element from q1 and enqueue to q2.
Step2: Dequeue the last item of q1, the dequeued item is stored in result variable.
Step3: Swap the names of q1 and q2 (for getting updated data after dequeue)
An array is a collection of data elements A linked list is a collection of entities known as nodes. The
Arrays Linked Lists
of the same type. node is divided into two sections: data and address.
The memory size of an array is fixed and The memory size of a linked list is allocated during
cannot be changed during runtime. runtime.
Operations like insertion and deletion take Operations like insertion and deletion are faster in the
longer time in an array. linked list.
Hashmap is a data structure that uses an implementation of a hash table data structure which allows
access to data in constant time (O(1)) complexity if you have the key.
Q7. What is the requirement for an object to be used as key or value in HashMap?
The key or value object that gets used in the hashmap must
implement equals() and hashcode() method.
The hash code is used when inserting the key object into the map and the equals method is
used when trying to retrieve a value from the map.
Q8. What are some key operations performed on the Deque data structure?
The following table contains an asymptotic analysis of different implementations of a priority queue:
Heap is a special tree-based non-linear data structure in which the tree is a complete binary tree. A
binary tree is said to be complete if all levels are completely filled except possibly the last level and
the last level has all elements as left as possible. Heaps are of two types:
Max-Heap:
o In a Max-Heap the data element present at the root node must be the greatest
among all the data elements present in the tree.
o This property should be recursively true for all sub-trees of that binary tree.
Min-Heap:
o In a Min-Heap the data element present at the root node must be the smallest (or
minimum) among all the data elements present in the tree.
o This property should be recursively true for all sub-trees of that binary tree.
Answer: In some programming languages, arrays can be resized dynamically, while in others, such
as C, the size is fixed.
Q12 How would you reverse an array in-place in linear time and constant space?
Answer: One approach is to use two pointers starting from the beginning and end of the array and
swap the elements until they meet in the middle.
Answer: A sparse array is an array in which most of the elements have the same value. It can be
represented using a data structure that only stores the non-default (non-zero) values.
Not cache-friendly
Dynamic arrays are more efficient for random access and large data sets, while linked lists are more
efficient for operations that involve insertion and deletion in the middle. Linked lists are also more
flexible and can represent complex data structures.
Answer: A stack can be used to check if a parenthesis expression is balanced by following these
steps:
When an closing parenthesis is encountered, pop the top element from the stack and check
if it matches the closing parenthesis.
If the stack is empty at the end of the expression, then the expression is balanced.
Answer: By creating a new heap and inserting the elements from both heaps into the new heap
while maintaining the heap property.
Q19 When would you choose a tree over other data structures like arrays or linked lists?
Answer: Trees are ideal for hierarchical data, maintaining relationships between elements, and
efficient searching based on order. Arrays or linked lists are better for simple linear data or frequent
insertions/deletions at specific positions.
Q20 How can you convert a binary search tree into a sorted array?
Answer: One efficient way is to use an inorder traversal of the tree. Since the tree is sorted, visiting
nodes in this order will result in a sorted array.
Yes, dynamic memory allocation can help in managing data. It is easier to work with data when it can
be assigned to memory locations dynamically.