Skip to content

Complete Binaray-Search-1 assignment - #2516

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

Complete Binaray-Search-1 assignment#2516
tejbharath wants to merge 3 commits into
super30admin:masterfrom
tejbharath:master

Conversation

@tejbharath

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Search in Rotated Sorted Array (RotatedSortedArraySearch.java)

Great work overall! Your solution correctly implements the binary search algorithm for a rotated sorted array with O(log n) time complexity. The logic for distinguishing between left-sorted and right-sorted halves is accurate.

A few points to improve:

  1. Input Validation Bug: Your validation condition if(nums == null && nums.length == 0) has a logical error. This should be if(nums == null || nums.length == 0) using OR instead of AND. With AND, if nums is null, accessing nums.length will throw a NullPointerException. Also, since the problem guarantees nums.length >= 1, this validation might be unnecessary, but if you keep it, make sure it's correct.

  2. Edge Case Handling: Consider what happens when low == mid or high == mid. Your current logic handles these correctly, but it's worth testing edge cases like a single-element array or an array that isn't rotated.

  3. Code Style: Consider adding a brief comment explaining the overall approach at the top of the method, not just the algorithm description.

Overall, your solution is correct and efficient. The main issue is the minor bug in the input validation, which doesn't affect the actual algorithm's correctness given the problem constraints.

VERDICT: PASS


Search in a Sorted Array of Unknown Size (SearchUnknownArraySize.java)

Your solution is correct and well-implemented! The approach matches the reference solution perfectly. Here are some observations:

Strengths:

  • Clear comments explaining the algorithm's logic
  • Proper use of exponential search followed by binary search
  • Correct handling of the unknown array size
  • Good variable naming

Minor improvements:

  • The final check if (reader.get(left) == target) after the binary search loop is redundant. When the binary search loop exits without returning, it means the target was not found in the range [left, right]. At that point, left > right, and reader.get(left) would either return 2^31 - 1 (out of bounds) or a value greater than target. You can safely remove this check and just return -1.
  • Consider adding a brief comment explaining why the exponential expansion is needed (because we don't know the array size).

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

VERDICT: PASS


Search a 2D Matrix (2DMatrixSearch.java)

EVE-2024 (EVE Evaluation Framework v1.0)

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