Skip to content

Implemented Hashmap and Queue using stacks#2454

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

Implemented Hashmap and Queue using stacks#2454
sumanth00100 wants to merge 2 commits intosuper30admin:masterfrom
sumanth00100:master

Conversation

@sumanth00100
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Create Queue using Stacks (implement-queue-using-stacks.java)

Your solution is correct and efficient. You have successfully implemented the queue using two stacks with amortized O(1) time complexity for each operation. The code is clean and follows good practices.

One minor point: while the problem constraints ensure that pop and peek are only called when the queue is non-empty, in a real-world scenario, it's good practice to handle edge cases explicitly. For example, you could add a check in pop and peek to throw a more descriptive exception if the queue is empty. However, given the problem constraints, this is not necessary.

Overall, great job! Your solution is on par with the reference solution.

VERDICT: PASS


Implement Hash Map (design-hashmap.java)

Strengths:

  • The solution is straightforward and easy to understand.
  • All operations (put, get, remove) run in O(1) time, which meets the expected time complexity.
  • The code is concise and correctly handles the problem constraints.

Areas for Improvement:

  1. Space Efficiency: The array size is fixed at 1,000,001, which is acceptable given the constraints (keys up to 10^6) but is inefficient in terms of memory. A typical hash map uses a smaller array and handles collisions with linked lists or other methods. This solution uses O(max_key) space, which is 1,000,001, while the reference solution uses O(n) space where n is the number of elements. For keys that are sparse, this wastes a lot of memory.

  2. Generality: This solution is tailored to the specific constraints of the problem (keys in [0, 10^6]) and is not a general hash map. It would not work efficiently if keys were larger or if the range of keys was unknown.

  3. Best Practices: While the solution works, it does not demonstrate understanding of hash map principles like hashing, collision resolution, and dynamic resizing. The problem is intended to teach these concepts.

Suggestions:

  • Consider implementing a more traditional hash map with a fixed number of buckets (e.g., 1000) and using linked lists to handle collisions. This would be more memory efficient for sparse keys and would be a more general solution.
  • The reference solution provides an example of such an implementation. It uses a linked list in each bucket to store key-value pairs, and it handles collisions by chaining.

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