Practice Problems For Exam 3: We've Compiled Some Exercises Before The Third Exam
Practice Problems For Exam 3: We've Compiled Some Exercises Before The Third Exam
1. Given a nearly sorted vector of integers, would you prefer to use quicksort or insertion sort? Explain why.
2. In the worst-case, what is the time complexity of quicksort? Intuitively, can you explain how this happens?
3. What is a drawback, in terms of space efficiency, of merge sort? What advantage does merge sort have,
in terms of time efficiency, over quicksort?
4. What is the primary assumption a developer has to make before using the binary search algorithm?
5. Given the following piece of code, determine the output:
C++
class Base {
public:
Base() {}
int main() {
std::vector<Base> base_objs;
Base obj1 = Derived();
Derived obj2 = Derived();
Base obj3 = Base();
base_objs.push_back(obj1);
base_objs.push_back(obj2);
base_objs.push_back(obj3);
obj1.f();
obj2.f();
obj3.f();
return 0;
}
6. What makes a class abstract? Is it possible to create an instance of an abstract class? Implement an
abstract class Person with a pure virtual function move .
C++
class A {
private:
int x;
int y;
public:
A(): x(0), y(0) {
std::cout << "Default constructor" << std::endl;
}
int main() {
A a(1, 2);
A b = a;
return 0;
}
9. For this question, we'll do some OOP and inheritance. You are tasked with writing an abstract class
Sorting with the pure virtual functions sort(std::vector<int>) and
worst_case_runtime() . This class is then subclassed into three classes which you are responsible
for: InsertionSort , QuickSort , and MergeSort . Within these classes, you are to implement
a function sort which sorts a vector of integers according to the type of sorting algorithm you have.
(You are free to implement this however you wish.) worst_case_runtime() would then return a
std::string containing the worst case runtime of the algorithm.
11. (Hard) Big Integer Addition: Suppose that we have two text files, num1.txt and num2.txt . For
simplicity, suppose that each file contains n lines of integers of length m .
128336484738349374939483
128383749204958695847474
192384737493958495848473
192383748395849540234210
093847382634739203984298
This file would constitute one large number. The case will be similar for num2.txt .
Your task is to read the numbers into an appropriate data structure and allow for the addition of these two
big integers and output the result to a text file. (Do this problem if you have time.)
13. Implement a MinStack . Design a stack that supports push, pop, top, and retrieving the minimum
element in constant time.
C++
class MinStack {
private:
public:
/** initialize your data structure here. */
MinStack() {
void push(int x) {
// Check if the stack is empty or not
void pop() {
int top() {
int getMin() {
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
// Given two binary trees, write a function to check if they are the same or not.
// Two binary trees are considered the same if they are structurally identical
// and the nodes have the same value.
bool isSameTree(TreeNode* p, TreeNode* q) {
// TODO
}