Skip to content

Complete Competitive-Coding-1 assignment - #1363

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

Complete Competitive-Coding-1 assignment#1363
tejbharath wants to merge 1 commit into
super30admin:masterfrom
tejbharath:master

Conversation

@tejbharath

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

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

Your solution appears to address a different problem (finding the k-th missing element) rather than the specific problem of finding the single missing number in a sorted array.

Key issues to address:

  1. Problem mismatch: The problem asks for the single missing number, but your solution is designed for finding the k-th missing number. You need to adjust your approach to find just the one missing number.

  2. Variable shadowing bug: You declared numberOfMissing both outside and inside the while loop. The inner declaration shadows the outer one, so the outer variable remains 0. Remove the inner declaration.

  3. Logic for single missing number: For the actual problem, you can use a simpler approach:

    • Binary search: Compare nums[mid] with mid + nums[0] to find where the discrepancy occurs.
    • Or use the formula: missing = expected_sum - actual_sum where expected_sum = n*(n+1)/2.
  4. Suggested approach (similar to reference):

    public int missingNumber(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (right - left > 1) {
            int mid = left + (right - left) / 2;
            if (nums[mid] - nums[left] != mid - left)
                right = mid;
            else
                left = mid;
        }
        return nums[left] + 1;
    }

VERDICT: NEEDS_IMPROVEMENT


Interview Problem: Design Min Heap (MinHeap.java)

Strengths:

  1. Good use of 1-indexed array approach with sentinel value at index 0.
  2. Clean separation of concerns with private helper methods (parent, leftChild, rightChild, isLeaf, swap).
  3. Insert operation correctly bubbles up to maintain heap property.
  4. Time and space complexity match the reference solution.

Areas for Improvement:

  1. Critical Bug in minHeapify: The method accesses Heap[rightChild(pos)] without checking if it's within bounds. When a node has only a left child (right child index > size), this causes an out-of-bounds access. Fix this by checking bounds before accessing children.

  2. Missing getMin() method: The problem explicitly mentions getMin() as one of the three operations. You should implement it as return Heap[FRONT];.

  3. No empty heap check in remove(): Add a check to handle the case when the heap is empty to avoid returning stale or sentinel values.

  4. Consider adding a peek() or getMin() method for O(1) access to the minimum element without removing it.

  5. Add a main method or test cases to demonstrate the heap works correctly.

  6. Code style: Consider using more descriptive variable names and adding comments for clarity.

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