0% found this document useful (0 votes)
15 views7 pages

Interview Question

The document discusses various data structures and their implementations, including the processes of storing variables in memory, differences between file and storage structures, and methods for implementing queues and stacks using stacks and queues. It also covers comparisons between arrays and linked lists, the concept of hashmaps, operations on deques, and the characteristics of heaps. Additionally, it addresses dynamic arrays, sparse arrays, and techniques for checking balanced parentheses, converting data structures, and dynamic memory allocation.

Uploaded by

Abhishek Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Interview Question

The document discusses various data structures and their implementations, including the processes of storing variables in memory, differences between file and storage structures, and methods for implementing queues and stacks using stacks and queues. It also covers comparisons between arrays and linked lists, the concept of hashmaps, operations on deques, and the characteristics of heaps. Additionally, it addresses dynamic arrays, sparse arrays, and techniques for checking balanced parentheses, converting data structures, and dynamic memory allocation.

Uploaded by

Abhishek Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1. Explain the process behind storing a variable in memory.

 A variable is stored in memory based on the amount of memory that is needed. Following
are the steps followed to store a variable:

o The required amount of memory is assigned first.

o Then, it is stored based on the data structure being used.

 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.

Q3. How to implement a queue using stack?

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)):

1. By making enqueue operation costly:

 Here, the oldest element is always at the top of stack1 which ensures dequeue operation
occurs in O(1) time complexity.

 To place the element at top of stack1, stack2 is used.

 Pseudocode:

o Enqueue: Here time complexity will be O(n)

enqueue(q, data):

While stack1 is not empty:

Push everything from stack1 to stack2.

Push data to stack1

Push everything back to stack1.

 Dequeue: Here time complexity will be O(1)

deQueue(q):

If stack1 is empty then error else


Pop an item from stack1 and return it

2. By making the dequeue operation costly:

 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:

o Enqueue: Time complexity: O(1)

enqueue(q, data):

Push data to stack1

 Dequeue: Time complexity: O(n)

dequeue(q):

If both stacks are empty then raise error.

If stack2 is empty:

While stack1 is not empty:

push everything from stack1 to stack2.

Pop the element from stack2 and return it.

Q4. How do you implement stack using queues?

 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:

1. By making push operation costly:

 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:

o Push element to stack s: Here push takes O(n) time complexity.

push(s, data):

Enqueue data to q2

Dequeue elements one by one from q1 and enqueue to q2.


Swap the names of q1 and q2

 Pop element from stack s: Takes O(1) time complexity.

pop(s):

dequeue from q1 and return it.

2. By making pop operation costly:

 In push operation, the element is enqueued to q1.

 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:

o Push element to stack s: Here push takes O(1) time complexity.

push(s,data):

Enqueue data to q1

 Pop element from stack s: Takes O(n) time complexity.

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)

Step4: Return the result.

Q5. Difference between Array and Linked List.

Arrays Linked Lists

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.

It keeps the data elements in a single


It stores elements at random, or anywhere in the memory.
memory.

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.

An array's elements are not dependent on


Linked List elements are dependent on one another.
one another.

It is easier and faster to access an element


In the linked list, it takes time to access an element.
in an array.

Memory utilization is ineffective in the


Memory utilization is effective in the case of linked lists.
case of an array.

Operations like insertion and deletion take Operations like insertion and deletion are faster in the
longer time in an array. linked list.

Q6. What is hashmap in data structure?

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?

Following are the key operations available deque:

 insertFront(): This adds an element to the front of the Deque.

 insertLast(): This adds an element to the rear of the Deque.

 deleteFront(): This deletes an element from the front of the Deque.

 deleteLast():This deletes an element from the front of the Deque.

 getFront(): This gets an element from the front of the Deque.

 getRear(): This gets an element from the rear of the Deque.

 isEmpty(): This checks whether Deque is empty or not.

 isFull(): This checks whether Deque is full or not.


Q9. Compare different implementations of priority queue

The following table contains an asymptotic analysis of different implementations of a priority queue:

Operations peek insert delete

Linked List O(1) O(n) O(1)

Binary Heap O(1) O(log n) O(log n)

Binary Search Tree O(1) O(log n) O(log n)

Q10. What is a heap data structure?

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.

Q11 Can an array be resized at runtime?

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.

Q13 Explain the concept of a sparse array.

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.

Q14 What is a cycle/loop in Singly Linked List:


Answer: A cycle, also known as a loop, in a singly-linked list occurs when a node in the list points
back to a previous node, creating a circular path. This means that if you start traversing the list from
any node, you will eventually come back to the same node, forming an infinite loop.

Q15 How would you compare Dynamic Arrays Vs Linked Lists?

Answer: Dynamic Array Advantages:

 Fast random access (O(1))

 Efficient for large data sets

 Contiguous memory allocation

Dynamic Array Disadvantages:

 Slow insertion and deletion in the middle (O(n))

 Fixed size, can lead to memory waste or reallocation

Linked Lists Advantages:

 Efficient insertion and deletion in the middle (O(1))

 Can grow and shrink dynamically

 Can represent complex data structures

Linked Lists Disadvantages:

 Slow random access (O(n))

 More memory overhead due to pointers

 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.

Q16 How can a stack be used to check if a parenthesis expression is balanced?

Answer: A stack can be used to check if a parenthesis expression is balanced by following these
steps:

 Push the opening parenthesis onto the stack.

 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.

 If the stack is not empty, then the expression is not balanced.

Q17 How do you convert a BST into a heap?


Answer: By performing an in-order traversal of the BST and inserting the elements into a heap.

Q18 How do you merge two heaps?

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.

Q21 Can Dynamic Memory Allocation Help in Managing Data?

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.

Q22 Explain the concept of a jagged array.


Answer: A jagged array is an array of arrays, where each sub-array could be of a different length.

You might also like