Skip to content

Conversation

@BioHazard786
Copy link
Contributor

@BioHazard786 BioHazard786 commented Oct 14, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a stack utility that supports constant-time retrieval of the current minimum alongside standard push, pop, and top operations.
    • Handles empty-state scenarios with safe fallbacks for querying top and minimum values.
    • Ensures all operations run in O(1) time, improving performance for scenarios requiring frequent minimum checks without additional memory overhead beyond the stack itself.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 14, 2025

Walkthrough

Adds a new MinStack implementation in O) Stack/minStack.cpp with O(1) push, pop, top, and getMin using an encoding technique to track the current minimum. Handles empty-stack cases by returning sentinel INT_MIN for top/getMin and initializes currMin with LONG_MAX.

Changes

Cohort / File(s) Summary
MinStack feature
O) Stack/minStack.cpp
Introduces class MinStack with O(1) push, pop, top, and getMin, using a single stack<long> and encoded values to maintain current minimum. Includes empty checks returning INT_MIN.

Sequence Diagram(s)

sequenceDiagram autonumber actor Client participant MS as MinStack participant S as Internal stack<long> note over MS: State: currMin Client->>MS: push(val) alt stack empty MS->>S: push(val) MS->>MS: currMin = val else val <= currMin MS->>S: push(2*val - currMin) %% encoded MS->>MS: currMin = val else MS->>S: push(val) end Client->>MS: top() alt S.top() < currMin (encoded) MS-->>Client: currMin else MS-->>Client: S.top() end Client->>MS: getMin() MS-->>Client: currMin Client->>MS: pop() alt S.top() < currMin (encoded) MS->>MS: currMin = 2*currMin - S.top() MS->>S: pop() else MS->>S: pop() end note over MS,S: Empty checks return INT_MIN for top/getMin 
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I stack my snacks with nimble grin,
A carrot’s low, I track the min. 🥕
Push, pop, hop—so swift and spry,
The smallest treat won’t pass me by.
In one quick peek, I know who’s thin—
A rabbit’s stack: I find the min!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title concisely and accurately reflects the primary change by specifying the addition of a MinStack class along with its core operations push, pop, top, and getMin, matching the changeset’s focus on implementing constant-time min tracking in a stack.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
O) Stack/minStack.cpp (1)

29-35: Encoding triggers unnecessarily when val equals currMin.

When val == currMin, the code encodes the value even though the minimum doesn't change. While functionally correct, this adds overhead. Consider changing line 29 to if (val >= currMin) to avoid encoding when the value equals the current minimum.

Apply this diff to optimize:

- if (val > currMin) + if (val >= currMin) st.push(val); else

Note: This is a minor optimization and the current implementation is correct.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f1f3df and ee7444b.

📒 Files selected for processing (1)
  • O) Stack/minStack.cpp (1 hunks)
Comment on lines +9 to +10
stack<long> st;
long currMin;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Consider using fixed-width types for guaranteed overflow safety.

The stack stores long values to handle the encoding arithmetic 2*val - currMin, but long is only guaranteed to be at least 32 bits by the C++ standard. On platforms where long is 32 bits (e.g., Windows with MSVC), this encoding can overflow when val approaches INT_MIN or INT_MAX.

Apply this diff to use guaranteed 64-bit storage:

+#include <cstdint> + private: - stack<long> st; - long currMin; + stack<int64_t> st; + int64_t currMin;

Then update the casts and operations accordingly (e.g., line 33: 2 * (int64_t)val).

🤖 Prompt for AI Agents
In O) Stack/minStack.cpp around lines 9 to 10, the code uses `long` for the stack and `currMin`, which is not guaranteed 64-bit and can overflow during the `2*val - currMin` encoding; replace `long` with a fixed-width type such as `int64_t` for both the stack element type and `currMin`, add the necessary header (<cstdint> or <stdint.h>), and update all casts and arithmetic to use `int64_t` (e.g., change occurrences like `2 * (int64_t)val` and `2*val - currMin` to use int64_t operands) so encoding/decoding cannot overflow on 32-bit long platforms. 
Comment on lines +48 to +50
long top = st.top();
if (top < currMin)
currMin = 2 * currMin - top;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Risk of integer overflow in minimum restoration.

The expression 2 * currMin - top on line 50 can overflow if long is 32 bits and values are extreme. For example, if currMin is large positive and top is large negative (encoded), the computation can exceed the range of a 32-bit long.

This is mitigated by using int64_t as suggested in the earlier comment.

🤖 Prompt for AI Agents
In O) Stack/minStack.cpp around lines 48 to 50, the expression `2 * currMin - top` can overflow with 32-bit long; change the relevant variables and stack element type to 64-bit integers (e.g., int64_t) and perform the subtraction using int64_t temporaries/casts so the arithmetic is done in 64-bit; update currMin, top (and the stack template type) to int64_t and include <cstdint> if needed to ensure safe, overflow-free restoration of the previous minimum. 
Comment on lines +58 to +68
int top()
{
if (st.empty())
return INT_MIN;

long top = st.top();
if (top < currMin)
return currMin;
else
return top;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Sentinel value INT_MIN creates ambiguity.

Returning INT_MIN for an empty stack (line 61) is problematic because INT_MIN is a valid value that can be pushed onto the stack. A caller cannot distinguish between an empty stack and a stack whose top element is INT_MIN.

Consider these alternatives:

  1. Throw an exception (e.g., std::runtime_error) when the stack is empty
  2. Use std::optional<int> as the return type
  3. Require callers to check emptiness before calling top()

Example using std::optional:

#include <optional> std::optional<int> top() { if (st.empty()) return std::nullopt; long top = st.top(); if (top < currMin) return currMin; else return top; }
🤖 Prompt for AI Agents
O) Stack/minStack.cpp around lines 58-68: the top() function currently returns INT_MIN on empty which is ambiguous; change the signature to return std::optional<int>, add #include <optional>, return std::nullopt when st.empty(), and otherwise return the computed top value (convert/cast to int as needed); update any callers to handle std::optional (or document that callers must check has_value()) and adjust any forward declarations or interface files to the new return type. 
Comment on lines +74 to +79
int getMin()
{
if (st.empty())
return INT_MIN;
return currMin;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Sentinel value INT_MIN creates ambiguity.

Similar to the top() method, returning INT_MIN for an empty stack creates ambiguity. If the stack contains INT_MIN as its minimum, getMin() would return the same value as it would for an empty stack.

Apply the same solution as suggested for top() (exception, std::optional, or documented precondition).

🤖 Prompt for AI Agents
In O) Stack/minStack.cpp around lines 74 to 79, getMin() currently returns INT_MIN for an empty stack which is ambiguous; change the API to avoid sentinel values by returning std::optional<int> (include <optional>), update the signature to std::optional<int> getMin(), return std::nullopt when st.empty(), and return currMin otherwise; alternatively, if you prefer exceptions, change it to throw a std::runtime_error on empty and update callers accordingly—ensure all call sites are updated to handle the new return/exception behavior and add a brief comment documenting the precondition/behavior. 
@AnkitMajee AnkitMajee merged commit 2552555 into AnkitMajee:main Oct 14, 2025
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment