Create a zero-filled NumPy structured array with an RGBA uint8 dtype

Quick Overview

Define a NumPy structured dtype with four uint8 color fields and return a zero-filled array of n colors. It tests field order and types in structured dtypes, the byte layout of each record, and how to reinterpret the buffer as a plain byte array.

Create a zero-filled NumPy structured array with an RGBA uint8 dtype

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Define a structured NumPy `dtype` for a color with four `uint8` fields named `r`, `g`, `b` and `a`. Then write `make_colors(n)`, which takes an integer `n` and returns a zero-filled structured array of shape `(n,)` with that dtype. Requirements: - `.dtype.names` must equal `('r', 'g', 'b', 'a')` in that order. - Every field is `uint8`. Example: ```python arr = make_colors(2) arr.dtype.names # ('r', 'g', 'b', 'a') arr['r'].dtype # dtype('uint8') arr.shape # (2,) print(arr[0]) # (0, 0, 0, 0) ``` ```hint Field order Look for a way of writing a dtype that fixes the order of the fields as well as their types. ``` ### What a Strong Answer Covers - A dtype definition that fixes both the field order and the `uint8` type of each field - An allocation that sets every field of every record to zero - The size of one record and whether it contains padding - How `uint8` values behave on overflow when the colors are modified later ### Follow-up Questions - How would you initialize the array to opaque white instead of zeros? - What is the memory layout, and how do you view it as a plain `(n, 4)` `uint8` array? - How would you brighten every color channel by 40 without values wrapping past 255?

Overview: Define a NumPy structured dtype with four uint8 color fields and return a zero-filled array of n colors. It tests field order and types in structured dtypes, the byte layout of each record, and how to reinterpret the buffer as a plain byte array.

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

Define a structured NumPy dtype for a color with four uint8 fields named r, g, b and a. Then write make_colors(n), which takes an integer n and returns a zero-filled structured array of shape (n,) with that dtype.

Requirements:

  • .dtype.names must equal ('r', 'g', 'b', 'a') in that order.
  • Every field is uint8 .

Example:

arr = make_colors(2)
arr.dtype.names   # ('r', 'g', 'b', 'a')
arr['r'].dtype    # dtype('uint8')
arr.shape         # (2,)
print(arr[0])     # (0, 0, 0, 0)

What a Strong Answer Covers Guidance

  • A dtype definition that fixes both the field order and the uint8 type of each field
  • An allocation that sets every field of every record to zero
  • The size of one record and whether it contains padding
  • How uint8 values behave on overflow when the colors are modified later

Follow-up Questions Guidance

  • How would you initialize the array to opaque white instead of zeros?
  • What is the memory layout, and how do you view it as a plain (n, 4) uint8 array?
  • How would you brighten every color channel by 40 without values wrapping past 255?