iq.lab
Python starts when a code cell comes near or you run one
03 Arrays and hash maps

Checkpoint

Arrays and hash maps

Twelve questions from the whole module, on inputs you have not met yet: list costs, buckets and keys, counting and seen-before, grouping keys, and prefix sums. Most take a minute with paper. The last one is a short program that joins two sum's dict to prefix sums. Work each one out before you check it: a wrong answer here shows you which lesson to reread.

  1. Question 1 of 12.

    Each line runs on a list nums of n items. Which one is O(n2)?

  2. Question 2 of 12.

    This function is meant to return the first character that appears a second time, or None if no character repeats. Type exactly what the two calls print.

    def first_repeat(s):
        seen = set()
        for ch in s:
            seen.add(ch)
            if ch in seen:
                return ch
        return None
    
    print(first_repeat("abcb"))
    print(first_repeat("xyz"))
    
  3. Question 3 of 12.

    A chained hash table like the lesson's holds small whole numbers, so hash(key) is the key itself. It has 8 buckets, and 27 sits in bucket 27 % 8 = 3. Then the table doubles to 16 buckets and places every key again. Which bucket holds 27 now? Type a whole number from 0 to 15.

    bucket

    Type a number: 12, -1, 0.5 and 3/4 all work.

  4. Question 4 of 12.

    prefix is built with a 0 in front, so prefix[i] is the sum of the first i items. Which expression is the sum of nums[3] through nums[6], both included?

  5. Question 5 of 12.

    You group lists of numbers in a dict of lists. Two lists belong together when they hold the same numbers, each the same number of times, in any order: [3, 1, 3] belongs with [3, 3, 1], but not with [1, 3, 1] or [1, 3]. Which key does that and can be stored as a dict key?

  6. Question 6 of 12.

    A list holds 9 items, at positions 0 to 8. nums.pop(2) removes the item at position 2 and keeps the list in order with no gaps. How many items move? A move is one item shifted from one slot to another.

    moves

    Type a number: 12, -1, 0.5 and 3/4 all work.

  7. Question 7 of 12.

    This is the one-walk two sum from the counting lesson, run on two lists with repeated numbers. Type exactly what it prints.

    def two_sum(nums, target):
        seen = {}
        for i, x in enumerate(nums):
            partner = target - x
            if partner in seen:
                return [seen[partner], i]
            seen[x] = i
    
    print(two_sum([3, 9, 5, 3], 6))
    print(two_sum([1, 4, 4], 8))
    
  8. Question 8 of 12.

    d is a dict and s is a set. Which of these lines raise a TypeError? Pick every one that does.

    Choose every option that applies.

  9. Question 9 of 12.

    The longest-run function from the grouping lesson runs on nums = [10, 4, 11, 5, 20, 6, 12]. It skips every x whose x - 1 is in the set. How many values of x start a run and measure it with the while loop?

    starts

    Type a number: 12, -1, 0.5 and 3/4 all work.

  10. Question 10 of 12.

    How many subarrays of [1, -1, 0] add up to 0? A subarray is one or more neighboring items. Count them by hand, then check your count with the prefix sum walk: a dict of running totals that starts as {0: 1}, looking up total - k before recording total.

    subarrays

    Type a number: 12, -1, 0.5 and 3/4 all work.

  11. Question 11 of 12.

    Type exactly what this prints. w[0] is the first letter of w.

    from collections import defaultdict
    
    groups = defaultdict(list)
    for w in ["ant", "bee", "art"]:
        groups[w[0]].append(w)
    print(len(groups))
    if groups["c"]:
        print("found c words")
    print(len(groups))
    print("z" in groups)
    
  12. Question 12 of 12.

    Run the tests before you submit. This question counts as correct if your latest run passes every check.

    Code itThe longest stretch that adds up to k

    Write longest_with_sum(nums, k), which returns the length of the longest subarray (one or more neighboring items) that adds up to exactly k, or 0 if none does. The numbers can be negative. For [2, 1, -1, 3, -2] with k = 2 the answer is 3: [2, 1, -1].

    Use one walk with a running total and a dict, as two sum does, but store each running total with the first position where it appeared. Then press Watch it run.

    ⌘+Enter runsPython starts when a code cell comes near or you run one
    Write your code where the starter says raise NotImplementedError, then press Run tests. Each check says what it expects.
0 of 12 answered. Unanswered questions count as wrong.