Skip to content

Hashing-2 solution - #2199

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

Simranb10 wants to merge 1 commit into
super30admin:masterfrom
Simranb10:master

Conversation

@Simranb10

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Subarray Sum Equals K (SubArraySum.java)

Great job! Your solution correctly implements the optimal prefix sum + HashMap approach for this problem. The logic is sound, and you've handled the edge case of subarrays starting from index 0 by initializing the map with 0 -> 1.

A few minor suggestions to improve code quality:

  1. You can simplify the map update logic using getOrDefault:

    map.put(sum, map.getOrDefault(sum, 0) + 1);

    This is more concise and idiomatic Java.

  2. Consider adding comments to explain the key insight (why we look for sum - k in the map) for future readers.

  3. The variable name curr could be more descriptive, like targetPrefixSum or neededSum.

Overall, this is a solid solution that demonstrates a good understanding of the problem and the optimal approach.

VERDICT: PASS


Contiguous Array (ContiguousArray.java)

Excellent work! Your solution is correct, efficient, and well-implemented. Here are some observations:

Strengths:

  • Your solution correctly implements the running sum technique with the clever insight of treating 0s as -1 and 1s as +1
  • The initialization with count.put(0, -1) is correctly handled to account for subarrays starting from index 0
  • Using Map<Integer, Integer> interface instead of concrete HashMap is a good practice (dependency inversion)
  • The code is clean, readable, and well-commented with complexity analysis
  • Time and space complexity match the optimal solution

Minor suggestions for improvement:

  • Consider adding a brief comment explaining the algorithm approach (e.g., "Treat 0 as -1 and 1 as +1, then find longest subarray with sum 0")
  • The variable name count could be more descriptive - perhaps sumIndexMap would better convey its purpose
  • Consider edge case handling: what if nums is null or empty? (Though this may not be required by the problem constraints)

Overall, this is a high-quality solution that demonstrates a strong understanding of the problem and the optimal approach.

VERDICT: PASS


Longest Palindrome (LongestPalindrome.java)

E.g., Format:
VERtdICT: NEEDS_IMPROVEMENT

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