Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value in a sorted array.
DSA C#
int floor = -1; for (int i = 0; i < arr.Length; i++) { if (arr[i] <= target) { floor = arr[i]; } else { break; } } return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the target instead of the floor value.
Returning the index variable instead of the floor value.
✗ Incorrect
The variable 'floor' stores the greatest element less than or equal to the target, so we return 'floor'.
2fill in blank
mediumComplete the code to find the ceil value in a sorted array.
DSA C#
int ceil = -1; for (int i = 0; i < arr.Length; i++) { if (arr[i] >= target) { ceil = arr[i]; break; } } return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the target instead of the ceil value.
Returning the index variable instead of the ceil value.
✗ Incorrect
The variable 'ceil' stores the smallest element greater than or equal to the target, so we return 'ceil'.
3fill in blank
hardFix the error in the binary search to find floor in a sorted array.
DSA C#
int low = 0, high = arr.Length - 1; int floor = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] [1] target) { floor = arr[mid]; low = mid + 1; } else { high = mid - 1; } } return floor;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '<=' causing wrong floor calculation.
Using '<' or '>' which misses equal elements.
✗ Incorrect
To find the floor, we move right when arr[mid] is less than or equal to target, so the condition is arr[mid] <= target.
4fill in blank
hardFill both blanks to complete the binary search for ceil in a sorted array.
DSA C#
int low = 0, high = arr.Length - 1; int ceil = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] [1] target) { ceil = arr[mid]; [2] = mid - 1; } else { low = mid + 1; } } return ceil;
Attempts:
3 left
💡 Hint
Common Mistakes
Moving low instead of high when arr[mid] >= target.
Using wrong comparison operator causing incorrect ceil.
✗ Incorrect
For ceil, if arr[mid] >= target, update ceil and move high to mid - 1 to find smaller ceil candidates.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each number to its floor and ceil in a sorted array.
DSA C#
var result = new Dictionary<int, (int floor, int ceil)>(); foreach (var num in nums) { int floor = -1, ceil = -1; int low = 0, high = arr.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] [1] num) { floor = arr[mid]; low = mid + 1; } else { high = mid - 1; } } low = 0; high = arr.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] [2] num) { ceil = arr[mid]; [3] = mid - 1; } else { low = mid + 1; } } result[num] = (floor, ceil); }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up floor and ceil conditions.
Moving wrong pointers in binary search loops.
✗ Incorrect
For floor, use '<=' and move low up; for ceil, use '>=' and move high down.
