Queue - LAB2
Queue - LAB2
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 1/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 1
Đúng
Implement all methods in class Queue with template type T. The description of each method is written as comment in
frame code.
#ifndef QUEUE_H
#define QUEUE_H
#include "DLinkedList.h"
template<class T>
class Queue {
protected:
DLinkedList<T> list;
public:
Queue() {}
void push(T item) ;
T pop() ;
T top() ;
bool empty() ;
int size() ;
void clear() ;
};
#endif /* QUEUE_H */
You can use all methods in class DLinkedList without implementing them again. The description of class DLinkedList is written as
comment in frame code.
For example:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 2/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Test Result
Queue<int> queue;
assert(queue.empty());
assert(queue.size() == 0);
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 3/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 4/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 2
Đúng
The function returns the sum of the maximum value of every consecutive subarray of nums with fixed length k.
Note:
- The iostream, vector, queue and deque libraries have been included and namespace std is being used. No other libraries are
allowed.
- You can write helper functions and classes.
For example:
Test Result
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 5/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Passed all tests!
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 6/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 3
Đúng
Research queue which is implemented in C library at: http://www.cplusplus.com/reference/queue/queue/. You can use library
queue in c++ for this question.
Using queue, complete function void bfs(vector<vector<int>> graph, int start) to traverse all the nodes of the graph from
given start node using Breadth First Search algorithm and data structure queue, and print the order of visited nodes.
You can use below liberaries in this question.
#include <iostream>
#include <vector>
#include <queue>
For example:
Test Result
bfs(graph, 0);
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 7/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 8/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 4
Đúng
Using queue, complete function bool isBipartite(vector<vector<int>> graph) to determine if a graph is bipartite or not (the
graph can be disconnected). In caat https://en.wikipedia.org/wiki/Bipartite_graph.
#include <iostream>
#include <vector>
#include <queue>
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 10/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 5
Đúng
Given a n*m grid where each cell in the grid can have a value of 0, 1 or 2, which has the following meaning:
1. Empty cell
2. This cell contains a fresh apple
3. This cell contains a rotten apple
After 1 second, the cell with rotten apple will rot all fresh apples in all the cells adjacent to it (i.e the cells (x+1, y), (x-1, y), (x,
y+1), (x, y-1))
Determine the minimum time (in seconds) required to rot all apples. If this cannot be done, return -1.
Note: iostream, vector, and queue are already included.
Constraint:
1 <= n, m <= 500
Example 1:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation:
The grid is
2201
The apple at (0, 3) cannot be rotten
Example 2:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation:
The grid is
012
012
211
Apples at positions (0,2), (1,2), (2,0)
will rot apples at (0,1), (1,1), (2,2) and (2,1) after 1 second.
For example:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 11/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Answer: (penalty regime: 0 %)
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 12/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 13/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 6
Đúng
[Eng] Given a queue of integers of even length, rearrange the elements by interleaving the first half of the queue with the
second half of the queue.
[Vie] Cho 1 hàng đợi có số lượng phần tử là số chẵn, sắp xếp lại các phần tử theo quy tắc xen kẽ phần tử ở nửa đầu và
nửa sau của hàng đợi.
For example:
queue<int> q; 4 1 3 2 4
int n; cin >> n; 1 2 3 4
for (int i = 0; i < n; i++){
int element; cin >> element;
q.push(element);
}
interleaveQueue(q);
while (!q.empty()){
cout << q.front() << ' ';
q.pop();
}
queue<int> q; 6 2 8 4 10 6 12
int n; cin >> n; 2 4 6 8 10 12
for (int i = 0; i < n; i++){
int element; cin >> element;
q.push(element);
}
interleaveQueue(q);
while (!q.empty()){
cout << q.front() << ' ';
q.pop();
}
Reset answer
queue<int> q; 4 1 3 2 4 1 3 2 4
int n; cin >> n; 1 2 3 4
for (int i = 0; i < n; i++){
int element; cin >> element;
q.push(element);
}
interleaveQueue(q);
while (!q.empty()){
cout << q.front() << ' ';
q.pop();
}
queue<int> q; 6 2 8 4 10 6 12 2 8 4 10 6 12
int n; cin >> n; 2 4 6 8 10 12
for (int i = 0; i < n; i++){
int element; cin >> element;
q.push(element);
}
interleaveQueue(q);
while (!q.empty()){
cout << q.front() << ' ';
q.pop();
}
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 15/17
10/22/24, 5:39 PM Queue: Xem lại lần làm thử | BK-LMS
Câu hỏi 7
Đúng
A nice number is a positive integer that contains only 2's and 5's.
Some nice numbers are: 2, 5, 22, 25, 52, 55, ...
Number 2 is the first nice number.
Given an integer N, return the Nth nice number.
Note: iostream, vector, queue are already included for you.
Constraint:
1 <= n <= 10^6
Example 1:
Input:
n=5
Output:
52
Explanation:
The sequence of nice numbers is 2, 5, 22, 25, 52, 55, ...
The 5th number in this sequence is 52
Example 2:
Input:
n = 10000
Output:
2255522252225
For example:
int n; 5 52
cin >> n;
cout << nthNiceNumber(n) << endl;
Reset answer
int n; 5 52 52
cin >> n;
cout << nthNiceNumber(n) << endl;
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4596338&cmid=445607 17/17