Skip to content

Implement twoSum method in Solution class#1186

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

Implement twoSum method in Solution class#1186
Praniksha123 wants to merge 1 commit into
super30admin:masterfrom
Praniksha123:master

Conversation

@Praniksha123

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Interview Problem : Two elements sum to target (competive.java)

Strengths:

  • Excellent use of HashMap to achieve O(n) time complexity, which meets the follow-up requirement for less than O(n²)
  • Clean and readable code structure
  • Correct logic for finding the complement
  • Good variable naming (res for result/complement)

Areas for Improvement:

  • The final return statement returns an empty array new int[]{} instead of new int[]{-1,-1} for consistency with the problem's error handling pattern
  • Consider initializing HashMap with initial capacity for slightly better performance: new HashMap<>(nums.length) to avoid rehashing

Overall: This is a solid solution that demonstrates understanding of hash-based optimization. The approach is more efficient than the reference solution in terms of time complexity.

VERDICT: PASS


Interview Problem: 0-1 Knapsack Problem

Strengths:

  • The Two Sum solution is well-implemented with O(n) time complexity
  • Uses appropriate data structure (HashMap) for O(1) lookup
  • Clean code structure with good variable naming
  • Handles the case where no solution exists by returning an empty array

Critical Issues:

  1. Wrong Problem: The submitted solution solves the Two Sum problem, not the 0-1 Knapsack Problem. This appears to be a case of submitting the wrong file or misunderstanding the assignment.
  2. Missing Knapsack Implementation: There is no implementation of the DP-based knapsack solution using 2D arrays as shown in the reference solution.
  3. No Comparison to Reference: Since the problem is completely different, no meaningful comparison can be made with the reference solution's time/space complexity.

Recommendation:
The student needs to implement the 0-1 Knapsack Problem using dynamic programming. The reference solution demonstrates the correct approach:

  • Create a 2D DP array where dp[i][j] represents maximum value using first i items with capacity j
  • Iterate through items and capacities
  • For each item, either include it (if weight allows) or exclude it
  • Use Math.max() to choose the better option

VERDICT: NEEDS_IMPROVEMENT

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