Skip to content

Copleted 1 problem - #1360

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

Copleted 1 problem#1360
lakshmidurgat wants to merge 1 commit into
super30admin:masterfrom
lakshmidurgat:master

Conversation

@lakshmidurgat

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Interview Problem: Find Missing Number in a sorted array (Problem1.java)

Your solution attempts a binary search approach, which is the right idea for O(log n) performance. However, there are several issues to address:

  1. Fix compilation errors first:

    • Replace hi with h (the parameter name)
    • Make leftBinarySearch and rightBinarySearch static so they can be called from the static missingNumber method
    • Add bounds checks: if (mid > 0 && arr[mid-1] + 1 != arr[mid]) and if (mid < arr.length - 1 && arr[mid+1] - 1 != arr[mid])
  2. Simplify your approach: Instead of two separate binary searches, use a single binary search based on the difference between value and index. The reference solution elegantly uses arr[i] - i — if all numbers were present, this difference would be constant. When the difference changes, you've found the missing region.

  3. Cleaner implementation suggestion:

static int missingNumber(int[] arr) {
    int low = 0, high = arr.length - 1;
    while (high - low > 1) {
        int mid = low + (high - low) / 2;
        if (arr[low] - low != arr[mid] - mid)
            high = mid;
        else
            low = mid;
    }
    return arr[low] + 1;
}
  1. Naming conventions: Use descriptive variable names like low and high instead of single letters, and fix typos in method names.

  2. Test edge cases: Consider what happens when the missing number is at the beginning or end of the array.

VERDICT: NEEDS_IMPROVEMENT


Interview Problem: Design Min Heap

Your submission does not address the problem at all. The problem requires you to implement a Min Heap data structure with the following operations:

  1. getMin(): Returns the root element in O(1)
  2. extractMin(): Removes and returns the minimum element in O(log N)
  3. insert(): Adds a new key while maintaining heap property in O(log N)

Your code appears to be a solution for a different problem (finding a missing number in an array). Additionally, your code has several compilation errors:

  • Using hi instead of h in the binary search methods
  • Missing return statements in some methods
  • Mixing static and non-static method declarations
  • Incorrect logic for the missing number problem

Please re-read the problem carefully and implement a Min Heap using an array-based approach. You'll need:

  • An array to store heap elements
  • Helper methods to calculate parent and child indices (parent = (i-1)/2, left child = 2i+1, right child = 2i+2)
  • A heapify method to maintain the heap property after removal
  • An insert method that adds elements and bubbles up
  • An extractMin method that removes the root and heapifies down

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