iq.lab
Python starts when a code cell comes near or you run one
02 Counting steps: Big O

Checkpoint

Counting steps: Big O

Eleven questions from all three lessons, most of them on code you have not seen. Work each one on paper before you answer: count the passes, name the case, and price every line. The last question asks you to write code, so run its tests before you submit. You pass with 80%.

  1. Question 1 of 11.

    Type exactly what this prints. Count on paper before you check: the inner loop's range depends on i, and // rounds down.

    steps = 0
    for i in range(6):
        for j in range(i):
            steps += 1
    print(steps)
    
    size = 40
    halvings = 0
    while size > 1:
        size = size // 2
        halvings += 1
    print(halvings)
    
  2. Question 2 of 11.

    nums is a list of n numbers and seen is a set. Which of these take O(n) time? Choose every one that does.

    Choose every option that applies.

  3. Question 3 of 11.

    Plan A takes 1,000n steps on n items. Plan B takes n2/2 steps. At what n do the two plans take the same number of steps? Type a whole number.

    n

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

  4. Question 4 of 11.

    A teammate runs the every-pair duplicate check (the inner loop starts at i + 1) on a list of 1,000 ids. The first two ids are equal, so it returns True after one comparison. The teammate says: "One comparison, so it is O(1)." What is the right reply?

  5. Question 5 of 11.

    This function returns the events newest first. events has n items. What does it cost in time?

    def newest_first(events):
        result = []
        for e in events:
            result.insert(0, e)
        return result
  6. Question 6 of 11.

    A problem's constraints say:

    • 1 ≤ len(nums) ≤ 1,000
    • −109 ≤ nums[i] ≤ 109

    Your plan compares every pair of numbers once. Will it fit in about one second?

  7. Question 7 of 11.

    How many times does the loop body run? Type a whole number.

    n = 1000
    size = 1
    passes = 0
    while size < n:
        size = size * 2
        passes += 1
    passes

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

  8. Question 8 of 11.

    readings holds n sensor readings and thresholds holds m thresholds. For each threshold, the function counts the readings above it. What is its time cost?

    def counts_above(readings, thresholds):
        result = []
        for t in thresholds:
            above = 0
            for r in readings:
                if r > t:
                    above += 1
            result.append(above)
        return result
  9. Question 9 of 11.

    Use the append model from the lesson on Python costs: the list starts with room for 1 item, and whenever an append finds it full, every stored item is copied into a block twice as big. Over 100 appends, how many items are copied in total? Type a whole number.

    items copied

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

  10. Question 10 of 11.

    This function adds up a list with recursion. It moves an index instead of slicing, and it is called as total(nums, 0). For a list of n numbers, what are its time and extra space?

    def total(nums, i):
        if i == len(nums):
            return 0
        return nums[i] + total(nums, i + 1)
  11. Question 11 of 11.

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

    Code itViews from blocked users

    views is a list of user ids, one per page view, in time order. blocked is a list of blocked user ids. This function returns the views made by blocked users, in time order. It is correct but slow:

    def blocked_views(views, blocked):
        result = []
        for user in views:
            if user in blocked:
                result.append(user)
        return result

    Write blocked_views(views, blocked) so it runs in O(n + m) time, for n views and m blocked ids. Keep the result in time order. The last test gives it 30,000 views and 30,000 blocked ids with a 1 second limit, which the version above cannot meet: it takes several seconds.

    ⌘+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 11 answered. Unanswered questions count as wrong.