0% found this document useful (0 votes)
26 views

Questions on Stack and Queue

The document outlines various programming challenges related to data structures such as stacks and queues. It includes problems like checking for balanced parentheses, finding stock spans, and identifying the first non-repeating character in a stream. Each problem is accompanied by example inputs and expected outputs.

Uploaded by

Code Geeks
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)
26 views

Questions on Stack and Queue

The document outlines various programming challenges related to data structures such as stacks and queues. It includes problems like checking for balanced parentheses, finding stock spans, and identifying the first non-repeating character in a stream. Each problem is accompanied by example inputs and expected outputs.

Uploaded by

Code Geeks
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/ 13

Stack

Queue
Parenthesis Checker
Given an expression string exp, write a program to test whether the
pairs and the order of “{”, “}”, “(”, “)”, “[”, “]” are correct in the given
expression.

Input: Output:
{([ ])} true
Redundant Parenthesis
Given a string of balanced expression, nd if it contains a redundant
parenthesis or not. A set of parenthesis are redundant if same sub-
expression is surrounded by unnecessary or multiple brackets. Print
‘yes’ if redundant else ‘no’.

Input: Output:
((a+b)) Yes

fi
Stock Span
Given an array of integers of length N that represents prices of stock on
N consecutive days. Find the span of stock’s price for all N days. The
span Si of the stock's price on a given day i is de ned as the maximum
number of consecutive days just before the given day, for which the
price of the stock on the current day is less than its price on the given
day

Input: Output:
10 1 2 1 2 5 1 1 1 4 10
13 15 12 14 16 8 6 4 10 30

fi
Previous Greater Element
Given an array of N distinct integers, nd the closest (position-wise)
greater on left of every element. If there is no greater element on left
then print -1.

Input: Output:
8 -1 15 -1 18 12 12 6 12
15 10 18 12 4 6 2 8
fi
Next Greater Element
Given an array of N distinct integers, nd the closest (position-wise)
greater on right of every element. If there is no greater element on right
then print -1.

Input: Output:
8 15 18 12 12 12 18 18 -1
5 15 10 8 6 12 9 18
fi
Largest Rectangular Area in a Histogram
Find the largest rectangular area possible in a given histogram where
the largest rectangle can be made of a number of contiguous bars
whose heights are given in an array. For simplicity, assume that all bars
have the same width and the width is 1 unit.

Input: Output:
7 100
60 20 50 40 10 50 60
Queue using two Stacks
Given a stack data structure with push and pop operations, the task is
to implement a queue using instances of stack data structure and
operations on them.
First non-repeating character in a stream
Given an input stream of characters consisting only of lowercase
alphabets. Find the rst non-repeating character in the input string
each time a new character is inserted into the stream. If there is no
non-repeating character, then append '-1' to the answer.

Input: Output:
aabcbc a -1 b b c -1
fi
Generate numbers with given digits
Given a number N, print rst N numbers (in increasing order) such that
all these numbers have digits in a set { 5, 6 }.

Input: Output:
10 5 6 55 56 65 66 555 556 565 566
fi
Reverse First K Elements of a Queue
Given an integer K and a queue of integers, we need to reverse the
order of the rst K elements of the queue, leaving the other elements in
the same order.
Only following standard operations are allowed on queue.

• enqueue(x): Add an item x to rear of queue


• dequeue(): Remove an item from front of queue
• size(): Returns number of elements in queue
• front(): Finds front element.
Input: Output:
53 32145
12345
fi
Max Subarray
Given an array of length N and an integer K, nd the maximum element
for each and every Contiguous subarray of size K.

Input:
9 Output:
123145236 3345556
3

fi
Gas Station | Circular Tour
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

Input:
5 Output:
12345 3
34512

You might also like