sts cat1
sts cat1
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
next = null;
class LoopDetection {
slow = slow.next;
fast = fast.next.next;
// If no loop exists
slow = slow.next;
fast = fast.next;
// Remove loop
fast.next = null;
return head;
// Test
head = solution.detectAndRemoveLoop(head);
class EvenOddList {
public ListNode segregateEvenOdd(ListNode head) {
if (curr.val % 2 == 0) {
if (evenHead == null) {
} else {
evenTail.next = curr;
evenTail = curr;
} else {
if (oddHead == null) {
} else {
oddTail.next = curr;
oddTail = curr;
curr = curr.next;
}
if (evenHead == null) return oddHead;
evenTail.next = oddHead;
oddTail.next = null;
return evenHead;
class StockSpan {
// Basic solution
int n = prices.length;
spans[0] = 1;
stack.push(0);
stack.pop();
stack.push(i);
}
return spans;
class StockSpanner {
public StockSpanner() {
int span = 1;
span += stack.pop()[1];
return span;
class Celebrity {
return true;
int candidate = 0;
// Find candidate
if (knows(candidate, i)) {
candidate = i;
// Verify candidate
if (i != candidate) {
return -1;
return candidate;
}
// 5. Maximum Sliding Window with Follow-up
class MaxSlidingWindow {
int n = nums.length;
deque.pollFirst();
deque.pollLast();
deque.offerLast(i);
if (i >= k - 1) {
result[i - k + 1] = nums[deque.peekFirst()];
}
}
return result;
class SlidingWindowMaximum {
private int k;
public SlidingWindowMaximum(int k) {
this.k = k;
window.offer(num);
deque.pollLast();
deque.offerLast(num);
// Remove element if window exceeds size k
if (window.size() > k) {
if (removed == deque.peekFirst()) {
deque.pollFirst();
return deque.peekFirst();
class StackPermutation {
int j = 0;
stack.push(num);
stack.pop();
j++;
}
return j == popped.length;
51. What happens to Floyd's cycle detection algorithm if the loop starts at the first node?
Answer: c
52. In a linked list with a loop, if the loop length is L and distance to loop start is K, when will the
pointers meet?
Answer: d
53. What is the minimum number of nodes required to form a loop in a linked list?
a) 1
b) 2
c) 3
d) 4
Answer: b
54. In a bitonic doubly linked list of length n, where can the peak element be located?
a) Only at n/2
Answer: c
55. What is the minimum number of comparisons needed to find the peak element in a bitonic
DLL?
a) O(1)
b) O(log n)
c) O(n)
d) O(n/2)
Answer: c
### Even/Odd Node Segregation
56. When segregating even and odd nodes, what should happen to nodes with value 0?
d) Should be removed
Answer: a
Answer: a
58. What is the advantage of using merge sort specifically for a doubly linked list?
d) Simpler implementation
Answer: b
59. During merge sort of DLL, when finding the middle node:
Answer: b
60. If we implement minimum stack using two stacks, what happens when we pop an element?
Answer: d
61. In a minimum stack, if we push the same minimum value multiple times:
Answer: a
a) n
b) n/2
c) 1
d) n-1
Answer: c
63. In the celebrity problem, if person A knows person B:
a) A cannot be celebrity
b) B must be celebrity
Answer: a
c) To save space
Answer: a
a) Random
b) Clockwise only
d) Counter-clockwise only
Answer: c
Answer: a
Answer: b
68. When implementing priority queue using DLL, insertion at beginning is done when:
c) Queue is empty
d) Both a and c
Answer: d
69. In a priority queue using DLL, what is the best case time complexity for insertion?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: a
### Maximum Sliding Window
70. In the maximum sliding window problem, what happens when window size equals array
length?
Answer: c