DSA Questions
DSA Questions
Story: A librarian has arranged books by their ID numbers in ascending order. She wants to
quickly check if a book with a specific ID is present.
Problem: Given a sorted array and a target value, determine if the target exists using binary
search.
Output: true
Solution:
int l = 0, r = nums.size() - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
else r = mid - 1;
return false;
}
2. Balanced Parentheses
Story: You're a magician and your spells require perfectly matching symbols (like parentheses).
One missing symbol, and the spell fails!
Problem: Check if a given string of parentheses is balanced.Input contains ‘(‘ and ‘)’
parenteses.
Input: "(()())"
Output: true
Solution:
bool isValid(string s) {
stack<char> st;
for (char c : s) {
if (c == '(') st.push(c);
else if (c == ')') {
st.pop();
return st.empty();
"([)]"
3.Count Nodes in Linked List
Story: Think of a train with connected coaches. You’re asked to count how many coaches
(nodes) the train has.
Output: 4
Solution:
Story: Imagine you're at a store with a gift card worth a certain amount. You want to pick two
items whose total cost exactly matches the card value.
Problem: Given an array of integers and a target, return the indices of two numbers such that
they add up to the target.
Solution:
● Story: After finding the biggest marble, the child wants to find the second biggest marble
to keep as a backup in case he loses his favorite.
● Problem: Given an array of integers, find the second maximum distinct element.
● Input: [3, 5, 1, 8, 2]
● Output: 5
● Explanation: 8 is the maximum, 5 is the second highest.
Solution:
Story: A group of dancers stand in a circle. After each song, they all move k steps to the
right. What’s the new order after the move?
Output: [5, 6, 7, 1, 2, 3, 4]
Explanation: After 3 right shifts, the last 3 elements come to the front.
Solution:
Story: A treasure map is split and rotated accidentally. You need to find a specific
number in the rotated map.
Output: 4
Solution:
Story: Each warrior wants to know who the next stronger warrior is to their right. Help
each warrior find their successor.
Problem: For each element, find the next greater element to its right.
Solution:
Story: You’re organizing a line of people for a performance but want to reverse the
order for a surprise ending.
Input: 1 → 2 → 3 → 4
Output: 4 → 3 → 2 → 1
Solution:
while (head) {
ListNode* next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
5.Daily Temperatures (Stack)
Story: A weather station records daily temperatures. For each day, you want to
know how many days you have to wait until a warmer temperature comes.
Problem: Given a list of daily temperatures, return a list such that for each day,
you tell in how many days a warmer temperature will occur. If not, return 0.
Input: temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
Explanation: For each day, we find the next day with a higher temperature.
Solution:
int n = temperatures.size();
vector<int> res(n, 0);
stack<int> st;
return res;
}
HARD LEVEL QUESTIONS
1. Word Break
Story: You have a dictionary and a sentence without spaces. Can you break it into
valid words?
Problem: Given a string and a word dictionary, return true if it can be segmented
into a space-separated sequence of one or more dictionary words.
Output: true
Story: Imagine each bar in a bar chart represents the height of a building. Find the
largest area rectangle you can make using these bars.
Problem: Given an array of bar heights, find the largest rectangular area in the
histogram.
Input: [2, 1, 5, 6, 2, 3]
Output: 10
Solution:
stack<int> st;
heights.push_back(0);
int maxArea = 0;
st.push(i);
return maxArea;
}
3.Trapping Rain Water
Story: After a rainstorm, valleys between buildings fill with water. Given the heights of
the buildings, calculate how much water is trapped.
Problem: Given n non-negative integers representing the elevation map where the width
of each bar is 1, compute how much water it can trap after raining.
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Solution:
}
return water;
}
4. Detect and Remove Loop in Linked List
Story: A group of people are passing a secret note in a circle. Someone notices that the
note never stops circulating—can you identify where the cycle starts and remove it to
stop the endless loop?
Problem: Given a linked list, detect if a loop exists, and if it does, remove the loop.
Solution:
void removeLoop(ListNode* head) {
ListNode *slow = head, *fast = head;
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
fast->next = nullptr;
}
5.N-Queens Problem
Story: You’re organizing a royal chess tournament. Each queen must be placed
such that no two threaten each other. Find all possible arrangements on an N×N
board.
Problem: Place N queens on an N×N chessboard such that no two queens attack
each other. Return all distinct solutions.
Input: n = 4
[".Q..",
"...Q",
"Q...",
"..Q."],
["..Q.",
"Q...",
"...Q",
".Q.."]
]
Solution:
return true;
if (row == n) {
result.push_back(board);
return;
board[row][col] = 'Q';
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
return result;