Professional Documents
Culture Documents
Extra Exercises
Extra Exercises
1. Use a stack or a queue (or both!) to determine if a given input is a palindrome or not.
2. Evaluate an infix expression using a stack or a queue (or both!)
Queues
You own a car wash and would like to implement a program to keep up with the queue of cars waiting.
When you open your car wash (at the start of the program), your queue is empty.
Once the cars start coming, you add them to your queue. Every car has a make (string), a color (string)
and a plate number (int).
Once you finish washing a car you remove it from the queue and print all the information about the car.
If no more cars are waiting (the queue is empty), you close your program.
When you want to add a car to the queue, you should also add the in
Stack
You work for the MIB (men in black) and would like to decode the messages that they send you.
You start looping through the message pushing each alphabetical character and white space to your
stack. Once you reach an Asterix, you pop one character out of your stack.
Sometimes, you receive an incomplete message. Therefore, if you reach the end of the string and you
still have characters in your stack, pop all of them out.
An example of such decoding is:
Priority Queues
You work at the HR office of SE Factory and you would like to schedule the FSW interviews for the
students.
Each student has a name, a midterm grade (int value between 0 and 100), a final grade (int value
between 0 and 100), and a Boolean value that indicates if they have a good character or not.
-first, you interview the students with good attitude. Among these students, you start with the one with
the highest grade on the final. If two students have the same grade on the final, you give priority to the
student with the highest midterm grade. If these students have the same midterm grade, you can
choose any one of them.
You would interview them in the following order: Faisal, Mazen, Batoul, Ali.
1. Add a student
2. Interview a student
In option 1, the user adds a student to the priority queue. You ask the user for the attributes of the
student and add them to the priority queue
In option 2, you print the name of the student the user should interview next.
Graphs
1. Given a graph where the nodes are cities and the edges are the routes connecting them. Write a
function that takes as input two cities and returns true if there is a route connecting them, false
otherwise.
Write another function that takes as input one city and prints all the other cities reachable from
it.
2. Given a graph, write a function that checks if the graph contains a cycle.
3. Given a graph that represents the Instagram social media app. The nodes there represent users,
and we connect user 1 to user 2 if user 1 follows user 2 (the graph is undirected).
Write a function that takes as input a two users and prints the list of users they both follow.
Write another function that prints the list of users none of them follow
Trees
1. Given a list of numbers, use a binary search tree to print these numbers in ascending order
2. A full binary tree is a tree where every node either contains two children, or is a leaf (so the
node either has exactly 2 children or 0). Write a function to check if a binary tree is full.
3. Given a binary search tree of integers, and an input integer i, return true if i is repeated multiple
times in the tree