Mid Term Coding Exam
Mid Term Coding Exam
Students are not allowed to use phones, web browsers, or social applications (such as Messenger, Zalo, etc.) for
any activities during the exam.
Mobile phones need to be put in your bag, not elsewhere.
Problem 1: Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N.
Find the missing element. (4 points)
class Solution:
def missingNumber(self,array,n):
# code here
Constraints:
1 ≤ N ≤ 10^6
1 ≤ A[i] ≤ 10^6
Problem 2: Design a queue that supports push and pop operations in the front, middle, and back (6
points).
Notice that when there are two middle position choices, the operation is performed on the frontmost
middle position choice. For example:
Example 1:
Input:
["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront",
"popMiddle", "popMiddle", "popBack", "popFront"]
[[], [1], [2], [3], [4], [], [], [], [], []]
Output:
[null, null, null, null, null, 1, 3, 4, 2, -1]
Explanation:
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
q.pushFront(1); // [1]
q.pushBack(2); // [1, 2]
q.pushMiddle(3); // [1, 3, 2]
q.pushMiddle(4); // [1, 4, 3, 2]
q.popFront(); // return 1 -> [4, 3, 2]
q.popMiddle(); // return 3 -> [4, 2]
q.popMiddle(); // return 4 -> [2]
q.popBack(); // return 2 -> []
q.popFront(); // return -1 -> [] (The queue is empty)
Constraints:
1 <= val <= 10^9
def __init__(self):
Marking Scheme: