Vectorized Insertion Indices in a Sorted NumPy Array

Quick Overview

For each value in a query array, count how many elements of a sorted NumPy array are strictly less than it, using one vectorized call and no Python loop. It tests binary search over sorted data, tie handling at equal values, and what changes when the array is not sorted.

Vectorized Insertion Indices in a Sorted NumPy Array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a sorted 1-D array `a` and a 1-D array `queries`, return an integer array (same length as `queries`) giving, for each query, the count of elements in `a` strictly less than it (its sorted insertion index). Use a single vectorized call, no Python loop. For example: ```python a = np.array([1, 3, 3, 5, 7]) queries = np.array([0, 3, 4, 8]) # Output: [0, 1, 3, 5] ``` ```hint Use the sort order Because `a` is sorted, each answer is a position you could find with a binary search. Look for a NumPy routine that runs that search for a whole array of queries at once, and check how it treats queries equal to elements of `a`. ``` ### Constraints & Assumptions - `a` is 1-D and sorted in ascending order; it may contain duplicates. - `queries` is 1-D and not necessarily sorted. - The answer must come from one vectorized call, with no Python loop over the queries. ### What a Strong Answer Covers - The NumPy call that answers every query at once, and why it works only because `a` is sorted. - How queries equal to one or more elements of `a` are counted, and how to switch to the "less than or equal" count. - Queries below the smallest or above the largest element, and empty inputs. - Time and space complexity in terms of the number of queries and the length of `a`. ### Follow-up Questions - How would you get the rightmost insertion point instead? - What if `a` is not sorted? - How would you count the elements of `a` that fall in each of many half-open intervals `[lo, hi)` without a loop?

Overview: For each value in a query array, count how many elements of a sorted NumPy array are strictly less than it, using one vectorized call and no Python loop. It tests binary search over sorted data, tie handling at equal values, and what changes when the array is not sorted.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a sorted 1-D array a and a 1-D array queries, return an integer array (same length as queries) giving, for each query, the count of elements in a strictly less than it (its sorted insertion index). Use a single vectorized call, no Python loop.

For example:

a = np.array([1, 3, 3, 5, 7])
queries = np.array([0, 3, 4, 8])
# Output: [0, 1, 3, 5]

Constraints & Assumptions

  • a is 1-D and sorted in ascending order; it may contain duplicates.
  • queries is 1-D and not necessarily sorted.
  • The answer must come from one vectorized call, with no Python loop over the queries.

What a Strong Answer Covers Guidance

  • The NumPy call that answers every query at once, and why it works only because a is sorted.
  • How queries equal to one or more elements of a are counted, and how to switch to the "less than or equal" count.
  • Queries below the smallest or above the largest element, and empty inputs.
  • Time and space complexity in terms of the number of queries and the length of a .

Follow-up Questions Guidance

  • How would you get the rightmost insertion point instead?
  • What if a is not sorted?
  • How would you count the elements of a that fall in each of many half-open intervals [lo, hi) without a loop?