Insertion tabs: First qualifying slip
Stuck or curious how this works? Visualize Binary Search ↗
Inputs
drawers contains exactly two lists: the sorted numbers in drawers[0], then the query numbers in drawers[1].
Your tasks
- Create a list
positions.drawerscontains two rows: a sorted number list, then a list of queries. Each query is a number to look up. For each query, find the first position in the sorted list whose number is at least the query. Positions, also called indexes, start at 0. If no number is large enough, record the length of the sorted list. Put the answers in query order. - Read
positionsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
insertionTabsFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If drawers is [[-8, 3, 5, 5, 6], [-8, 5, 6, 3, 5]] and k is 8, then positions is [0, 2, 4, 1, 2] and insertionTabsFirstQualifyingSlip returns -1.
Rules
- If no number is at least
k, return -1. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: drawers is [[-4, 0, 2, 2, 6], []] and k is 7.
TEST DATA 2: drawers is [[-4, 4, 4, 4], [16]] and k is 4.
Examples
Input: drawers = [[-4,0,2,2,6],[]] k = 7 Output: -1
positions is []. Here k is 7. No number is at least 7, so return -1.
Input: drawers = [[-4,4,4,4],[16]] k = 4 Output: 4
positions is [4]. Here k is 4. The first number at least 4 is 4. Return that value.
Input: drawers = [[],[]] k = 4 Output: -1
positions is []. Here k is 4. No number is at least 4, so return -1.
Input limits
- 0 <= input size <= 64 per row.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- The input has exactly two rows, each containing at most 64 integers.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(q log(n+1)); extra memory, including the helper list, O(n). n is the length of the sorted number list; q is the number of queries. These labels describe how work and memory grow with the input.
Practice focus
Build positions first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.