Bird
Raised Fist0
DSA Cprogramming~10 mins

Floor and Ceil in Sorted Array in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the floor of a target in a sorted array.

DSA C
int floorInSortedArray(int arr[], int n, int target) {
    int low = 0, high = n - 1, floor = -1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] < target) {
            floor = mid;
            low = [1];
        } else {
            high = mid - 1;
        }
    }
    return floor;
}
Drag options to blanks, or click blank then click option'
Alow + 1
Bmid - 1
Cmid + 1
Dhigh - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1 causes infinite loop or wrong search direction.
2fill in blank
medium

Complete the code to find the ceil of a target in a sorted array.

DSA C
int ceilInSortedArray(int arr[], int n, int target) {
    int low = 0, high = n - 1, ceil = -1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] > target) {
            ceil = mid;
            high = [1];
        } else {
            low = mid + 1;
        }
    }
    return ceil;
}
Drag options to blanks, or click blank then click option'
Amid + 1
Blow - 1
Chigh + 1
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 causes wrong search direction.
3fill in blank
hard

Fix the error in the binary search loop condition to avoid infinite loops.

DSA C
while ([1]) {
    int mid = low + (high - low) / 2;
    // rest of code
}
Drag options to blanks, or click blank then click option'
Alow < high
Blow <= high
Clow == high
Dlow > high
Attempts:
3 left
💡 Hint
Common Mistakes
Using low < high misses checking when low equals high.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

DSA C
int lengths[] = {0};
for (int i = 0; i < n; i++) {
    if ([1] > 3) {
        lengths[i] = [2];
    }
}
Drag options to blanks, or click blank then click option'
Astrlen(words[i])
Bwords[i]
Ci
Dstrlen(words)
Attempts:
3 left
💡 Hint
Common Mistakes
Using words[i] directly instead of its length.
Using strlen(words) which is incorrect.
5fill in blank
hard

Fill all three blanks to create a map of keys to values where values are positive.

DSA C
typedef struct {
    char key[20];
    int value;
} Pair;

Pair result[100];
int count = 0;
for (int i = 0; i < n; i++) {
    if (data[i].[1] > 0) {
        strcpy(result[count].[2], data[i].[3]);
        result[count].value = data[i].value;
        count++;
    }
}
Drag options to blanks, or click blank then click option'
Avalue
Bkey
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up key and value fields.
Not copying the key string properly.

Practice

(1/5)
1. What is the floor of a number in a sorted array?
easy
A. The smallest element strictly less than the number
B. The smallest element greater than or equal to the number
C. The average of all elements less than the number
D. The greatest element less than or equal to the number

Solution

  1. Step 1: Understand floor definition in sorted array

    The floor of a number is the greatest element in the array that is less than or equal to the given number.
  2. Step 2: Compare options with definition

    The greatest element less than or equal to the number matches the definition exactly, others describe ceil or incorrect concepts.
  3. Final Answer:

    The greatest element less than or equal to the number -> Option D
  4. Quick Check:

    Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor is strictly less than number
  • Mixing floor with average or median
2. Which of the following is the correct C syntax to find the middle index in binary search for array of size n?
easy
A. int mid = (0 + n) / 2;
B. int mid = 0 + n - 1 / 2;
C. int mid = (0 + (n - 1)) >> 1;
D. int mid = n / 2;

Solution

  1. Step 1: Understand middle index calculation in binary search

    For array indices 0 to n-1, middle is (0 + n-1) / 2 or equivalent.
  2. Step 2: Analyze options for correct syntax and efficiency

    int mid = (0 + (n - 1)) >> 1; uses bitwise right shift for division by 2, which is efficient and correct for integer mid calculation.
  3. Final Answer:

    int mid = (0 + (n - 1)) >> 1; -> Option C
  4. Quick Check:

    Mid = (start + end) >> 1 [OK]
Hint: Use (start + end) >> 1 for efficient mid calculation [OK]
Common Mistakes:
  • Using n instead of n-1 as end index
  • Forgetting parentheses causing wrong precedence
  • Using floating division instead of integer
3. Given the sorted array arr = {1, 3, 5, 7, 9}, what is the floor of 6?
medium
A. 7
B. 5
C. 6
D. -1

Solution

  1. Step 1: Identify elements less than or equal to 6

    Elements ≤ 6 are 1, 3, 5.
  2. Step 2: Find greatest among these

    The greatest element ≤ 6 is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Floor(6) = 5 [OK]
Hint: Floor is largest ≤ target in sorted array [OK]
Common Mistakes:
  • Choosing 7 which is greater than 6
  • Assuming floor equals the number if not present
  • Returning -1 incorrectly when floor exists
4. Consider this code snippet to find ceil in a sorted array int ceil = -1; for(int i=0; i= x) { ceil = arr[i]; break; } }. What is the main issue?
medium
A. It may return wrong value if array is not sorted
B. It does not handle empty array
C. It returns first element ≥ x, which is correct for ceil
D. It finds floor instead of ceil

Solution

  1. Step 1: Understand the code logic

    The code returns the first element in array ≥ x, which is correct if array is sorted.
  2. Step 2: Identify assumptions and issues

    If array is not sorted, first element ≥ x may not be the smallest such element, so ceil is incorrect.
  3. Final Answer:

    It may return wrong value if array is not sorted -> Option A
  4. Quick Check:

    Ceil requires sorted array for correctness [OK]
Hint: Ceil logic needs sorted array to work correctly [OK]
Common Mistakes:
  • Ignoring array sorting requirement
  • Confusing ceil with floor
  • Not checking for empty array before loop
5. You have a sorted array arr = {2, 4, 6, 8, 10}. If you want to find the ceil of 11 using binary search, what should be the returned value?
hard
A. -1
B. 11
C. 10
D. Error

Solution

  1. Step 1: Understand ceil definition and array elements

    Ceil is smallest element ≥ target. Here, target is 11, which is greater than all elements.
  2. Step 2: Check if any element ≥ 11 exists

    No element in array ≥ 11, so ceil does not exist, return -1.
  3. Final Answer:

    -1 -> Option A
  4. Quick Check:

    No element ≥ 11 means ceil = -1 [OK]
Hint: If target > max element, ceil is -1 (not found) [OK]
Common Mistakes:
  • Returning last element as ceil incorrectly
  • Returning target itself if not found
  • Not handling no ceil case properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard