Q1.
You are given a singly linked list of size n, where each node contains two pointers:
next pointer, which points to the next node in the list,
random pointer, which points to any random node in the list or can be NULL.
Your task is to create a deep copy (clone) of this list.
The cloned list should have the same structure, where the next and random pointers of the new
nodes replicate the corresponding connections in the original list, but with completely new nodes
(no shared nodes with the original list).
Constraints:
The number of nodes n is in the range [0, 10^5].
You must do this with O(n) time complexity and O(1) additional space complexity (apart
from the new list).
Q2. You are given an array arr[] of size n, containing both positive and negative integers.
Your task is to find the first negative integer in every contiguous window of size k.
If there is no negative integer in a window, output 0 for that window.
Q3. You are at a party with n people (labeled from 0 to n-1).
A celebrity is defined as someone who:
Is known by everyone else (i.e., all other n-1 people know the celebrity).
Knows no one (i.e., the celebrity does not know any other person).
You are given a knowledge matrix M[][] of size n x n, where M[i][j] == 1 means person i
knows person j, and M[i][j] == 0 means person i does not know person j.
Your task is to find the index of the celebrity if there is one.
If there is no celebrity, return -1.
Constraints:
2 <= n <= 1000
M[i][i] == 0 for all 0 <= i < n (a person does not know themselves)
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1) (apart from the input)
Q4. You are given a rotated sorted array arr[] of n distinct integers. Initially, the array was
sorted in ascending order but was rotated at an unknown pivot. Your task is to search for a target
element in the array and return its index.
Q5. You are given a list of flight routes represented by a 2D array flights[][], where each
flight is represented as a tuple (u, v, w):
u is the starting city,
v is the destination city,
w is the price of the flight.
Additionally, you are given:
A source city src and a destination city dst,
An integer k, which is the maximum number of stops allowed.
Your task is to determine the cheapest price from the source city to the destination city, such
that the total number of stops does not exceed k.
If there is no such route, return -1.
Q6. You are given a string s of lowercase and uppercase English letters. Your task is to generate
all possible abbreviations for the string using bit manipulation.
In the abbreviation, a number represents a series of consecutive characters replaced by that
number. For example:
"word" → "w1d" (abbreviation: 1 represents "o", and "r").
"hello" → "h3o" (abbreviation: 3 represents "ell").
You are required to print all possible valid abbreviations, where each abbreviation can be
formed by:
Keeping a character as is.
Replacing a sequence of consecutive characters by their count.
Q7. There are n gas stations along a circular route, where the amount of gas at the ith station is
gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the
ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the
gas stations.Given two integer arrays gas and cost, return the starting gas station's index if you
can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists
a solution, it is guaranteed to be unique.
Q8. You are given a binary array nums and an integer k.
A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0
in the subarray to 1, and every 1 in the subarray to 0.
Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not
possible, return -1.
Q9. An animal shelter holds only dogs and cats, and operates on a strictly "first in,first out"
basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter,
or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of
that type). They cannot select which specific animal they would like. Create the data structures to
maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog
anddequeueCat.You may use the built-in LinkedList data structure.
Q.10 You are given a histogram of n bars. Your task is to find the sum of the areas of the k
largest distinct rectangles that can be formed using contiguous bars.
Q11. You are initially given an empty histogram. You receive q queries, each query either:
Adds a bar with a given height at the end.
Asks for the maximum rectangle in the current histogram.
Design a solution that answers all queries efficiently.