Challenge - 5 Problems
Next Greater Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Next Greater Element for a given array
What is the output of the following C++ code that finds the next greater element for each element in the array?
DSA C++
std::vector<int> nextGreaterElement(std::vector<int>& nums) { std::stack<int> s; std::vector<int> result(nums.size(), -1); for (int i = nums.size() - 1; i >= 0; --i) { while (!s.empty() && s.top() <= nums[i]) { s.pop(); } if (!s.empty()) { result[i] = s.top(); } s.push(nums[i]); } return result; } int main() { std::vector<int> nums = {4, 5, 2, 25}; std::vector<int> res = nextGreaterElement(nums); for (int x : res) { std::cout << x << " "; } return 0; }
Attempts:
2 left
💡 Hint
Think about how the stack helps find the next greater element by popping smaller or equal elements.
✗ Incorrect
The code iterates from right to left, using a stack to keep track of next greater elements. For 4, next greater is 5; for 5, next greater is 25; for 2, next greater is 25; for 25, no greater element exists, so -1.
❓ Predict Output
intermediateNext Greater Element output for circular array
What is the output of the following code that finds the next greater element in a circular array?
DSA C++
std::vector<int> nextGreaterCircular(std::vector<int>& nums) { int n = nums.size(); std::vector<int> result(n, -1); std::stack<int> s; for (int i = 2 * n - 1; i >= 0; --i) { while (!s.empty() && s.top() <= nums[i % n]) { s.pop(); } if (i < n && !s.empty()) { result[i] = s.top(); } s.push(nums[i % n]); } return result; } int main() { std::vector<int> nums = {1, 2, 1}; std::vector<int> res = nextGreaterCircular(nums); for (int x : res) { std::cout << x << " "; } return 0; }
Attempts:
2 left
💡 Hint
Remember the array is circular, so elements after the end wrap around to the start.
✗ Incorrect
For the circular array [1,2,1], the next greater element for 1 is 2, for 2 there is no greater element so -1, and for the last 1 the next greater is 2 again.
🔧 Debug
advancedIdentify the error in Next Greater Element implementation
What error will the following code produce when run?
DSA C++
std::vector<int> nextGreaterElement(std::vector<int>& nums) { std::stack<int> s; std::vector<int> result(nums.size(), -1); for (int i = 0; i < nums.size(); ++i) { while (!s.empty() && nums[s.top()] < nums[i]) { result[s.top()] = nums[i]; s.pop(); } s.push(i); } return result; } int main() { std::vector<int> nums = {2, 1, 2, 4, 3}; std::vector<int> res = nextGreaterElement(nums); for (int x : res) { std::cout << x << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check how the stack stores indices and how the result is assigned.
✗ Incorrect
The code correctly finds the next greater element for each index by storing indices in the stack. For input {2,1,2,4,3}, the output is 4 2 4 -1 -1.
🧠 Conceptual
advancedStack usage in Next Greater Element algorithm
Why does the Next Greater Element algorithm use a stack to solve the problem efficiently?
Attempts:
2 left
💡 Hint
Think about how the algorithm processes elements and remembers which ones need a next greater element.
✗ Incorrect
The stack stores elements (or their indices) waiting for their next greater element. When a greater element is found, it resolves all smaller elements on the stack. This avoids nested loops and achieves O(n) time.
❓ Predict Output
expertOutput of Next Greater Element with duplicate values and large input
What is the output of the following code for the input array with duplicates?
DSA C++
std::vector<int> nextGreaterElement(std::vector<int>& nums) { std::stack<int> s; std::vector<int> result(nums.size(), -1); for (int i = nums.size() - 1; i >= 0; --i) { while (!s.empty() && s.top() <= nums[i]) { s.pop(); } if (!s.empty()) { result[i] = s.top(); } s.push(nums[i]); } return result; } int main() { std::vector<int> nums = {2, 2, 1, 3, 2, 3, 1}; std::vector<int> res = nextGreaterElement(nums); for (int x : res) { std::cout << x << " "; } return 0; }
Attempts:
2 left
💡 Hint
Duplicates do not affect the logic; the stack pops elements less than or equal to current.
✗ Incorrect
For each element, the next greater element is the first element to the right that is strictly greater. For the input, the output is 3 3 3 -1 3 -1 -1.
