Stack - Attempt Review
Stack - Attempt Review
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 1/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 1
Chính xác
You are keeping score for a basketball game with some new rules. The game consists of several rounds, where the scores of past rounds may affect
future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the operation you must apply to the
record, with the following rules:
For example:
ops = "52CD+"
For example:
Test Result
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 2/22
22:31 22/10/2023 Stack: Attempt review
25 ▼ for (int score : record) {
26 sum += score;
27 }
28 return sum;
29 }
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=1436093&cmid=188424 3/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 2
Chính xác
Implement all methods in class Stack with template type T. The description of each method is written as comment in frame code.
#ifndef STACK_H
#define STACK_H
#include "DLinkedList.h"
template<class T>
class Stack {
protected:
DLinkedList<T> list;
public:
Stack() {}
void push(T item) ;
T pop() ;
T top() ;
bool empty() ;
int size() ;
void clear() ;
};
#endif
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://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 4/22
22:31 22/10/2023 Stack: Attempt review
Test Result
Stack<int> stack; 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 5/22
22:31 22/10/2023 Stack: Attempt review
Stack<int> stack; 1 0 1 0
cout << stack.empty() << " " << stack.size();
Stack<int> stack; 8 8
assert(stack.top() == 12);
stack.pop();
stack.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=1436093&cmid=188424 6/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 3
Chính xác
Given an array nums[] of size N having distinct elements, the task is to find the next greater element for each element of the array
Next greater element of an element in the array is the nearest element on the right which is greater than the current element.
If there does not exist a next greater of a element, the next greater element for it is -1
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9
Example 1:
Input:
nums = {15, 2, 4, 10}
Output:
{-1, 4, 10, -1}
Example 2:
Input:
nums = {1, 4, 6, 9, 6}
Output:
{4, 6, 9, -1, -1}
For example:
int N; 4 -1 4 10 -1
cin >> N; 15 2 4 10
vector<int> nums(N);
for(int i = 0; i < N; i++) cin >> nums[i];
vector<int> greaterNums = nextGreater(nums);
for(int i : greaterNums)
cout << i << ' ';
cout << '\n';
int N; 5 4 6 9 -1 -1
cin >> N; 1 4 6 9 6
vector<int> nums(N);
for(int i = 0; i < N; i++) cin >> nums[i];
vector<int> greaterNums = nextGreater(nums);
for(int i : greaterNums)
cout << i << ' ';
cout << '\n';
Reset answer
int N; 4 -1 4 10 -1 -1 4 10 -1
cin >> N; 15 2 4 10
vector<int> nums(N);
for(int i = 0; i < N; i++) cin >> nums[i];
vector<int> greaterNums = nextGreater(nums);
for(int i : greaterNums)
cout << i << ' ';
cout << '\n';
int N; 5 4 6 9 -1 -1 4 6 9 -1 -1
cin >> N; 1 4 6 9 6
vector<int> nums(N);
for(int i = 0; i < N; i++) cin >> nums[i];
vector<int> greaterNums = nextGreater(nums);
for(int i : greaterNums)
cout << i << ' ';
cout << '\n';
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=1436093&cmid=188424 8/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 4
Chính xác
Given string S representing a postfix expression, the task is to evaluate the expression and find the final value. Operators will only include the
basic arithmetic operators like *, /, + and -.
Postfix expression: The expression of the form “a b operator” (ab+) i.e., when a pair of operands is followed by an operator.
For example: Given string S is "2 3 1 * + 9 -". If the expression is converted into an infix expression, it will be 2 + (3 * 1) – 9 = 5 – 9 = -4.
For example:
Test Result
Reset answer
1 #include <sstream>
2 ▼ int evaluatePostfix(const string& expression) {
3 stack<int> s;
4 stringstream ss(expression);
5 string token;
6 ▼ while (getline(ss, token, ' ')) {
7 ▼ if (token == "+" || token == "-" || token == "*" || token == "/") {
8 int b = s.top();
9 s.pop();
10 int a = s.top();
11 s.pop();
12 ▼ if (token == "+") {
13 s.push(a + b);
14 ▼ } else if (token == "-") {
15 s.push(a - b);
16 ▼ } else if (token == "*") {
17 s.push(a * b);
18 ▼ } else if (token == "/") {
19 s.push(a / b);
20 }
21 ▼ } else {
22 s.push(stoi(token));
23 }
24 }
25 return s.top();
26 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 9/22
22:31 22/10/2023 Stack: 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=1436093&cmid=188424 10/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 5
Chính xác
A Maze is given as 5*5 binary matrix of blocks and there is a rat initially at the upper left most block i.e., maze[0][0] and the rat wants to eat
food which is present at some given block in the maze (fx, fy). In a maze matrix, 0 means that the block is a dead end and 1 means that the
block can be used in the path from source to destination. The rat can move in any direction (not diagonally) to any block provided the block is
not a dead end.
Your task is to implement a function with following prototype to check if there exists any path so that the rat can reach the food or not:
bool canEatFood(int maze[5][5], int fx, int fy);
Template:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
class node {
public:
int x, y;
int dir;
node(int i, int j)
{
x = i;
y = j;
// Initially direction
// set to 0
dir = 0;
}
};
Some suggestions:
- X : x coordinate of the node
- Y : y coordinate of the node
- dir : This variable will be used to tell which all directions we have tried and which to choose next. We will try all the
directions in anti-clockwise manner starting from up.
If dir=0 try up direction.
If dir=1 try left direction.
If dir=2 try down direction.
If dir=3 try right direction.
For example:
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 11/22
22:31 22/10/2023 Stack: Attempt review
Test Result
// Maze matrix 1
int maze[5][5] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 0 },
{ 1, 0, 0, 1, 0 }
};
// Food coordinates
int fx = 1, fy = 4;
// Maze matrix 1
int maze[5][5] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 1 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 0 }
};
// Food coordinates
int fx = 2, fy = 3;
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 12/22
22:31 22/10/2023 Stack: Attempt review
// Maze matrix 1 1
int maze[5][5] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 0 },
{ 1, 0, 0, 1, 0 }
};
// Food coordinates
int fx = 1, fy = 4;
// Maze matrix 1 1
int maze[5][5] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 1 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 0 }
};
// Food coordinates
int fx = 2, fy = 3;
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=1436093&cmid=188424 13/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 6
Chính xác
Given a string S of characters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
Return the final string after all such duplicate removals have been made.
For example:
Test Result
Reset answer
1 ▼ string removeDuplicates(string S) {
2 stack<char> st;
3 ▼ for (char c : S) {
4 ▼ if (!st.empty() && st.top() == c) {
5 st.pop();
6 ▼ } else {
7 st.push(c);
8 }
9 }
10 string result = "";
11 ▼ while (!st.empty()) {
12 result = st.top() + result;
13 st.pop();
14 }
15 return result;
16 }
17
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 14/22
22:31 22/10/2023 Stack: 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=1436093&cmid=188424 15/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 7
Chính xác
Vietnamese version:
Bài toán stock span là một bài toán về chủ đề kinh tế tài chính, trong đó ta có thông tin về giá của một cổ phiếu qua từng ngày. Mục tiêu của
bài toán là tính span của giá cổ phiếu ở từng ngày.
Span của giá cổ phiếu tại ngày thứ i (ký hiệu là Si) được định nghĩa là số ngày liên tục nhiều nhất liền trước ngày thứ i có giá cổ phiếu thấp hơn,
cộng cho 1 (cho chính nó).
Ví dụ, với chuỗi giá cổ phiếu là [100, 80, 60, 70, 60, 75, 85].
Yêu cầu. Viết chương trình tính toán chuỗi span từ chuỗi giá cổ phiếu từ đầu vào.
Input. Các giá trị giá cổ phiếu, cách nhau bởi các ký tự khoảng trắng, được đưa vào standard input.
Output. Các giá trị span, cách nhau bởi một khoảng cách, được xuất ra standard ouput.
=================================
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 16/22
22:31 22/10/2023 Stack: Attempt review
Requirement. Write a program to calculate the spans from the stock's prices.
Input Result
100 80 60 70 60 75 85 1 1 1 2 1 4 6
10 4 5 90 120 80 1 1 2 4 5 1
Reset answer
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 17/22
22:31 22/10/2023 Stack: Attempt review
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 18/22
22:31 22/10/2023 Stack: Attempt review
100 80 60 70 60 75 85 1 1 1 2 1 4 6 1 1 1 2 1 4 6
10 4 5 90 120 80 1 1 2 4 5 1 1 1 2 4 5 1
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=1436093&cmid=188424 19/22
22:31 22/10/2023 Stack: Attempt review
Câu hỏi 8
Chính xác
Given a string s containing just the characters '(', ')', '[', ']', '{', and '}'. Check if the input string is valid based on following rules:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
For example:
For example:
Test Result
Reset answer
1 ▼ bool isValidParentheses(string s) {
2 stack<char> stack;
3 ▼ for (char c : s) {
4 ▼ if (c == '(' || c == '[' || c == '{') {
5 stack.push(c);
6 ▼ } else {
7 ▼ if (stack.empty()) {
8 return false;
9 }
10 ▼ if ((c == ')' && stack.top() != '(') || (c == ']' && stack.top() !=
11 return false;
12 }
13 stack.pop();
14 }
15 }
16 return stack.empty();
17 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 20/22
22:31 22/10/2023 Stack: 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Ệ
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 21/22
22:31 22/10/2023 Stack: Attempt review
[email protected]
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1436093&cmid=188424 22/22