You are on page 1of 3

Data Structures – Stack and Queue Implementation

Problem Statement 1:
Consider an input queue containing characters as elements and an input stack containing
integers (integers can be either 1 or 2 only).

Example for input queue (front  rear): A, B, C


Example for input stack (top  bottom): 2, 1

Write a function which takes input queue and input stack as input parameters and returns an
output queue. The output queue contains same elements as that of input queue but in an order
which is determined by the elements of input stack.

For every element of the input stack, input queue is reordered based on the below rule-
 If the element in the stack is 1, then the first element in the queue (element at front)
will become last element (element at rear).
 If the element in the stack is 2, then the last element (element at rear) in the queue will
become the first element (element in front).
 After reordering the input queue based on the elements in the stack, if the reordered
queue and input queue are same, then based on the top element of the input stack
reordering has to be done once again.

Example:
Input_queue (frontrear): A, B, C
Input_stack (topbottom): 2, 1
Output_queue(frontrear): C, A, B

Step 1: For the first element in the input stack that is 2, the last element in the input queue that
is ‘C’ is moved to the front, so the input queue is now reordered as (front->rear) C, A, B.

Step 2: For the second element in the input stack that is 1, the first element in the reordered
input queue as per step 1 that is ‘C’ is moved to the rear of the queue, the input queue is now
reordered as (frontrear) A, B, C

Step 3: Now the reordered queue and the input queue are same and the top of the input stack
is 2, the last element in the input queue that is “C” is moved to the front , so the input queue is
now reordered as (frontrear) C, A, B.

Step 4: Final elements in the output queue after step 3 are (frontrear) C, A, B

Note: For first element in input stack, original input queue is reordered first, then for each
element in input stack the previous reordered queue is referenced to reorder again
Assumption:

 Input queue and input stack will always contain at least one element each
 Input queue will always contain single character strings
 The elements in the input stack can be either 1 or 2
Note: No need to validate assumptions
Sample Input and Output:

Problem Statement 2:
Implement the class People as given in the class diagram.

People

- name
- gender
- age

__init__(name,age,gender)
+ get_name()
+ get_age()
+ get_gender() 
__str__()
+ check_gender(people_queue,gender) -> static

Here, people queue represents a queue of people. Each person is identified by their name, age
and gender. This method should create and return a new queue with the people belonging to
the specified gender.
Note:
The new queue should have the same size as that of the people queue.
Perform case-sensitive comparison wherever applicable.
Estimated time: 30 minutes

Problem Statement 3:
Given a queue of whole numbers. Write a python function to return a new queue which
contains the evenly divisible numbers.

Note: A number is said to be evenly divisible if it is divisible by all the numbers in the given
range without any remainder. Consider the range to be from 1 to 10 (both inclusive).
Assume that there will always be few elements in the input queue, which are evenly divisible by
the numbers in the mentioned range.

Example:
Input Queue: 13983, 10080, 7113, 2520, 2500 (front - rear)
Output Queue: 10080, 2520 (front-rear)

Estimated time: 30 minutes

You might also like