Skip to content

Design-2 solution#2451

Open
shakthinandana wants to merge 2 commits intosuper30admin:masterfrom
shakthinandana:master
Open

Design-2 solution#2451
shakthinandana wants to merge 2 commits intosuper30admin:masterfrom
shakthinandana:master

Conversation

@shakthinandana
Copy link
Copy Markdown

No description provided.

Shakthi Nandana added 2 commits April 7, 2026 22:39
@super30admin
Copy link
Copy Markdown
Owner

Create Queue using Stacks (queue-using-stack.py)

Your solution is very good and follows the standard approach for implementing a queue using two stacks. Here are a few points for improvement:

  1. In the empty method, the expression not bool(self.stack_in or self.stack_out) is a bit redundant. The bool call is unnecessary because the not operator already treats the expression as a boolean. Moreover, it is more common to write this as return not (self.stack_in or self.stack_out). However, note that this is equivalent to return not self.stack_in and not self.stack_out due to De Morgan's laws. The reference solution uses the latter, which is more explicit and easier to read. Consider changing it to:

    return not self.stack_in and not self.stack_out
  2. While your code is correct, it's important to note that in the pop method, you call self.peek() to ensure the output stack has elements. This is correct because peek handles the transfer if needed. However, you should also consider the case when the queue is empty. The problem states that all calls to pop and peek are valid, so you don't need to handle an empty queue in pop or peek beyond what is required. But for robustness, the reference solution checks if self.empty(): return -1 in pop. Since the problem constraints say all calls are valid, this is optional. However, it's good practice to handle edge cases if you were to extend the code.

  3. Your comments at the top are good, but you might want to add a brief explanation of the approach for future reference.

Overall, your solution is efficient and clean. Keep up the good work!

VERDICT: PASS


Implement Hash Map (hashmap.py)

Your solution is well-structured and uses a double hashing technique with a 2D array. This approach ensures O(1) time complexity for all operations, which is excellent. The code is readable and you have provided helpful comments.

However, there are a few points to consider:

  1. You have used class variables for buckets and bucket_size. While this is acceptable since these are constants, it is generally better to use instance variables if there is a possibility of multiple instances (though in this problem, only one instance is created). Alternatively, you can keep them as class variables since they are constants.

  2. Your solution pre-allocates a fixed size secondary array (1001 elements) for each primary bucket that is used. This might lead to higher memory usage compared to a dynamic linked list approach (as in the reference solution), especially if only a few keys are stored. However, given the constraints (keys up to 10^6), the memory usage is acceptable.

  3. Note that your solution is specific to the key range (0 to 10^6). If the key range were to change, the constants might need adjustment. But for this problem, it is perfect.

  4. One minor improvement: in the put method, you might want to check if the value being inserted is the same as the existing value to avoid unnecessary assignment. However, this is not required.

Overall, your solution is correct and efficient.

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