MTH 4300 Final Exam Practice Questions
Exams in MTH 4300 revolve around 3 classes of questions:
- Tracing - evaluating the output of a code snippet
- Debugging - given a code snippet, diagnose and fix the errors
- Implementation - write code from scratch to solve a problem
Clarity is the only way you'll get points. If you don't even understand what it is you're trying to say, then how can I?
Tracing
-
Consider the following program. It builds a BST by inserting values in order, then runs an inorder traversal and two depth-finding queries.
First draw the BST that results from the insertions. Then write the exact output of the program. For each
findDepthcall, trace which nodes are visited. -
Consider the following program. It runs BFS twice on the same 5-node undirected graph from different starting nodes.
Write the exact output produced by this program. For each BFS call, trace the queue state after each dequeue operation.
Debugging
-
The following function is supposed to detect whether an undirected graph has a cycle using DFS. It contains 3 bugs. For each one:
- Identify the line or lines where the bug occurs.
- Explain in one or two sentences what goes wrong.
- Write the corrected line or lines of code.
-
The following function is supposed to compute the shortest distances from a source vertex using Dijkstra's algorithm. It contains 3 bugs. For each one:
- Name the line or lines where the bug occurs.
- Explain in one or two sentences what goes wrong.
- Write the corrected line or lines of code.
Implementation
-
Given the following node definition:
Implement the following function:
Returns the height of the binary tree — the number of nodes on the longest root-to-leaf path. An empty tree has height 0.
-
Given the following function signature:
Given a 2D grid of
'1'(land) and'0'(water), returns the number of islands. An island is a maximal group of'1'cells connected horizontally or vertically. You may modify the grid in-place to mark visited cells. -
Given the following function signature:
Returns
trueif the directed graph withnvertices has a cycle. Use Kahn's algorithm: compute in-degrees, process 0-in-degree vertices with a queue, and check whether the resulting topological order contains allnvertices.
Relevant LeetCode questions
For all these problems, give each one an honest 30-minute attempt. If it's not working out, then ask AI to walk you through the correct framework to solve the problem.
You can expect the problems on the final exam to test the patterns you've learned from solving the problems below.
Trees
- 94. Binary Tree Inorder Traversal
- 100. Same Tree
- 101. Symmetric Tree
- 104. Maximum Depth of Binary Tree
- 108. Convert Sorted Array to Binary Search Tree
- 144. Binary Tree Preorder Traversal
- 145. Binary Tree Postorder Traversal
- 226. Invert Binary Tree
- 572. Subtree of Another Tree
- 102. Binary Tree Level Order Traversal
- 230. Kth Smallest Element in a BST
- 107. Binary Tree Level Order Traversal II
- 429. N-ary Tree Level Order Traversal