Bird
Raised Fist0
NumPydata~20 mins

Views share memory with originals in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in NumPy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of modifying a NumPy view
What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4])
view = arr[1:3]
view[0] = 10
print(arr)
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
view = arr[1:3]
view[0] = 10
print(arr)
A[1 2 3 4]
B[1 10 3 4]
C[10 2 3 4]
D[1 10 10 4]
Attempts:
2 left
💡 Hint
Remember that views share memory with the original array.
❓ data_output
intermediate
1:30remaining
Memory sharing check with np.shares_memory
Given the code below, what does the print statement output?
import numpy as np
x = np.arange(5)
y = x[2:]
print(np.shares_memory(x, y))
NumPy
import numpy as np
x = np.arange(5)
y = x[2:]
print(np.shares_memory(x, y))
AFalse
BNone
CTrue
DRaises an error
Attempts:
2 left
💡 Hint
Slices usually share memory with the original array.
🔧 Debug
advanced
2:00remaining
Why does modifying a copy not affect the original?
Consider this code:
import numpy as np
arr = np.array([5, 6, 7, 8])
copied = arr.copy()
copied[0] = 100
print(arr)

Why does the original array not change?
NumPy
import numpy as np
arr = np.array([5, 6, 7, 8])
copied = arr.copy()
copied[0] = 100
print(arr)
ABecause modifying copied modifies arr internally
BBecause copied is a view sharing memory with arr
CBecause arr is immutable
DBecause copied is a separate copy with its own memory
Attempts:
2 left
💡 Hint
Think about what .copy() does to the data.
🚀 Application
advanced
2:30remaining
Detecting if two arrays share memory
You have two arrays:
import numpy as np
base = np.array([1, 2, 3, 4, 5])
arr1 = base[1:4]
arr2 = base.copy()[1:4]

Which code snippet correctly detects which arrays share memory with base?
NumPy
import numpy as np
base = np.array([1, 2, 3, 4, 5])
arr1 = base[1:4]
arr2 = base.copy()[1:4]
Aprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # True False
Bprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # False False
Cprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # False True
Dprint(np.shares_memory(base, arr1), np.shares_memory(base, arr2)) # True True
Attempts:
2 left
💡 Hint
Remember that slicing creates views but copy() creates independent arrays.
🧠 Conceptual
expert
3:00remaining
Effect of modifying a view on a reshaped array
Given the code below, what is the output?
import numpy as np
original = np.arange(6)
reshaped = original.reshape(2, 3)
view = reshaped[:, 1]
view[0] = 99
print(original)
NumPy
import numpy as np
original = np.arange(6)
reshaped = original.reshape(2, 3)
view = reshaped[:, 1]
view[0] = 99
print(original)
A[0 99 2 3 4 5]
B[0 1 2 3 4 5]
C[0 1 99 3 4 5]
D[99 1 2 3 4 5]
Attempts:
2 left
💡 Hint
Think about how reshaping and slicing create views sharing memory.

Practice

(1/5)
1. What happens when you modify a NumPy array view?
easy
A. An error occurs because views are read-only
B. Only the view changes, original stays same
C. A new copy of the array is created
D. The original array also changes

Solution

  1. Step 1: Understand what a view is

    A view is a way to look at the same data without copying it.
  2. Step 2: Effect of modifying a view

    Since the view shares memory with the original, changing the view changes the original array too.
  3. Final Answer:

    The original array also changes -> Option D
  4. Quick Check:

    Views share memory = original changes [OK]
Hint: Views share memory; changing view changes original [OK]
Common Mistakes:
  • Thinking views create copies
  • Assuming views are independent
  • Believing views are read-only
2. Which of the following is the explicit way to create a view of a NumPy array a?
easy
A. b = a.view()
B. b = a.copy()
C. b = a[:]
D. b = np.array(a)

Solution

  1. Step 1: Identify methods to create views

    The view() method explicitly creates a view sharing memory with the original array.
  2. Step 2: Check other options

    copy() and np.array() create copies, not views. a[:] creates a view only if a is an ndarray, but view() is the explicit and recommended method.
  3. Final Answer:

    b = a.view() -> Option A
  4. Quick Check:

    Use view() for views [OK]
Hint: Use .view() method to create a view [OK]
Common Mistakes:
  • Using copy() instead of view()
  • Assuming slicing always creates a copy
  • Using np.array() which copies data
3. What is the output of this code?
import numpy as np
x = np.array([1, 2, 3, 4])
y = x[1:3]
y[0] = 10
print(x)
medium
A. [1 2 3 4]
B. [10 2 3 4]
C. [1 10 3 4]
D. [1 2 10 4]

Solution

  1. Step 1: Understand slicing creates a view

    The slice x[1:3] creates a view of elements at index 1 and 2.
  2. Step 2: Modify the view and check original

    Changing y[0] changes x[1] because they share memory. So x becomes [1, 10, 3, 4].
  3. Final Answer:

    [1 10 3 4] -> Option C
  4. Quick Check:

    Slices are views = original changes [OK]
Hint: Slicing creates views; changes reflect in original [OK]
Common Mistakes:
  • Thinking slicing creates a copy
  • Modifying view does not affect original
  • Confusing indices in slice
4. Find the error in this code that tries to create a view and modify it:
import numpy as np
arr = np.array([5, 6, 7, 8])
view_arr = arr.copy()
view_arr[1] = 100
print(arr)
medium
A. copy() creates a view, so arr changes
B. copy() creates a copy, so arr does not change
C. Syntax error in creating view
D. view_arr is read-only, cannot assign

Solution

  1. Step 1: Understand copy() behavior

    copy() creates a new array with its own memory, not a view.
  2. Step 2: Effect of modifying copy

    Changing view_arr[1] does not affect arr because they are separate arrays.
  3. Final Answer:

    copy() creates a copy, so arr does not change -> Option B
  4. Quick Check:

    copy() makes independent array [OK]
Hint: copy() makes independent array, not view [OK]
Common Mistakes:
  • Assuming copy() creates a view
  • Expecting original to change after copy modification
  • Confusing copy() and view()
5. You have a large NumPy array data. You want to modify a small part without copying the whole array to save memory. Which approach is best?
hard
A. Use slicing or view() to get a view and modify it
B. Convert array to list, modify, then back to array
C. Use data.copy() and modify the copy
D. Use np.array(data) to create a new array and modify

Solution

  1. Step 1: Understand memory use in copying vs views

    Copying creates a full new array, using more memory. Views share memory and are efficient.
  2. Step 2: Choose method to modify part without full copy

    Slicing or view() creates a view sharing memory, so modifying it changes original without extra memory.
  3. Final Answer:

    Use slicing or view() to get a view and modify it -> Option A
  4. Quick Check:

    Views save memory and allow modification [OK]
Hint: Use views to modify without copying large arrays [OK]
Common Mistakes:
  • Using copy() wastes memory
  • Converting to list is slow and memory heavy
  • Creating new arrays duplicates data unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard