Queue - Attempt Review
Queue - Attempt Review
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 1/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 1
Chính xác
[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
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 2/16
22:29 22/10/2023 Queue: Attempt review
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();
}
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 3/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 2
Chính xác
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
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 5/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 3
Chính xác
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
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 7/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 4
Chính xác
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:
Test Result
Queue<int> queue;
assert(queue.empty());
assert(queue.size() == 0);
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 8/16
22:29 22/10/2023 Queue: Attempt review
Answer: (penalty regime: 0 %)
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 9/16
22:29 22/10/2023 Queue: Attempt review
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 10/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 5
Chính xác
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;
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 12/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 6
Chính xác
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://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 13/16
22:29 22/10/2023 Queue: Attempt review
Answer: (penalty regime: 0 %)
Reset answer
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 14/16
22:29 22/10/2023 Queue: Attempt review
Câu hỏi 7
Chính xác
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://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 15/16
22:29 22/10/2023 Queue: Attempt review
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
WEBSITE
HCMUT
MyBK
BKSI
LIÊN HỆ
268 Lý Thường Kiệt, P.14, Q.10, TP.HCM
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 16/16