Skip to content

Done Competitive-Coding-1#1346

Open
samikshm wants to merge 2 commits into
super30admin:masterfrom
samikshm:master
Open

Done Competitive-Coding-1#1346
samikshm wants to merge 2 commits into
super30admin:masterfrom
samikshm:master

Conversation

@samikshm
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Problem 1 (Missing Number):

  • The binary search approach is correct and well-implemented for finding the missing number in a sorted array where nums[i] == i + 1 before the missing element.
  • The code handles the edge case where the missing number could be at the end (returns low + 1 correctly).
  • However, there's a potential bug: if the array is empty, high = nums.length - 1 would be -1, and the while loop wouldn't execute, returning low + 1 = 1, which is incorrect for an empty array.
  • The algorithm correctly identifies the first mismatching position and returns the missing number.

Problem 2 (Min Heap):

  • The min heap implementation is well-structured with clear separation of concerns (push, pop, peek, size, heapify, swap).
  • The bubble-up logic in push and heapify-down logic in pop are correctly implemented.
  • Edge cases are handled: pop on empty heap returns gracefully, peek on empty heap returns -1.
  • The heapify method correctly finds the smallest among parent, left child, and right child.
  • The implementation follows proper OOP principles with private helper methods.

Areas for Improvement:

  1. Problem 1: Add a check for empty array at the beginning.
  2. Problem 2: Consider adding a method to check if the heap is empty rather than relying on size() == 0 checks.
  3. Both solutions could benefit from more descriptive variable names (e.g., mid could be midIndex).
  4. Problem 2: The peek method returning -1 for empty heap is a design choice - consider throwing an exception or documenting this behavior.

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