Skip to content

Completed Design-1. - #2690

Open
lakshmidurgat wants to merge 3 commits into
super30admin:masterfrom
lakshmidurgat:master
Open

Completed Design-1.#2690
lakshmidurgat wants to merge 3 commits into
super30admin:masterfrom
lakshmidurgat:master

Conversation

@lakshmidurgat

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Design HashSet (Design HashSet.java)

Strengths:

  • Good overall approach using a two-level hash structure
  • O(1) time complexity for all operations
  • Clean separation of methods
  • Helpful inline comments

Areas for Improvement:

  1. Critical Bug - Edge Case Handling: Your solution will throw an ArrayIndexOutOfBoundsException when key = 1000000 (the maximum allowed value). When key % 1000 == 0, the secondary index key / 1000 can be 1000, but your secondary array only has size 1000 (valid indices 0-999). You need to handle this edge case, similar to the reference solution:

    if (set[z] == null) {
        if (z == 0) {
            set[z] = new boolean[1001]; // +1 for the edge case
        } else {
            set[z] = new boolean[1000];
        }
    }
  2. Variable Naming: Use more descriptive variable names like primaryIndex and secondaryIndex instead of z and s.

  3. Simplify contains: After the null check, you can directly return set[z][s] instead of the if-else structure.

  4. Misleading Comments: The space complexity comments stating "O(1)" are incorrect. The actual space complexity is O(n) where n is the number of unique keys stored.

VERDICT: NEEDS_IMPROVEMENT


Min Stack (minStack.java)

Strengths:

  • Correct implementation with O(1) time complexity for all operations.
  • The approach of only pushing to minSt when value <= minSt.peek() is a valid optimization that saves space compared to always pushing.
  • Good code organization and clear comments explaining the approach.

Areas for Improvement:

  1. Raw types: Use parameterized types: new Stack<Integer>() instead of new Stack() to avoid compiler warnings.
  2. Comparison style: Instead of st.peek().equals(minSt.peek()), consider using st.peek().intValue() == minSt.peek().intValue() or rely on auto-unboxing with == for primitive comparison clarity.
  3. Edge case consideration: While the problem guarantees non-empty stack operations, adding defensive checks could improve robustness.
  4. Variable naming: The reference uses val while you use value - both are fine, but consistency with the problem statement might be preferred.

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