Skip to content

Implement Queue using 2 stacks - #2491

Open
PavanKaushikAduri wants to merge 2 commits into
super30admin:masterfrom
PavanKaushikAduri:master
Open

Implement Queue using 2 stacks#2491
PavanKaushikAduri wants to merge 2 commits into
super30admin:masterfrom
PavanKaushikAduri:master

Conversation

@PavanKaushikAduri

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Implement Queue using Stacks (queue_using_two_stacks.py)

Great work! Your solution correctly implements a queue using two stacks with the same time and space complexity as the reference solution. The logic is sound and the code is readable.

A few minor suggestions:

  1. Dead code: The return -1 at the end of peek() is unreachable since both if/else branches return values. You can remove it for cleaner code.
  2. Code style: Consider following the reference pattern where pop() calls peek() to ensure the output stack is populated. This reduces code duplication:
    def pop(self) -> int:
        self.peek()  # ensures stack2 is populated
        return self.stack2.pop()
  3. Comments: Your comments are good, but you could add a brief explanation of the two-stack approach (why it works) for future readers.

VERDICT: PASS


Design HashMap (design_hashmap.py)

Strengths:

  1. Correct implementation: Your solution correctly implements all required operations (put, get, remove) using separate chaining with linked lists.
  2. Clean code structure: The code is well-organized with a separate ListNode class and clear method definitions.
  3. Good use of dummy head: Using a dummy head node simplifies the linked list operations, similar to the reference solution.
  4. Proper comments: The header comments clearly state the time/space complexity and confirm successful execution.
  5. Readable variable names: Variables like keyHash, prevNode, and primaryarray are descriptive.

Areas for improvement:

  1. Bucket size: You chose 10000 buckets while the reference uses 1000. While both work, consider the trade-off between memory usage and collision probability. With 10000 buckets and at most 10^4 operations, you'll have very few collisions but use more memory.
  2. getPrevNode return value: The function returns None when the head matches the key, which works because of the dummy node pattern. However, this could be confusing. Consider adding a comment explaining this behavior.
  3. Edge case handling: Consider adding explicit handling for edge cases like empty storage in remove (you do check this, which is good).
  4. Type hints: The type hints are good, but consider adding return type hints for getPrevNode for clarity.
  5. Magic numbers: The bucket size (10000) is a magic number. Consider making it a class constant or explaining why this size was chosen.

Overall, this is a solid implementation that correctly solves the problem with good time and space complexity.

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants