0% found this document useful (0 votes)
361 views42 pages

Codes

The document discusses loop detection in linked lists using Floyd's Tortoise and Hare algorithm. It describes how the algorithm works by using two pointers, a slow pointer and a fast pointer, to detect cycles in a linked list. It has a linear time complexity of O(n) and can detect loops in both singly and doubly linked lists.

Uploaded by

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

Codes

The document discusses loop detection in linked lists using Floyd's Tortoise and Hare algorithm. It describes how the algorithm works by using two pointers, a slow pointer and a fast pointer, to detect cycles in a linked list. It has a linear time complexity of O(n) and can detect loops in both singly and doubly linked lists.

Uploaded by

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

Loop Detection

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;
}

public static void display(Node head){


Node temp = head;
while(temp != null){
System.out.print(temp.val + " ");
temp = temp.next;
}
System.out.println();
}
public static void createCycle(Node head){
if( head == null || head.next == null )return ;
Node temp = head;
while(temp.next.next != null ) temp = temp.next;
Node h = temp.next;
Node temp1 = head;
while(temp1.val != h.val) temp1 = temp1.next;
if(temp1 != h)
temp.next = temp1;
}
public static boolean detectCycle(Node head){
if( head == null )return false;
Node a = head;
Node b = head;
while(b.next != null && b.next.next != null) {
a = a.next;
b = b.next.next;
if(a == b) return true;
}
return false;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n == 0) {
return;
}
Node head = null;
for(int i = 0; i < n ; i ++){
int temp = sc.nextInt();
head = insertNode(head, temp);
}
//display(head);
createCycle(head);

if(detectCycle(head) == true) System.out.println("YES");


else System.out.println("NO");
}
}

1. What is a node in a singly linked list?


A) The first element
B) The last element
C) An individual element
D) A pointer to the middle element

2. In a doubly linked list, each node contains how many pointers?


A) One
B) Two
C) Three
D) Four

3.In which type of linked list is loop detection not applicable?


A) Singly linked list
B) Doubly linked list
C) Circular linked list
D) Dynamic linked list

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

6. Which of the following is a drawback of the Floyd's algorithm?


A) It may not always detect loops
B) It has a high space complexity
C) It requires sorting the linked list
D) It cannot handle singly linked lists

7.What is a common method for detecting loops in a singly linked list?


A) Using a counter variable
B) Using a hash table
C)Using two pointers (slow and fast)
D) Traversing the list backward

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: Which of the following is a characteristic of a linked list with no loop?


A. The list is empty
B. The last node points to NULL
C. It has only one node
D. All nodes point to the same address
Question: In a linked list with a loop, how does Floyd's Tortoise and Hare algorithm detect
the loop?
A. By reversing the linked list
B. By using a hash table
C. By comparing node values
D. By detecting a cycle in 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.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- != 0){
ArrayList<Integer> l = new ArrayList<Integer>();
while(true){
int val = sc.nextInt();
if(val == -1) break;
l.add(val);
}
Collections.sort(l);
for(int i=0;i<l.size();i++){
System.out.print(l.get(i) + " ");
}
System.out.println();
}

}
}

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

2.What is a bitonic sequence?


A) A sequence of numbers with random order
B) A sequence of numbers that alternates between increasing and decreasing order
C) A sequence of numbers sorted in descending order
D) A sequence of numbers sorted in ascending order

3.Which algorithm is commonly used to sort a bitonic sequence?


A) Quick Sort
B) Merge Sort
C) Bitonic Sort
D) Bubble Sort
4.In the context of sorting a bitonic DLL, what does "bitonic merging" refer to?
A) Merging two sorted lists into a single sorted list
B) Merging two bitonic sequences into a single bitonic sequence
C) Combining two sorted arrays
D) Rearranging elements in a bitonic sequence

5.which data structure can be used to efficiently implement bitonic merging for a bitonic
DLL?
A) ArrayList
B) Stack
C) PriorityQueue
D) Deque

6. Which of the following is NOT a step involved in sorting a bitonic DLL?


A) Identifying the bitonic point
B) Splitting the list into two sublists
C) Merging the two sublists
D) Reversing the sublist before the bitonic point

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

9.What is the Space complexity of sorting a bitonic DLL with n elements?


A) O(1)
B) O(log n)
C) O(n log n)
D) O(n^2)

10.In a bitonic DLL, where does the bitonic point occur?


0/1
A) At the beginning of the list
B) At the end of the list
C) It can occur at any position
D) There is no bitonic point in a DLL

Question: What is a Bitonic sequence in the context of sorting?


A. A sequence with both ascending and descending parts
B. A strictly increasing sequence
C. A sequence with only equal elements
D. A random sequence
Question: Which sorting algorithm is typically used for sorting a Bitonic sequence?
A. Bubble Sort
B. Quick Sort
C. Merge Sort
D. Selection 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.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
ArrayList<Integer> even = new ArrayList<Integer>();
ArrayList<Integer> odd = new ArrayList<Integer>();

while(true){
int i = sc.nextInt();
if( i == -1) break;
if(i%2 == 0) even.add(i);
else odd.add(i);
}

for(int i = 0; i < odd.size(); i++) System.out.print(odd.get(i)+" ");


for(int i = 0; i < even.size(); i++) System.out.print(even.get(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

3.How can you identify whether a number is even or odd?


0/1

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

a) To minimize time complexity


b) To minimize space complexity
c) To create a new linked list

d) To maximize the number of operations

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

a) Creating two separate linked lists


b) Rearranging nodes within the existing list

c) Using additional memory for a stack


d) Swapping nodes in place

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

a) It points to the previous node.


b) It stores the address of the head node.

c) It is not relevant to this operation.


d) It is used for deleting nodes.
Correct answer
a) It points to the previous node.

7.In an in-place solution for segregating even and odd nodes, what is the final step
after rearranging the nodes?
0/1

a) Reversing the entire linked list


b) Swapping the even and odd parts

c) Updating the "tail" pointer of the even part


d) Merging the even and odd parts

Correct answer
c) Updating the "tail" pointer of the even part

8.Which pointer do you need to update when rearranging nodes in an in-place


solution for segregating even and odd nodes?
0/1

a) Only the next pointer


b) Only the previous pointer

c) Both next and previous pointers


d) Neither next nor previous pointers

Correct answer
c) Both next and previous pointers

9. What data structure is commonly used to represent a singly linked list?


0/1

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

c) To reverse the order of even nodes


d) To switch the positions of even and odd 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

a) A sorting algorithm that operates only on arrays


b) A sorting algorithm that operates only on linked lists

c) A divide-and-conquer sorting algorithm


d) A sorting algorithm that uses a stack data structure

Correct answer
c) A divide-and-conquer sorting algorithm

2. Why is Merge Sort preferred for sorting DLLs?


0/1

a) It has a lower time complexity compared to other sorting algorithms


b) It doesn't require additional memory for sorting DLLs

c) It is easy to implement for DLLs


d) It efficiently utilizes the DLL's bidirectional traversal capability

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

4. In Merge Sort for DLLs, what is the base case?


0/1

a) When the DLL has a single element


b) When the DLL has two elements

c) When the DLL is empty


d) When the DLL has three elements

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

c) By finding the middle element


d) By dividing it into an even and an odd sublist

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

c) To combine and sort two sorted sublists


d) To find the middle element of 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

a) By using a temporary linked list


b) By using a stack data structure

c) By comparing and rearranging nodes from both DLLs


d) By reversing one of the DLLs
Correct answer
c) By comparing and rearranging nodes from both DLLs

9. In Merge Sort, how is the DLL divided during the recursive process?
1/1

a) Into three equal parts


b) Into two equal parts

c) Into four equal parts


d) It is not divided

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

c) It depends on the implementation


d) It is stable only for small DLLs

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.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
ArrayList<Integer> l = new ArrayList<Integer>();
int n = sc.nextInt();
while(n-- != 0){
int size = sc.nextInt();
for(int i=0;i<size;i++){
int val = sc.nextInt();
l.add(val);
}
Collections.sort(l);
System.out.println(l.get(0));
}
}
}

1. What is a stack data structure?


1/1

A) A linear data structure with a FIFO (First-In-First-Out) order.


B) A linear data structure with a LIFO (Last-In-First-Out) order.

C) A hierarchical data structure.


D) A non-linear data structure.

2.Which operation involves inserting an item into a stack?


0/1

A) Push
B) Pop

C) Peek
D) Swap

Correct answer
A) Push

3.Which operation involves deleting an item from the stack?


1/1
A) Push
B) Pop

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

A) Using an additional stack to track minimums.


B) Storing the minimum element in a variable.

C) Scanning the entire stack to find the minimum when needed.


D) Using a priority queue.

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

A) "Get minimum" stack has a smaller memory footprint.


B) "Get minimum" stack has a faster time complexity.

C) "Get minimum" stack allows elements to be in any order.


D) "Get minimum" stack has fewer operations.

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.

C) The minimum element is pushed onto the auxiliary stack.


D) The stack becomes empty.

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: What is a potential limitation of a minimum stack compared to a regular stack?


A. Higher time complexity for push operation
B. Increased space complexity
C. Limited support for dynamic resizing
D. Inability to handle negative numbers

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.*;

public class Solution {


public static int celeb(ArrayList<ArrayList<Integer>> M, int n) {

Stack<Integer> st = new Stack<>();


for(int i = 0 ; i < n; i ++ ){
st.push(i);
}

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);
}

int pot = st.pop();


for(int i = 0; i < n; i ++)
if(M.get(pot).get(i) != 0 && M.get(i).get(pot) != 1)
return -1;

return pot;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
ArrayList<ArrayList<Integer>> ll = new ArrayList<>();
int n = sc.nextInt();
for(int i = 0; i < n; i ++ ){
ArrayList<Integer> l = new ArrayList<>();
for(int j = 0 ; j < n; j ++ ){
int num = sc.nextInt();
l.add(num);
}
ll.add(l);
}
int cel = celeb(ll, n);
if(cel == -1) System.out.println("No Celebrity");
else System.out.println(cel);
}
}
1. What is the Celebrity Problem in computer science?
0/1

A) A problem related to identifying famous people in a social network.


B) A problem related to finding a person who is widely recognized.

C) A problem of identifying a person who is known by everyone but knows no one.


D) A problem of identifying a popular movie star.

Correct answer
C) A problem of identifying a person who is known by everyone but knows no one.

2. In the Celebrity Problem, what does it mean for someone to be a "celebrity"?


0/1

A) They have a large social media following.


B) They are famous in their field.

C) They are known by everyone but know no one.


D) They are a popular public figure.

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

A) At least one person.


B) At least two people.

C) At least half of the total people.


D) Everyone in the group.
Correct answer
D) Everyone in the group.

5.In the Celebrity Problem, what is the primary goal of the algorithm?
0/1

A) To identify the most famous person.


B) To minimize the number of comparisons.

C) To find a person with the largest social network following.


D) To find a person known by everyone.

Correct answer
D) To find a person known by everyone.

6.How is the Celebrity Problem typically represented?


0/1

A) Using a circular linked list.


B) With a binary search tree.

C) Using a square matrix.


D) Through a hash table.

Correct answer
C) Using a square matrix.

7. Which factor primarily influences the time complexity of the Celebrity Problem
algorithm?
1/1

A) The number of iterations.


B) The number of comparisons.

C) The number of recursive calls.


D) The number of celebrities.

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: What is the primary objective in solving the Celebrity Problem?


A. Identifying the most famous person
B. Determining if a person is famous
C. Finding a person who knows the most people
D. Identifying a person known by everyone

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 class Solution {

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

tower( n-1, aux, dest, source );


}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
tower(n,'a','c','b');
}
}

1.In the Tower of Hanoi problem, what is the objective?


A. To move all disks from one rod to another rod.
B. To sort the disks in ascending order.
C. To find the largest disk.
D. To find the total number of moves required.

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.

10.What is the space complexity of the iterative Tower of Hanoi solution?


A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)

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.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

int price[] = new int[n];


for(int i = 0 ; i < n; i ++) price[i] = sc.nextInt();

int arr[] = new int[n];


arr[0] = 1;
for(int i = 1; i < n; i ++ ){
arr[i] = 1;
for(int j = i - 1; j >= 0 && price[i]>=price[j]; j --) arr[i]++;
}

for(int i = 0 ; i < n; i ++) System.out.print(arr[i]+" ");

}
}

Question: What does the Stock Span Problem aim to calculate?


A. Total stock value
B. Maximum stock price
C. Minimum stock price
D. The span of each stock's price

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)

Priority Queue using DLL


1. What is a Priority Queue?
0/1

A. A queue with fixedsize capacity


B. A queue with variablesize capacity

C. A queue that follows Last In First Out (LIFO) order


D. A queue where each element has an associated priority

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

A. FIFO (First-In-First-Out) behavior


B. LIFO (Last-In-First-Out) behavior

C. Elements are ordered by priority


D. No ordering of elements

Correct answer
C. Elements are ordered by priority

5. How are elements stored in a Priority Queue based on a DLL?


1/1

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

7.How is the highest-priority element determined in a Priority Queue based on a


DLL?

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

8.What is the time complexity of inserting an element into a Priority Queue


implemented with a DLL, assuming the DLL is already sorted by priority?
0/1

A. O(1)
B. O(log n)

C. O(n)
D. O(n log n)

Correct answer
C. O(n)

9.What is the space complexity of a Priority Queue implemented with a DLL?


1/1

A. O(1)
B. O(n)

C. O(log n)
D. O(n log n)

10.Which operation is used to add an element to a Priority Queue implemented with


a DLL?
1/1

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.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> l = new ArrayList<Integer>();
for(int i = 0; i < n; i ++){
l.add(sc.nextInt());
}
Collections.sort(l);
for(int i = 0; i < n; i ++){
System.out.print(l.get(i)+" ");
}
}
}
1.Which sorting algorithm divides the array into a sorted and an unsorted region and
repeatedly selects the minimum element from the unsorted region and moves it to
the sorted region?
0/1

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

5.In-place sorting means that:


0/1

a) Extra space is not used


b) Extra space is used

c) The algorithm is fast


d) The algorithm is stable

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

7.Which sorting algorithm's performance is significantly affected by the choice of the


pivot element?
0/1

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

9.Which sorting algorithm is not a comparison-based sorting algorithm and is


suitable for integers or fixed-length strings?
0/1
a) Insertion Sort
b) Heap 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

c) Using additional memory to speed up the sorting process


d) Sorting elements in a separate data structure
Question: In the context of sorting without extra space, which sorting algorithm is often
used?
A. Quick Sort
B. Merge Sort
C. Bubble Sort
D. Insertion Sort

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

Question: What is a potential drawback of in-place sorting algorithms in terms of time


complexity?
A. Higher time complexity compared to other algorithms
B. Limited support for parallel processing
C. Increased space complexity
D. Averages case time complexity may be higher
Max Sliding Window
import java.io.*;
import java.util.*;

public class Solution {


public static boolean stackPerm(int org[], int tar[]){
Stack<Integer> st = new Stack<Integer>();
int i = 0;
for( int element : org) {
st.push(element);
while(!st.isEmpty() && st.peek() == tar[i]){
st.pop();
i++;
}
}
return st.isEmpty();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int org[] = new int[3];
int tar[] = new int[3];
for(int i = 0; i < 3; i ++) org[i] = sc.nextInt();
for(int i = 0; i < 3; i ++) tar[i] = sc.nextInt();

if(stackPerm(org, tar)) System.out.println("YES");


else System.out.println("Not Possible");
}
}
1. What is the Max Sliding Window problem about?
0/1

a) Finding the minimum element in a sliding window


b) Finding the maximum element in a sliding window

c) Counting the number of elements in a sliding window


d) Summing the elements in a sliding window

Correct answer
a) Finding the minimum element in a sliding window

2. In the Max Sliding Window problem, what is the "window size"?


0/1

a) The size of the entire array


b) The number of windows in the array
c) The number of elements in each window
d) The number of distinct elements in the array

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

a) A visual representation of the array


b) A graphical user interface for data visualization

c) A fixed-size subarray moving through the original array


d) A software window displaying the array's contents

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.*;

public class Solution {


public static boolean stackPerm(int org[], int tar[]){
Stack<Integer> st = new Stack<Integer>();
int i = 0;
for( int element : org) {
st.push(element);
while(!st.isEmpty() && st.peek() == tar[i]){
st.pop();
i++;
}
}
return st.isEmpty();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int org[] = new int[3];
int tar[] = new int[3];
for(int i = 0; i < 3; i ++) org[i] = sc.nextInt();
for(int i = 0; i < 3; i ++) tar[i] = sc.nextInt();

if(stackPerm(org, tar)) System.out.println("YES");


else System.out.println("Not Possible");
}
}

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)

You might also like