Codes
Codes
import java.io.*;
import java.util.*;
class Node{
int val;
Node next;
Node(int num){
val = num;
next = null;
}
}
public class Solution {
public static Node insertNode(Node head, int num){
Node newNode = new Node(num);
if(head == null){
head = newNode;
return head;
}
Node temp = head;
while(temp.next != null) temp = temp.next;
temp.next = newNode;
return head;
}
4.Which of the following data structures is NOT typically used to implement a linked list?
A) Array
B) Pointer
C) Node
D) Reference
5. In a circular linked list, what is the difference between the last node's "next" pointer and
the first node's "next" pointer?
A) There is no difference
B) The last node points to the first node
C) The first node points to the last node
D) The last node points to null
8. What is the time complexity of the Floyd's algorithm for loop detection in a linked list?
A) O(1)
B) O(n)
C) O(log n)
D) O(n^2)
9. What is the space complexity of the Floyd's algorithm for loop detection in a linked list?
A) O(1)
B) O(n)
C) O(log n)
D) O(n^2)
10.Which of the following statements is true when comparing hashing and Floyd's Tortoise
algorithm for loop detection in linked lists or sequences?
A) Hashing guarantees constant time complexity for loop detection.
B) Floyd's Tortoise algorithm guarantees linear time complexity for loop detection.
C) Hashing requires additional memory to store hash values.
D) Floyd's Tortoise algorithm is not suitable for loop detection in singly linked lists.
Question: What is a Floyd's Tortoise and Hare algorithm used for in the context of linked
lists?
A. Sorting
B. Loop Detection
C. Merging
D. Reverse the linked list
Question: What is the time complexity of Floyd's Tortoise and Hare algorithm for loop
detection in a linked list?
A. O(n)
B. O(log n)
C. O(n^2)
D. O(1)
Question: Which of the following data structures is commonly used for loop detection in
linked lists?
A. Array
B. Stack
C. Hash Table
D. Queue
Sort the bitonic DLL
import java.io.*;
import java.util.*;
}
}
1.Which of the following data structures is suitable for implementing a bitonic doubly linked
list (DLL)?
A) ArrayList
B) LinkedList
C) Stack
D) PriorityQueue
5.which data structure can be used to efficiently implement bitonic merging for a bitonic
DLL?
A) ArrayList
B) Stack
C) PriorityQueue
D) Deque
7. How many sorting passes are required to completely sort a bitonic DLL?
A) One
B) Two
C) Three
D) It depends on the size of the list
8.Which sorting algorithm is commonly used to sort the two sublists in a bitonic DLL?
A) Bubble Sort
B) Quick Sort
C) Merge Sort
D) Insertion Sort
Question: In Bitonic DLL, what is the role of the merging step in the sorting process?
A. To rearrange elements in descending order
B. To split the list into subproblems
C. To combine two sorted halves into a single sorted list
D. To remove duplicates from the list
Question: What is the time complexity of sorting a Bitonic DLL using the Bitonic Sort
algorithm?
A. O(n log n)
B. O(n^2)
C. O(log n)
D. O(n)
Question: Which step is critical for achieving the bitonic property in a Bitonic DLL?
A. Merging
B. Sorting
C. Splitting
D. Reversing
Segregate even & odd nodes in a LL
import java.io.*;
import java.util.*;
while(true){
int i = sc.nextInt();
if( i == -1) break;
if(i%2 == 0) even.add(i);
else odd.add(i);
}
}
}
1.To segregate even and odd nodes, you need to iterate through the linked list once.
0/1
a) True
b) False
Correct answer
a) True
2.Which of the following operations is NOT typically required when segregating even
and odd nodes in a linked list?
1/1
a) Insertion
b) Deletion
c) Swapping
d) Sorting
a) Check if it is divisible by 2
b) Check if it is a prime number
c) Check if it is a multiple of 5
d) Check if it is greater than 10
Correct answer
a) Check if it is divisible by 2
4.In an in-place solution for segregating even and odd nodes, what is the primary
goal?
0/1
Correct answer
b) To minimize space complexity
5.Which of the following is NOT an in-place method for segregating even and odd
nodes?
0/1
Correct answer
a) Creating two separate linked lists
6. In the context of segregating even and odd nodes, what is the significance of the
"previous" pointer?
0/1
7.In an in-place solution for segregating even and odd nodes, what is the final step
after rearranging the nodes?
0/1
Correct answer
c) Updating the "tail" pointer of the even part
Correct answer
c) Both next and previous pointers
a) Array
b) Stack
c) Queue
d) Linked List
Correct answer
d) Linked List
10.In the in-place approach, what is the role of the "evenTail" pointer?
0/1
a) To track the last even node in the list
b) To count the number of even nodes
Correct answer
a) To track the last even node in the list
Question: Which of the following is an efficient approach to segregate even and odd nodes
in a linked list?
A. Bubble Sort
B. Quick Sort
C. Merge Sort
D. Iterative traversal
Question: What is the key idea behind segregating even and odd nodes in a linked list?
A. Sorting based on node values
B. Rearranging nodes based on index
C. Grouping nodes based on parity
D. Removing duplicate nodes
Question: In the context of linked lists, what is the significance of maintaining the relative
order of even and odd nodes during segregation?
A. It does not matter
B. Required for stability
C. Reduces time complexity
D. Improves space complexity
Question: Which time complexity is achievable for the even-odd segregation algorithm in a
linked list?
A. O(n)
B. O(n^2)
C. O(log n)
D. O(1)
Question: What is the role of pointers in the segregation of even and odd nodes in a linked
list?
A. To perform arithmetic operations
B. To maintain the order of nodes
C. To implement recursion
D. To access neighboring nodes
Merge sort for DLL
1.What is Merge Sort?
0/1
Correct answer
c) A divide-and-conquer sorting algorithm
Correct answer
d) It efficiently utilizes the DLL's bidirectional traversal capability
3. What is the time complexity of Merge Sort for DLLs with 'n' elements?
1/1
a) O(n)
b) O(n * log(n))
c) O(n^2)
d) O(log(n))
Correct answer
a) When the DLL has a single element
5. How is a DLL divided during the Merge Sort process?
0/1
a) By selecting a random pivot element
b) By splitting it into two equal halves
Correct answer
c) By finding the middle element
6. What is the purpose of the "Merge" function in Merge Sort for DLLs?
0/1
a) To split the DLL into two sublists
b) To reverse the order of elements in the DLL
Correct answer
c) To combine and sort two sorted sublists
7. In Merge Sort for DLLs, which data structure is used to perform the merging of
sublists efficiently?
0/1
a) Array
b) Stack
c) Queue
d) Recursion
Correct answer
a) Array
8. In Merge Sort for DLLs, how do you merge two sorted DLLs?
0/1
9. In Merge Sort, how is the DLL divided during the recursive process?
1/1
10. Which of the following is true about the stability of Merge Sort for DLLs?
0/1
a) It is always stable
b) It is never stable
Correct answer
a) It is always stable
Question: Why is Merge Sort a preferred choice for sorting a doubly linked list?
A. It has a lower space complexity
B. It is an in-place sorting algorithm
C. It works well with linked lists
D. It has a faster average case time complexity
Question: What is the key step in the merge sort algorithm for doubly linked lists?
A. Partitioning the list
B. Merging sorted sublists
C. Swapping adjacent elements
D. Reversing the list
Question: What is the time complexity of the merge step in the merge sort algorithm for
doubly linked lists?
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
Question: How does merge sort maintain stability during sorting in a doubly linked list?
A. By using random pivot elements
B. By comparing node values
C. By maintaining the original order of equal elements
D. By reversing the list at the end
Question: What is the space complexity of the merge sort algorithm for doubly linked lists?
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
Minimum Stack
import java.io.*;
import java.util.*;
A) Push
B) Pop
C) Peek
D) Swap
Correct answer
A) Push
C) Peek
D) Swap
4.Which operation involves displaying the contents of the stack without removing it?
0/1
A) Push
B) Pop
C) Peek
D) Swap
Correct answer
C) Peek
5.Which operation allows you to retrieve the minimum element from a stack
efficiently?
0/1
A) getMin()
B) findMinimum()
C) retrieveMinimum()
D) minimumElement()
Correct answer
A) getMin()
6. Which of the following is NOT a valid approach to implement the "get minimum"
operation for a stack?
0/1
Correct answer
C) Scanning the entire stack to find the minimum when needed.
7.What is the advantage of using a "get minimum" stack over scanning the entire
stack to find the minimum when needed?
1/1
8.In a "get minimum" stack, what happens when you pop the minimum element from
the stack?
0/1
A) The minimum element is lost, and you cannot retrieve it.
B) The minimum element is moved to the top of the stack.
Correct answer
A) The minimum element is lost, and you cannot retrieve it.
9.What is the time complexity of the "get minimum" operation for a stack
implemented with an additional stack to track minimums?
0/1
A) O(1)
B) O(log N)
C) O(N)
D) O(N^2)
Correct answer
A) O(1)
10.What is the space complexity of a stack without any additional data structures?
0/1
A) O(1)
B) O(N)
C) O(log N)
D) O(N^2)
Correct answer
A) O(1)
Question: What is the primary advantage of a minimum stack over a regular stack?
A. Faster push and pop operations
B. Reduced space complexity
C. Quick access to the minimum element
D. Support for parallel processing
Question: How is the minimum element updated in a minimum stack when a new element is
pushed onto it?
A. By comparing with the top element
B. By keeping a separate list of minimum elements
C. By using a hash table
D. By iterating through the entire stack
Question: In the context of a minimum stack, what is the time complexity of the push
operation?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Question: How does a minimum stack ensure constant-time retrieval of the minimum
element?
A. By using a hash table
B. By storing the minimum value with each element
C. By performing a linear search
D. By using a priority queue
The Celebrity problem
import java.io.*;
import java.util.*;
while(st.size() >= 2 ) {
int i = st.pop();
int j = st.pop();
if(M.get(i).get(j) == 1){
st.push(j);
}
else st.push(i);
}
return pot;
}
Correct answer
C) A problem of identifying a person who is known by everyone but knows no one.
Correct answer
C) They are known by everyone but know no one.
3. Which data structure is commonly used to solve the Celebrity Problem efficiently?
0/1
A) Stack
B) Queue
C) Graph
D) Array
Correct answer
A) Stack
4. In the Celebrity Problem, how many people need to vouch for someone to be
considered a celebrity?
0/1
5.In the Celebrity Problem, what is the primary goal of the algorithm?
0/1
Correct answer
D) To find a person known by everyone.
Correct answer
C) Using a square matrix.
7. Which factor primarily influences the time complexity of the Celebrity Problem
algorithm?
1/1
8.How does the time complexity of the optimized Celebrity Problem algorithm scale
with an increase in the number of people in the group?
0/1
A) It remains constant.
B) It decreases.
C) It increases linearly.
D) It increases exponentially.
Correct answer
C) It increases linearly.
9. What is the time complexity of the optimized algorithm for solving the Celebrity
Problem?
0/1
A) O(1)
B) O(log N)
C) O(N)
D) O(N^2)
Correct answer
C) O(N)
10. What is the space complexity of the optimized algorithm for solving the Celebrity
Problem?
1/1
A) O(1)
B) O(N)
C) O(log N)
D) O(N^2)
Question: In the Celebrity Problem, what is the definition of a "celebrity"?
A. A famous person
B. A person who knows everyone
C. A person who is known by everyone
D. A person with a large social media following
Question: Which data structure is commonly used to solve the Celebrity Problem efficiently?
A. Stack
B. Queue
C. Graph
D. Array
Question: What is the time complexity of the efficient algorithm for solving the Celebrity
Problem?
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(1)
Question: What is the significance of the elimination step in the Celebrity Problem algorithm?
A. Reducing time complexity
B. Avoiding unnecessary comparisons
C. Ensuring a celebrity is found
D. Minimizing space complexity
Iterative Tower of Hanoi
import java.io.*;
import java.util.*;
public static void tower(int n, char source, char dest, char aux){
if(n == 1) {
System.out.println(source+" "+dest);
return;
}
tower( n-1, source, aux , dest );
System.out.println(source+" "+dest);
2. How many rods are used in the classic Tower of Hanoi problem?
A. 1
B. 2
C. 3
D. 4
3.In the iterative solution to the Tower of Hanoi, how is the problem divided into
subproblems?
A. Divide the disks into two equal parts.
B. Divide the disks into three equal parts.
C. Divide the disks into smaller and smaller subproblems.
D. Divide the disks into four equal parts.
4.What is the minimum number of moves required to solve the Tower of Hanoi
problem with 4 disks?
A. 4
B. 7
C. 15
D. 31
5.Which data structure is commonly used to implement the iterative solution to the
Tower of Hanoi problem?
A. Stack
B. Queue
C. Array
D. LinkedList
6.In the iterative Tower of Hanoi solution, how are the disks moved between the
rods?
A. Using recursion
B. Using a loop
C. Using dynamic programming
D. Using a binary tree
7.What is the time complexity of the iterative solution to the Tower of Hanoi problem
with 'n' disks?
A. O(n)
B. O(2^n)
C. O(log n)
D. O(n^2)
8.In the iterative Tower of Hanoi solution, what is the purpose of using a stack data
structure?
A. To store the rods.
B. To keep track of the number of disks.
C. To maintain the order of disk movements.
D. To simulate the recursive calls in an iterative manner.
9.How does the iterative Tower of Hanoi solution ensure that the correct disk
movement order is maintained?
A. By using a queue data structure.
B. By using a depth-first search algorithm.
C. By using a loop and a stack data structure.
D. By using a breadth-first search algorithm.
Question: What is the minimum number of moves required to solve the Tower of Hanoi
problem with n disks?
A. n
B. 2^n - 1
C. 2n
D. n!
Question: In the iterative solution to the Tower of Hanoi, what data structure is commonly
used to simulate the recursive calls?
A. Stack
B. Queue
C. Linked List
D. Priority Queue
Question: What is the key idea behind the iterative Tower of Hanoi algorithm?
A. Using dynamic programming
B. Simulating recursive calls with a stack
C. Dividing the problem into subproblems
D. Sorting the disks based on size
Question: How does the time complexity of the iterative Tower of Hanoi algorithm compare
to the recursive solution?
A. It is higher
B. It is lower
C. It is the same
D. It depends on the number of disks
Question: What is the role of the auxiliary peg in the iterative Tower of Hanoi algorithm?
A. Storing the smallest disk
B. Facilitating the movement of disks
C. Preventing the use of additional memory
D. Representing the destination peg
Stock Span problem
import java.io.*;
import java.util.*;
}
}
Question: In the context of the Stock Span Problem, what does the "span" of a stock refer
to?
A. The price of the stock
B. The difference between the highest and lowest prices
C. The number of consecutive days the stock price is less than a certain threshold
D. The number of consecutive days the stock price is greater than or equal to the
current day
Question: Which data structure is commonly used to efficiently solve the Stock Span
Problem?
A. Stack
B. Queue
C. Hash Table
D. Linked List
Question: How does the Stock Span Problem algorithm utilize a stack to calculate the spans
of stock prices?
A. By maintaining a running sum
B. By storing indices in the stack
C. By sorting the stock prices
D. By using a priority queue
Question: What is the time complexity of the Stock Span Problem algorithm using a stack?
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(1)
Correct answer
D. A queue where each element has an associated priority
2. Which data structure is suitable for implementing a Priority Queue using a DLL?
0/1
A. Stack
B. Queue
C. Linked List
D. Binary Heap
Correct answer
C. Linked List
3. In a Priority Queue implemented with a DLL, which operation takes O(1) time
complexity?
0/1
A. Insertion
B. Deletion
C. Searching
D. Traversal
Correct answer
A. Insertion
4. What is the key feature of a Priority Queue that differentiates it from a regular
queue?
0/1
Correct answer
C. Elements are ordered by priority
A. In a random order
B. In ascending order
C. In descending order
D. In order of insertion
6. Which operation is used to remove and return the highest-priority element from a
Priority Queue implemented with a DLL?
0/1
A. pop()
B. peek()
C. poll()
D. push()
Correct answer
C. poll()
1/1
A. By its position in the DLL
B. By the value of its key
C. By its index in the DLL
D. By its order of insertion
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Correct answer
C. O(n)
A. O(1)
B. O(n)
C. O(log n)
D. O(n log n)
A. add()
B. push()
C. insert()
D. enqueue()
Question: What is a DLL (Doubly Linked List) commonly used for in the context of data
structures?
A. To represent a binary tree
B. To implement a queue
C. To store a collection of elements with quick access to the middle
D. To facilitate hash table operations
Question: What is the key advantage of using a doubly linked list to implement a priority
queue?
A. Reduced space complexity
B. Faster insertion and deletion operations
C. Quick access to the minimum element
D. Improved cache locality
Question: How is the priority maintained in a priority queue implemented using a doubly
linked list?
A. By using a separate array for priorities
B. By comparing node values
C. By using a binary heap
D. By sorting the linked list
Question: What is the time complexity of the insertion operation in a priority queue
implemented using a doubly linked list?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Question: What is a potential drawback of using a doubly linked list for a priority queue
compared to other data structures?
A. Higher time complexity for insertion
B. Increased space complexity
C. Limited support for dynamic resizing
D. Inability to handle duplicate priorities
Sort without extra Space
import java.io.*;
import java.util.*;
a) Insertion Sort
b) Quick Sort
c) Selection Sort
d) Heap Sort
Correct answer
c) Selection Sort
2.Which sorting algorithm uses a pivot element and partitions the array into two
subarrays such that elements less than the pivot are on the left and elements greater
than the pivot are on the right?
0/1
a) Merge Sort
b) Insertion Sort
c) Quick Sort
d) Radix Sort
Correct answer
c) Quick Sort
3. Which sorting algorithm works by repeatedly dividing the input array into two
subarrays, sorting them, and then merging them?
0/1
a) Quick Sort
b) Insertion Sort
c) Merge Sort
d) Selection Sort
Correct answer
c) Merge Sort
4.In the context of "sorting without extra space," which data structure is commonly
used for in-place sorting algorithms?
0/1
a) Linked List
b) Hash Table
c) Binary Tree
d) Priority Queue
Correct answer
a) Linked List
Correct answer
a) Extra space is not used
6.Which sorting algorithm is often used to implement priority queues due to its heap
data structure?
0/1
a) Merge Sort
b) Quick Sort
c) Radix Sort
d) Heap Sort
Correct answer
d) Heap Sort
a) Insertion Sort
b) Bubble Sort
c) Selection Sort
d) Quick Sort
Correct answer
d) Quick Sort
8.Which sorting algorithm does not perform well with duplicate values in the array
and can be unstable?
0/1
a) Quick Sort
b) Insertion Sort
c) Selection Sort
d) Merge Sort
Correct answer
a) Quick Sort
c) Radix Sort
d) Quick Sort
Correct answer
c) Radix Sort
10.What does it mean to "sort without extra space" in the context of sorting
algorithms?
1/1
a) Creating a new array to store the sorted elements
b) Rearranging elements in the original array to achieve a sorted order
Question: What is the primary challenge in implementing a sorting algorithm without using
extra space?
A. Handling duplicate elements
B. Achieving stability
C. Minimizing time complexity
D. In-place rearrangement of elements
Question: How does an in-place sorting algorithm differ from other sorting algorithms?
A. It uses additional arrays for sorting
B. It rearranges elements within the existing array without using extra space
C. It always has a lower time complexity
D. It requires more memory compared to other algorithms
Question: Which sorting algorithm can be adapted for in-place sorting without using extra
space efficiently?
A. Merge Sort
B. Bubble Sort
C. Insertion Sort
D. Selection Sort
Correct answer
a) Finding the minimum element in a sliding window
Correct answer
c) The number of elements in each window
3. Which data structure is commonly used to efficiently solve the Max Sliding
Window problem?
0/1
a) Linked List
b) Stack
c) Queue
d) Array
Correct answer
c) Queue
4.What is the time complexity of the Naive Approach for solving the Max Sliding
Window problem, where 'N' is the size of the array, and 'K' is the window size?
1/1
a) O(N)
b) O(N * K)
c) O(N * log(K))
d) O(N^2)
5.Which approach is efficient for small windows or large arrays when solving the Max
Sliding Window problem?
1/1
a) Naive Approach
b) Using Self-balancing Tree
c) Using Max-Heap
d) Using Deque
6.When using a self-balancing tree for the Max Sliding Window problem, what is the
primary benefit of this approach?
0/1
a) It has the best time complexity for all scenarios.
b) It requires the least amount of memory.
c) It is simple and easy to implement.
d) It can efficiently handle large windows in large arrays.
Correct answer
a) It has the best time complexity for all scenarios.
7.What is the time complexity of the approach that uses a Max-Heap for the Max
Sliding Window problem?
0/1
a) O(N)
b) O(N * K)
c) O(N * log(K))
d) O(K * log(K))
Correct answer
c) O(N * log(K))
8.What is the space complexity of the approach that uses a Max-Heap for the Max
Sliding Window problem?
1/1
a) O(N)
b) O(K)
c) O(N * log(K))
d) O(K * log(K))
9. Which approach is the most efficient in terms of both time and space complexity
for solving the Max Sliding Window problem, particularly when dealing with large
windows?
0/1
a) Naive Approach
b) Using Self-balancing Tree
c) Using Max-Heap
d) Using Deque
Correct answer
d) Using Deque
10.What does the term "sliding window" refer to in this problem?
0/1
Correct answer
c) A fixed-size subarray moving through the original array
Question: In the context of the Max Sliding Window problem, what does the "sliding window"
represent?
A. A graphical representation of the array
B. A fixed-size subarray moving through the main array
C. The maximum value in the entire array
D. The minimum value in the entire array
Question: What is the primary objective of the Max Sliding Window problem?
A. Finding the maximum element in the array
B. Identifying the position of the maximum element
C. Determining the maximum element in each subarray of a fixed size
D. Sorting the array in descending order
Question: Which data structure is commonly used to efficiently solve the Max Sliding
Window problem?
A. Stack
B. Queue
C. Hash Table
D. Linked List
Question: What is the time complexity of the efficient algorithm for solving the Max Sliding
Window problem?
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(1)
Question: What is the significance of using a doubly-ended queue (deque) in the Max Sliding
Window algorithm?
A. To minimize space complexity
B. To maintain a running sum
C. To efficiently track the maximum element in the sliding window
D. To sort the elements in the window
Stack permutations
import java.io.*;
import java.util.*;
Question: In the context of stack permutations, what does a valid permutation represent?
A. A random arrangement of elements
B. A sequence of elements in ascending order
C. A sequence of elements that can be obtained by performing stack operations
D. A sequence of elements with no duplicates
Question: What is the key property of a stack permutation that distinguishes it from other
permutations?
A. It always starts with the maximum element
B. It always ends with the minimum element
C. It can be obtained by a specific sequence of push and pop operations on a stack
D. It contains only odd or even elements
Question: What is the significance of using a stack in checking whether a given permutation
is valid?
A. To reduce time complexity
B. To maintain the order of elements
C. To simulate the permutation process
D. To sort the elements
Question: How does the concept of a stack permutation relate to the properties of a stack
data structure?
A. It guarantees constant-time push and pop operations
B. It ensures that elements are always sorted
C. It follows the Last In, First Out (LIFO) principle
D. It minimizes space complexity
Question: What is the time complexity of checking whether a given permutation is a valid
stack permutation?
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(1)