NumPy interview question

Data ScienceNumPymedium

A loyalty system stores points as pts = np.array([1201, 350, 85], dtype=np.int64). For a 1.5× points campaign, a notebook runs boosted = pts * 1.5 and then pts *= 1.5. What happens in current NumPy?

  1. ABoth lines run: boosted is float64, and pts *= 1.5 silently truncates in place to [1801 525 127] and stays int64, the way C converts a double that is assigned to an int variable.
  2. BBoth lines upcast: pts *= 1.5 switches pts to float64 in place, because NumPy reallocates an array's data buffer whenever an in-place result needs a wider dtype than it has.
  3. Cpts * 1.5 raises a casting error, because an int64 array cannot be mixed with a Python float without astype, while pts *= 1.5 works since in-place operations cast automatically.
  4. Dpts * 1.5 returns a new float64 array, but pts *= 1.5 raises a casting error: an in-place result must fit the int64 buffer, and float64 → int64 breaks 'same_kind'.
Show the answer and explanation

Correct answer: D, pts * 1.5 returns a new float64 array, but pts *= 1.5 raises a casting error: an in-place result must fit the int64 buffer, and float64 → int64 breaks 'same_kind'.

Explanation NumPy

A binary operation allocates a new output, so NumPy's type promotion can choose a wider dtype: int64 × float gives float64, and boosted is [1801.5, 525.0, 127.5].

An in-place operation writes into the existing buffer, so it cannot change the array's dtype. pts *= 1.5 is np.multiply(pts, 1.5, out=pts): the float64 result would have to be cast back to int64, and in-place ufuncs use the 'same_kind' casting rule, which forbids float → int. NumPy raises a UFuncTypeError (a subclass of TypeError): Cannot cast ufunc 'multiply' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'. pts is left unchanged.

StatementResult
pts * 1.5new float64 array
pts *= 2fine, the int result fits int64
pts *= 1.5UFuncTypeError
pts = pts * 1.5rebinds the name to a new float64 array
np.multiply(pts, 1.5, out=pts, casting="unsafe")truncates to [1801 525 127]

For points or money, make the rounding rule explicit, e.g. pts = np.floor(pts * 1.5).astype(np.int64), or np.round if the business rule says so.

Why the distractors fail: NumPy releases before 1.10 did allow this unsafe cast, which is exactly why the rule was tightened; an in-place operation never changes an array's dtype or swaps its buffer; and mixing an int array with a Python float is ordinary type promotion, not an error.

Interview tip: distinguish x = x + y (a new object, the dtype may change) from x += y (the same object and dtype, and the change is visible through every other reference or view of that array).

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Data Science questions

  1. A bank automatically offers a premium card to customers with an internal score of 700 or higher. You compare customers just above and just below 700 to…
  2. An online grocer's eight baskets in a pilot region have values 12, 15, 18, 22, 30, 41, 55 and 1,007. In PostgreSQL, what do PERCENTILE_CONT(0.5) WITHIN GROUP…
  3. What is an A/B test (an online controlled experiment)?
  4. In product analytics, what is meant by a north star metric?
  5. In a query that uses GROUP BY, how does the WHERE clause differ from the HAVING clause?