Stack - Attempt Review - BK-LMS
Stack - Attempt Review - BK-LMS
Question 1
Implement all methods in class Stack with template type T. The description of each method is written as
Correct
comment in frame code.
Mark 1.00 out
of 1.00
#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:
Test Result
Stack<int> stack; 1 0
cout << stack.empty() << " " << stack.size();
Test Result
Stack<int> stack; 8
assert(stack.top() == 12);
stack.pop();
stack.pop();
Reset answer
Test Expected Got
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();
Correct
Marks for this submission: 1.00/1.00.
Question 2
You are keeping score for a basketball game with some new rules. The game consists of several rounds, where the scores of
Correct
past rounds may affect future rounds' scores.
Mark 1.00 out
of 1.00 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:
ops = "52CD+"
For example:
Test Result
Reset answer
Correct
Marks for this submission: 1.00/1.00.
Question 3
Given an array nums[] of size N having distinct elements, the task is to find the next greater element for
Correct
each element of the array
Mark 1.00 out
Next greater element of an element in the array is the nearest element on the right which is greater than
of 1.00
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';
Correct
Marks for this submission: 1.00/1.00.
Question 4
Given a string S of characters, a duplicate removal consists of choosing two adjacent and equal letters, and
Correct
removing them.
Mark 1.00 out
of 1.00 We repeatedly make duplicate removals on S until we no longer can.
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> s;
3
4 ▼ for (char c : S) {
5 ▼ if (!s.empty() && s.top() == c) {
6 s.pop();
7 ▼ } else {
8 s.push(c);
9 }
10 }
11
12 string result;
13 ▼ while (!s.empty()) {
14 result = s.top() + result;
15 s.pop();
16 }
17
18 return result;
19 }
Test Expected Got
Correct
Marks for this submission: 1.00/1.00.
Question 5 Given a string s containing just the characters '(', ')', '[', ']', '{', and '}'. Check if the input string is valid based
Correct on following rules:
Mark 1.00 out
1. Open brackets must be closed by the same type of brackets.
of 1.00
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> stk;
3
4 ▼ for (char c : s) {
5 ▼ if (c == '(' || c == '[' || c == '{') {
6 stk.push(c);
7 ▼ } else {
8 if (stk.empty()) return false;
9
10 char top = stk.top();
11 stk.pop();
12
13 if ((c == ')' && top != '(') ||
14 (c == ']' && top != '[') ||
15 ▼ (c == '}' && top != '{')) {
16 return false;
17 }
18 }
19 }
20
21 return stk.empty();
22 }
Test Expected Got
Correct
Marks for this submission: 1.00/1.00.
Question 6
Correct
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.,
Mark 1.00 out
maze[0][0] and the rat wants to eat food which is present at some given block in the maze (fx, fy). In a
of 1.00
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.
For example:
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;
cout << canEatFood(maze, fx, fy);
Test Expected Got
// 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;
Correct
Marks for this submission: 1.00/1.00.
Question 7 Vietnamese version:
Correct
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ổ
Mark 1.00 out
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.
of 1.00
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.
=================================
The stock span problem is a financial problem where we have a series of daily price quotes for a stock and
we need to calculate the span of the stock’s price for each day.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days
just before the given day, for which the price of the stock on the current day is less than its price on the
given day, plus 1 (for itself).
For example: take the stock's price sequence [100, 80, 60, 70, 60, 75, 85]. (See image above)
The given input span for 100 will be 1, 80 is smaller than 100 so the span is 1, 60 is smaller than 80 so the span
is 1, 70 is greater than 60 so the span is 2 and so on.
Requirement. Write a program to calculate the spans from the stock's prices.
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
Input Expected Got
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
Correct
Marks for this submission: 1.00/1.00.
Question 8 Given string S representing a postfix expression, the task is to evaluate the expression and find the final
Correct value. Operators will only include the basic arithmetic operators like *, /, + and -.
Mark 1.00 out
Postfix expression: The expression of the form “a b operator” (ab+) i.e., when a pair of operands is followed
of 1.00
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