Skip to content

Complete Competitive-Coding1#1342

Open
paridhimalviya wants to merge 1 commit intosuper30admin:masterfrom
paridhimalviya:master
Open

Complete Competitive-Coding1#1342
paridhimalviya wants to merge 1 commit intosuper30admin:masterfrom
paridhimalviya:master

Conversation

@paridhimalviya
Copy link
Copy Markdown

Problem 1 - Find Missing Number in a sorted array
Problem 2 - Design Min Heap

@super30admin
Copy link
Copy Markdown
Owner

Interview Problem: Find Missing Number in a sorted array (FindMissingNumberInSortedArray.swift)

Your solution shows that you understand the concept of using binary search to find the missing number by comparing the difference between the value and the index. However, there are a few critical issues that need to be addressed:

  1. Termination Condition: The condition while ((high - low) >= 2) should be changed to while (high - low > 1) as in the reference solution. This ensures that the loop continues until there are exactly two elements left, at which point the missing number is between them.

  2. Comparison Logic: In the reference solution, the code checks if the difference between the value and index for the left segment (ar[a] - a) is not equal to the mid segment (ar[mid] - mid). If so, the missing number is in the left half. Similarly for the right half. Your code checks both left and mid, and mid and right, but it should be more precise. The reference solution uses:

    • If (ar[a] - a) != (ar[mid] - mid), then the missing number is in the left half (so set b = mid).
    • Else if (ar[b] - b) != (ar[mid] - mid), then the missing number is in the right half (so set a = mid).

    Your code currently checks differenceOfLow != differenceOfMid and differenceOfMid != highIndexDifference, which is similar but note that the reference solution uses an else-if to avoid unnecessary checks.

  3. Return Value: The return value should be nums[low] + 1 or nums[high] - 1, but actually, since the array is contiguous except for one missing number, the correct missing number is nums[low] + 1 (or equivalently nums[high] - 1). Your current approach of averaging nums[low] and nums[high] only works if exactly one number is missing between them, but in this problem, there is exactly one missing number overall, so it should be correct. However, if the binary search does not correctly narrow down to two consecutive numbers, this might fail. For instance, if the array is [2, 3, 4, 5] (missing 1), your code might not handle the edge case.

  4. Edge Cases: Consider cases where the missing number is the first element (1) or the last element (n). For example, if the array is [2, 3, 4, 5], the missing number is 1. Your code should return 1. Similarly, if the array is [1, 2, 3, 4], the missing number is 5. Ensure your binary search handles these.

  5. Debugging Prints: Remove the print statements in the final code for clarity and professionalism.

  6. Code Structure: The class and function are well-named, but the initializer that calls the function and prints is not necessary for a utility function. It's better to have a separate function that can be tested.

To improve your solution, study the reference solution provided and adjust your code accordingly. Focus on the termination condition and the logic for moving the pointers. Also, test with various edge cases.

VERDICT: NEEDS_IMPROVEMENT


Interview Problem: Design Min Heap (DesignMinHeap.swift)

Strengths:

  • You have implemented the basic structure of a min-heap with insert and delete operations.
  • The use of generics makes the heap reusable for any comparable type.
  • The heapifyUp method is correctly implemented iteratively.

Areas for improvement:

  1. The delete method should not take an argument. It should always remove and return the min element (the root). Rename it to extractMin or removeMin and remove the parameter.
  2. Fix the heapifyDown method: the condition should check if the parent is greater than the child to swap. Change the condition to:
    if elements[parentIndex] > elements[childIndex] {
        elements.swapAt(parentIndex, childIndex)
        parentIndex = childIndex
        leftChildIndex = 2 * parentIndex + 1
    } else {
        break
    }
    
    Alternatively, you can restructure the loop to swap when needed.
  3. Handle edge cases: when the heap is empty, extractMin should return nil (since you're using an optional). Also, when there's only one element, heapifyDown should not run.
  4. Consider adding a peek method (which you have as min) and isEmpty check.
  5. Remove the test class MinHeapImpl from the solution unless required. The problem is to implement the heap, not to test it.

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