Select the last two columns of every second row in a NumPy array with one slice

Quick Overview

Select the last two columns of every second row of a 2-D NumPy array with a single slicing expression. It tests stepped and negative-index slicing, how the output shape depends on the row count, and whether basic slicing returns a view or a copy.

Select the last two columns of every second row in a NumPy array with one slice

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D NumPy array `arr`, return the last two columns of every second row (rows `0, 2, 4, ...`) using a single slicing expression: no Python loops or manual index building. Requirements: - Rows: step 2 from index `0`. Columns: use negative indexing so the code works for any number of columns. - Use one combined indexing expression on `arr`. - Returning a view is acceptable. Example: ```python arr = np.array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]) # expected result: # array([[ 3, 4], # [11, 12], # [19, 20]]) ``` ```hint Start, stop and step per axis Work out the start, stop and step each axis needs, and remember that a negative start counts from the end of that axis. ``` ### What a Strong Answer Covers - Writes one indexing expression with a stepped slice on the rows and a negative-start slice on the columns - States the output shape for an array with `n` rows, for both odd and even `n` - Explains whether the result shares memory with `arr` and what that means for later writes - Covers unusual inputs: fewer than two columns, zero rows, and a 1-D array ### Follow-up Questions - Is the result a view or a copy, and why does it matter? - How would you select every second row starting from the last row instead? - When would you need the result as an independent, contiguous array, and how would you get one?

Overview: Select the last two columns of every second row of a 2-D NumPy array with a single slicing expression. It tests stepped and negative-index slicing, how the output shape depends on the row count, and whether basic slicing returns a view or a copy.

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

Given a 2-D NumPy array arr, return the last two columns of every second row (rows 0, 2, 4, ...) using a single slicing expression: no Python loops or manual index building.

Requirements:

  • Rows: step 2 from index 0 . Columns: use negative indexing so the code works for any number of columns.
  • Use one combined indexing expression on arr .
  • Returning a view is acceptable.

Example:

arr = np.array([[ 1,  2,  3,  4],
                [ 5,  6,  7,  8],
                [ 9, 10, 11, 12],
                [13, 14, 15, 16],
                [17, 18, 19, 20]])

# expected result:
# array([[ 3,  4],
#        [11, 12],
#        [19, 20]])

What a Strong Answer Covers Guidance

  • Writes one indexing expression with a stepped slice on the rows and a negative-start slice on the columns
  • States the output shape for an array with n rows, for both odd and even n
  • Explains whether the result shares memory with arr and what that means for later writes
  • Covers unusual inputs: fewer than two columns, zero rows, and a 1-D array

Follow-up Questions Guidance

  • Is the result a view or a copy, and why does it matter?
  • How would you select every second row starting from the last row instead?
  • When would you need the result as an independent, contiguous array, and how would you get one?