completed design - 2#2473
Open
yashhh-23 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds Java solutions for two LeetCode “design” problems (Queue using Stacks, and Design HashMap), including complexity notes and brief approach explanations.
Changes:
- Implemented
MyQueueusing two stacks with amortized O(1) operations. - Implemented
MyHashMapusing separate chaining via bucketed linked lists. - Expanded header comments with complexity, status, and approach notes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+46
to
+59
| } | ||
|
|
||
| //Problem 2:Design Hashmap (https://leetcode.com/problems/design-hashmap/) | ||
|
|
||
| // Time Complexity : O(1) for put, get, and remove operations. | ||
| // Space Complexity : O(n) in the worst case if all keys hash to the same bucket, but on average it will be O(1) if the hash function distributes keys equally. | ||
| // Did this code successfully run on Leetcode : Yes | ||
| // Any problem you faced while coding this : I understood the problem and implemented the solution but faced issues on how to write the code in java according to problem requirements. | ||
|
|
||
| // Your code here along with comments explaining your approach | ||
| //used an array of buckets, and each bucket stores a linked list of (key, value) pairs. The hash function key % SIZE decides which bucket a key belongs to, and if multiple keys land in the same bucket, we keep them in that list. For put, get, and remove, we only scan one bucket. | ||
| import java.util.LinkedList; | ||
|
|
||
| class MyHashMap { |
Comment on lines
+10
to
+14
| import java.util.Stack; | ||
|
|
||
| class MyQueue { | ||
| private Stack<Integer> inStack; | ||
| private Stack<Integer> outStack; |
Comment on lines
+61
to
+68
| private LinkedList<int[]>[] buckets; | ||
|
|
||
| public MyHashMap() { | ||
| buckets = new LinkedList[SIZE]; | ||
| for (int i = 0; i < SIZE; i++) { | ||
| buckets[i] = new LinkedList<>(); | ||
| } | ||
| } |
Comment on lines
+50
to
+51
| // Time Complexity : O(1) for put, get, and remove operations. | ||
| // Space Complexity : O(n) in the worst case if all keys hash to the same bucket, but on average it will be O(1) if the hash function distributes keys equally. |
Owner
Create Queue using Stacks (Sample.java)Strengths:
Areas for Improvement:
Overall, this is a solid implementation that correctly solves the problem with good code structure and efficiency. VERDICT: PASS Implement Hash MapStrengths:
Areas for Improvement:
Overall, the HashMap implementation is solid and meets all requirements. VERDICT: PASS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.