Skip to content

Design 2 - #2489

Open
Yashzaparto wants to merge 1 commit into
super30admin:masterfrom
Yashzaparto:master
Open

Design 2#2489
Yashzaparto wants to merge 1 commit into
super30admin:masterfrom
Yashzaparto:master

Conversation

@Yashzaparto

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Implement Queue using Stacks (queue_using_stacks.py)

Great job! Your solution correctly implements a queue using two stacks with the optimal amortized O(1) approach. The logic is clean and the code is readable. A few minor suggestions:

  1. Pythonic style: Instead of while len(self.in_stack) != 0, you can use while self.in_stack which is more Pythonic and slightly more efficient.

  2. DRY principle: The transfer logic is duplicated in both pop and peek. You could extract this into a helper method to avoid code duplication:

def _transfer(self):
    while self.in_stack:
        self.out_stack.append(self.in_stack.pop())
  1. Comments: Good job including the complexity analysis in comments. You could also add brief inline comments explaining the lazy transfer strategy.

Overall, this is a solid solution that demonstrates a clear understanding of the problem.

VERDICT: PASS


Design HashMap (design_hashmap.py)

Strengths:

  1. Your solution closely mirrors the reference approach (chaining with dummy head nodes), which is a clean and proven design pattern.
  2. The prev_node helper function is well-implemented and reduces code duplication across put, get, and remove.
  3. Good use of comments to explain the overall approach at the top.
  4. The code is readable and follows Python conventions well.

Areas for improvement:

  1. Time complexity comment: Update your TC comment to reflect amortized/average O(1) rather than O(N). O(N) only describes the worst case when all keys hash to the same bucket, which is rare with 1000 buckets.
  2. Edge case in prev_node: When the head itself has the matching key, your function returns head (the dummy), and prev.next correctly points to the matching node. This works, but worth noting that the dummy head pattern elegantly handles this.
  3. Consider adding a brief docstring to each method explaining what it does — this improves maintainability.
  4. Optional optimization: For very large inputs, you could increase num_buckets (e.g., 10000) to reduce average chain length, though 1000 is fine given the constraints.

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.

2 participants