0% found this document useful (0 votes)
214 views16 pages

Queue - Attempt Review

Uploaded by

Jo Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views16 pages

Queue - Attempt Review

Uploaded by

Jo Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

22:29 22/10/2023 Queue: Attempt review

Đã bắt đầu vào Thứ bảy, 21 Tháng mười 2023, 12:55 PM


lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Chủ nhật, 22 Tháng mười 2023, 10:28 PM
lúc
Thời gian thực 1 ngày 9 giờ
hiện
Điểm 7,00/7,00
Điểm 10,00 của 10,00 (100%)

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

Điểm 1,00 của 1,00

[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.

Your task is to implement interleaveQueue function.


stack and queue are included.

[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.

Sinh viên cần hiện thực hàm interleaveQueue.

Thư viện stack và queue đã được thêm vào.

For example:

Test Input Result

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();
}

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ void interleaveQueue(queue<int>& q){


2 queue<int> tempQueue;
3 int n = q.size();
4
5 ▼ for (int i = 0; i < n / 2; i++) {
6 tempQueue.push(q.front());
7 q.pop();
8 }
9
10 ▼ for (int i = 0; i < n / 2; i++) {
11 q.push(tempQueue.front());
12 tempQueue.pop();
13 q.push(q.front());
14 q.pop();
15 }
16 }

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 2/16
22:29 22/10/2023 Queue: Attempt review

Test Input Expected Got

 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();
}

Passed all tests! 

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

Điểm 1,00 của 1,00

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 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.

You can use below liberaries in this question.

#include <iostream>
#include <vector>
#include <queue>

For example:

Test Result

int G[6][6] = { {0, 1, 0, 0, 0, 1}, Yes


{1, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 0},
{0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0} };
int n = 6;

vector<vector<int>> graph(n, vector<int>());


for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (G[i][j]) graph[i].push_back(j);
}
}

isBipartite(graph) ? cout << "Yes" : cout << "No";

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ bool isBipartite(vector<vector<int>>& graph) {


2 int n = graph.size();
3 vector<int> colors(n, 0); // 0: not colored, 1: color 1, -1: color -1
4 queue<int> q;
5
6▼ for (int i = 0; i < n; i++) {
7 if (colors[i] != 0) continue;
8 q.push(i);
9 colors[i] = 1;
10
11 ▼ while (!q.empty()) {
12 int node = q.front();
13 q.pop();
14
15 ▼ for (int neighbor : graph[node]) {
16 if (colors[neighbor] == colors[node]) return false;
17 ▼ if (colors[neighbor] == 0) {
18 colors[neighbor] = -colors[node];
19 q push(neighbor);
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 4/16
22:29 22/10/2023 Queue: Attempt review
19 q.push(neighbor);
20 }
21 }
22 }
23 }
24 return true;
25 }
26

Passed all tests! 

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

Điểm 1,00 của 1,00

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

int init_graph[10][10] = { {0, 1, 1, 0, 1, 0, 1, 0, 1, 0}, 0 1 2 4 6 8 3 7 5 9


{0, 0, 1, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 1, 0, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 1, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 1, 0} };
int n = 10;
vector<vector<int>> graph(n, vector<int>());
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (init_graph[i][j]) graph[i].push_back(j);
}
}

bfs(graph, 0);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ void bfs(vector<vector<int>> graph, int start) {


2 int n = graph.size();
3 vector<bool> visited(n, false);
4 queue<int> q;
5
6 q.push(start);
7 visited[start] = true;
8
9▼ while (!q.empty()) {
10 int current = q.front();
11 q.pop();
12 cout << current << " ";
13
14 ▼ for (int neighbor : graph[current]) {
15 ▼ if (!visited[neighbor]) {
16 q.push(neighbor);
17 visited[neighbor] = true;
18 }
19 }
20 }
21 }
22
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 6/16
22:29 22/10/2023 Queue: Attempt review
22

Passed all tests! 

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

Điểm 1,00 của 1,00

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.

template <class T>


class DLinkedList
{
public:
class Node; //forward declaration
protected:
Node* head;
Node* tail;
int count;
public:
DLinkedList() ;
~DLinkedList();
void add(const T& e);
void add(int index, const T& e);
T removeAt(int index);
bool removeItem(const T& removeItem);
bool empty();
int size();
void clear();
T get(int index);
void set(int index, const T& e);
int indexOf(const T& item);
bool contains(const T& item);
};

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

1 ▼ void push(T item) {


2 list.add(item);
3 }
4
5 ▼ T pop() {
6 ▼ if (list.empty()) {
7 throw "Queue is empty";
8 }
9 T item = list.get(0);
10 list.removeAt(0);
11 return item;
12 }
13
14 ▼ T top() {
15 ▼ if (list.empty()) {
16 throw "Queue is empty";
17 }
18 return list.get(0);
19 }
20
21 ▼ bool empty() {
22 return list.empty();
23 }
24
25 ▼ int size() {
26 return list.size();
27 }
28
29 ▼ void clear() {
30 list.clear();
31 }

Passed all tests! 

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

Điểm 1,00 của 1,00

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:

Test Input Result

int n; 5 52
cin >> n;
cout << nthNiceNumber(n) << endl;

int n; 10000 2255522252225


cin >> n;
cout << nthNiceNumber(n) << endl;

Answer: (penalty regime: 0, 0, 0, 5, 10, ... %)

Reset answer

1 // iostream, vector and queue are included


2 // You can write helper methods
3
4 ▼ long long nthNiceNumber(int n) {
5 queue<long long> q;
6 q.push(2);
7 q.push(5);
8
9 long long result = 0;
10 ▼ for (int i = 0; i < n; i++) {
11 result = q.front();
12 q.pop();
13 q.push(result * 10 + 2);
14 q.push(result * 10 + 5);
15 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 11/16
22:29 22/10/2023 Queue: Attempt review
15 }
16 return result;
17 }

Test Input Expected Got

 int n; 5 52 52 
cin >> n;
cout << nthNiceNumber(n) << endl;

 int n; 10000 2255522252225 2255522252225 


cin >> n;
cout << nthNiceNumber(n) << endl;

Passed all tests! 

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

Điểm 1,00 của 1,00

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

Hint: Have you ever heard about breadth-first-search?

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:

Test Input Result

int rows, cols; 1 4 -1


cin >> rows >> cols; 2 2 0 1
vector<vector<int>> grid(rows, vector<int>(cols));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) cin >> grid[i][j];
}
cout << secondsToBeRotten(grid);

int rows, cols; 3 3 1


cin >> rows >> cols; 0 1 2
vector<vector<int>> grid(rows, vector<int>(cols)); 0 1 2
for(int i = 0; i < rows; i++) { 2 1 1
for(int j = 0; j < cols; j++) cin >> grid[i][j];
}
cout << secondsToBeRotten(grid);

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

1 // iostream, vector and queue are included


2 // Hint: use breadth-first-search
3
4 ▼ int secondsToBeRotten(vector<vector<int>>& grid) {
5 int n = grid.size();
6 int m = grid[0].size();
7 int fresh = 0;
8 queue<pair<int, int>> q;
9
10 // Find rotten apples and count fresh apples
11 ▼ for (int i = 0; i < n; i++) {
12 ▼ for (int j = 0; j < m; j++) {
13 ▼ if (grid[i][j] == 2) {
14 q.push({i, j});
15 ▼ } else if (grid[i][j] == 1) {
16 fresh++;
17 }
18 }
19 }
20
21 int time = 0;
22 vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
23
24 ▼ while (!q.empty() && fresh > 0) {
25 int size = q.size();
26 ▼ for (int k = 0; k < size; k++) {
27 pair<int, int> cell = q.front();
28 q.pop();
29
30 ▼ for (auto dir : directions) {
31 int x = cell.first + dir[0];
32 int y = cell.second + dir[1];
33
34 ▼ if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == 1) {
35 grid[x][y] = 2;
36 fresh--;
37 q.push({x, y});
38 }
39 }
40 }

Test Input Expected Got

 int rows, cols; 1 4 -1 -1 


cin >> rows >> cols; 2 2 0 1
vector<vector<int>> grid(rows, vector<int>(cols));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) cin >> grid[i][j];
}
cout << secondsToBeRotten(grid);

 int rows, cols; 3 3 1 1 


cin >> rows >> cols; 0 1 2
vector<vector<int>> grid(rows, vector<int>(cols)); 0 1 2
for(int i = 0; i < rows; i++) { 2 1 1
for(int j = 0; j < cols; j++) cin >> grid[i][j];
}
cout << secondsToBeRotten(grid);

Passed all tests! 

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

Điểm 1,00 của 1,00

Given an array of integers.


Your task is to implement a function with following prototype:
int sumOfMaxSubarray(vector<int>& nums, int k);

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

vector<int> nums {1, 2, 4, 3, 6}; 14


int k = 3;
cout << sumOfMaxSubarray(nums, k);

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ int sumOfMaxSubarray(vector<int>& nums, int k) {


2 int n = nums.size();
3 ▼ if (k > n) {
4 return 0;
5 }
6
7 int sum = 0;
8 deque<int> dq;
9
10 ▼ for (int i = 0; i < n; i++) {
11 // Remove elements outside the current window of size k
12 ▼ if (!dq.empty() && dq.front() == i - k) {
13 dq.pop_front();
14 }
15
16 // Remove smaller elements as they are no longer needed
17 ▼ while (!dq.empty() && nums[dq.back()] < nums[i]) {
18 dq.pop_back();
19 }
20
21 dq.push_back(i);
22

Test Expected Got

 vector<int> nums {1, 2, 4, 3, 6}; 14 14 


int k = 3;
cout << sumOfMaxSubarray(nums, k);

 vector<int> nums {8016}; 8016 8016 


int k = 1;
cout << sumOfMaxSubarray(nums, k);

Passed all tests! 

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.

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI

LIÊN HỆ
 268 Lý Thường Kiệt, P.14, Q.10, TP.HCM

 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

[email protected]

Copyright 2007-2022 BKEL - Phát triển dựa trên Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1500151&cmid=188426 16/16

You might also like