Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Solution: Ceiling of a Number
Problem Statement
Given an array of numbers sorted in an ascending order, find the ceiling of a given number ‘key’. The ceiling of the ‘key’ will be the smallest element in the given array greater than or equal to the ‘key’.
Write a function to return the index of the ceiling of the ‘key’. If there isn’t any ceiling return -1.
Example 1:
Input: [4, 6, 10], key = 6
Output: 1
Explanation: The smallest number greater than or equal to '6' is '6' having index '1'.
Example 2:
Input: [1, 3, 8, 10, 15], key = 12
Output: 4
.....
.....
.....
Like the course? Get enrolled and start learning!
I
Ivy
· 4 years ago
Would it be correct in the floor solution to return either end or mid outside the loop? Since integer division "rounds down" so end and mid should always be equivalent by that point.