← Introduction to Computer Science

Unit 8: Arrays, Lists, and Strings

Contiguous storage, zero-based indexing, and the sequence type every other data structure gets compared against.

7 topics · 22 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Arrays and Zero-Based Indexing

  1. How it worksHow does an array lay its elements out back to back so any index can be reached in one calculation?
  2. Worked exampleTrace a = [10, 20, 30, 40] and find a[0], a[2], and a[3].
  3. Common mistakeWhy isn't the last legal index of an array the same number as its length?
Your turn1 of 2
An array holds 697 elements, indexed starting at 0. What is the index of its last element?

List Operations and Their Cost

  1. How it worksHow does inserting at the front of a list force every other element to shift over by one spot?
  2. Worked exampleTrace lst = [1, 2, 3] after append(4), then insert(0, 0), then remove(2).
  3. Going furtherWhat happens to the cost of an insert once the list holds a million elements instead of ten?
Your turn1 of 2
Inserting at the front of a list forces every one of its 696417 existing elements to shift over by one spot. How many elements need to shift for this single front insertion?

Two-Dimensional Arrays and Grids

  1. How it worksHow does a grid of rows and columns sit inside a list of lists, one row per outer element?
  2. Worked exampleTrace grid[1][2] for grid = [[1,2,3],[4,5,6],[7,8,9]].
  3. Common mistakeWhy isn't grid[1][2] the same cell as grid[2][1]?
Your turn1 of 2
A grid has 35 rows and 33 columns, stored as a list of lists. How many total cells does the grid contain?

Strings as Sequences of Characters

  1. How it worksHow does slicing a string with a start and stop index pull out a substring without touching the original?
  2. Worked exampleTrace word = 'python' and find word[0], word[-1], and word[1:4].
  3. Going furtherWhat happens to a slice's result when the stop index reaches past the end of the string?
Your turn1 of 2
A string has 349 characters, indexed starting at 0. What is the index of its last character?

String Immutability

  1. How it worksHow does every string method that looks like it edits a string actually build and return a brand new one?
  2. Worked exampleTrace s = 'hello' then t = s.upper() and find whether s itself changed.
  3. Common mistakeWhy doesn't calling a method like replace on a string change the original string in place?
  4. Ties togetherHow does string immutability connect to the same reason a tuple can't be changed after creation?
Your turn1 of 2
A string s of 35 characters has a method called on it that returns a brand new string of 33 characters, leaving s unchanged. How many total characters exist between s and the new string, added together?

Efficient String Building

  1. How it worksHow does each += on a string inside a loop copy the entire string built so far into a brand new one?
  2. Worked exampleTrace the total characters copied when building a 5-character string one letter at a time with +=.
  3. Going furtherWhat changes in performance once the loop collects pieces in a list and joins them only once at the end?
Your turn1 of 2
A loop builds a string one character at a time using +=, and each += copies the entire string built so far. For a final string of 350 characters, roughly how many total characters get copied across the whole loop?

Out-of-Bounds Access and Off-by-One on Arrays

  1. How it worksHow does an index equal to or past an array's length reach past the memory the array actually owns?
  2. Worked exampleTrace a loop from i=0 to i=len(a) inclusive over a=[1,2,3] and find exactly where it fails.
  3. Common mistakeWhy doesn't an array automatically grow just because you tried to index one past its end?
Your turn1 of 2
An array a has 697 elements, with valid indices 0 through n minus 1. A loop runs from i=0 through i=697 inclusive, one too many passes. At which index i does the loop first fail?