DSA Questions
Time : 20 Minutes
Note : - To solve these questions please create stack and qeue class for both string and integer type.
Question 1: Bracket Matching Using Stack and Queue
You are given:
1. A Stack (openBrackets) containing opening brackets: ['(', '[', '{'] (Top to
Bottom: ( -> {).
2. A Queue (closeBrackets) containing closing brackets: [')', ']', '}'] (Front to
Rear: ) -> }).
Task:
1. Match brackets from openBrackets and closeBrackets in pairs (e.g., '(' matches ')').
2. Push the matched pair as a string (e.g., "()") into an output stack (opStack).
3. If no matching pair exists, discard the unmatched bracket.
Example Output:
At the end, opStack might look like:
["{}", "[]", "()"]
Time : 20 Minutes
Question 2: Special Character Processing and String
Manipulation
You are given:
1. A Queue (ipQueue) containing strings formatted as ["Ca:t", "Do:g", "Fi:sh",
"Parr:ot", "Bi:rd"] (Front to Rear: Ca:t -> Bi:rd).
Task:
1. For each string in ipQueue:
1. Split the string into two parts using the special character (:).
2. Compare the lengths of the two parts:
1. If the lengths are equal, push the original string (with the special character)
into an output stack (opStack).
2. If the lengths are not equal, remove the special character, reverse the
entire string, and push the reversed string into opStack.
Example:
For "Fi:sh", the lengths are equal (2 == 2), so "Fi:sh" is pushed to opStack.
For "Ca:t", the lengths are not equal (2 != 1), so reverse the string without : and push
"taC" to opStack.
Output Example:
At the end, the opStack (from Top to Bottom) might look like:
[“Bi:rd” , “torraP” , “Fi:sh” , “goD” , “taC”]
Time : 20 Minutes
Question 3: Matching Conditions Between Stack and
Queue
You are given:
1. A Stack (numStack) containing integers: [3, 3, 4, 2, 1] (Top to Bottom: 5 -> 1).
2. A Queue (wordQueue) containing strings: ["Orange", "Eagle", "Dog",
"Banana", "Owl"] (Front to Rear: Orange -> Ice).
Task:
For each element in wordQueue:
1. Calculate the count of vowels (A, E, I, O, U) in the string.
2. Compare the vowel count to the top value of numStack.
1. If the vowel count matches the top value of numStack:
1. Push the string concatenated with the numeric value (string + num)
into an output stack (opStack).
2. Remove the top value from numStack.
2. Otherwise:
1. Push the string into a temporary stack (tempStack).
After processing all elements in wordQueue:
1. Push all elements from tempStack into opStack in the same order they were in
tempStack.
Expected Output:
At the end, opStack should have its final state. Example output:
[”Banana” , "Dog" , "Owl1" , "Eagle3", "Orange3"]
Time : 20 Minutes
Question 4: Reversing and Combining Data
You are given:
1. A Queue (inputQueue) containing integers: [12, 34, 56, 78, 90] (Front to Rear:
12 -> 90).
2. A Stack (inputStack) containing integers: [5, 4, 3, 2, 1] (Top to Bottom: 5 -> 1).
Task:
1. Pop elements from inputStack and dequeue elements from inputQueue.
2. Combine each pair as a string in the format "QueueElement:StackElement".
3. Push the combined string into an output stack (opStack).
Example Output:
If all elements are processed, opStack might look like:
["90:1", "78:2", "56:3", "34:4", "12:5"]
Time : 20 Minutes
Question 5: Alternating Stack and Queue Merging
You are given:
1. A Stack (numStack) containing integers: [10, 20, 30, 40] (Top to Bottom: 10 -> 40).
2. A Queue (numQueue) containing integers: [5, 15, 25, 35] (Front to Rear: 5 -> 35).
Task:
1. Create a new stack (resultStack) by alternating elements from numStack and
numQueue:
1. Push the top element of numStack into resultStack.
2. Then enqueue the front element of numQueue into resultStack.
Example Output:
At the end, resultStack might look like:
[35, 40, 25, 30, 15, 20, 5, 10]
Time : 20 Minutes
Question 6: Priority Sorting Using Stacks
You are given:
1. A Stack (priorityStack) containing integers representing priorities: [3, 1, 4, 2]
(Top to Bottom: 3 -> 2).
2. A Queue (taskQueue) containing strings representing tasks: ["TaskA", "TaskB",
"TaskC", "TaskD"] (Front to Rear: TaskA -> TaskD).
Task:
1. Pair each task in taskQueue with the priority from priorityStack.
2. Sort the tasks by priority in descending order and push them into an output stack
(opStack).
Example Output:
At the end, opStack might look like:
["TaskC4", "TaskA3", "TaskD2", "TaskB1"]
Time : 20 Minutes
Question 7: Special Character Replacement
You are given:
1. A Queue (ipQueue) containing strings formatted with special characters, e.g.,
["He@llo", "Wor#ld", "Pyt%hon", "Stac&k", "Que*ue"].
Task:
1. For each string in ipQueue:
1. Replace all special characters with '-'.
2. If the modified string contains an even number of vowels (A, E, I, O, U), push
the string to an output stack (opStack).
3. Otherwise, enqueue the modified string back to ipQueue.
Example Output:
At the end, opStack might look like: ["Stac-k", "He-llo"], and ipQueue might
have: ["Wor-ld", "Pyt-hon", "Que-ue"].
Time : 20 Minutes
Question 8: Alternating Case Transformation
You are given:
1. A Queue (ipQueue) containing strings, e.g., ["hello", "world", "stack",
"queue", "python"].
Task:
1. For each string in ipQueue:
1. Transform it into alternating case (e.g., "hello" becomes "HeLlO").
2. If the length of the transformed string is even, push it into opStack.
3. If the length is odd, enqueue it back into ipQueue.
Example Output:
At the end, opStack might contain: ["StAcK", "QuEuE"], and ipQueue might have:
["HeLlO", "WoRlD", "PyThOn"].