iq.lab
Python starts when a code cell comes near or you run one
14 Interview mode

Checkpoint

Interview mode

Twelve questions that mix all five lessons: reading the signals, running the round, testing without a run button, building in parts, and taking hards apart. Every input is new, so work each one on paper before you answer: write the notebook after every step, and say out loud why each wrong option is wrong. The last two items are code: a teammate's function to test and fix, and a problem you have not seen, built from a pattern you have.

  1. Question 1 of 12.

    A delivery map has up to 104 intersections joined by roads. Each road takes a known number of minutes, from 1 to 60, and different roads take different times. Return the fewest minutes from the depot to a customer's intersection. Which plan fits?

  2. Question 2 of 12.

    A list holds up to 15 positive integers, each up to 109. Split the numbers into two groups so that the two group sums are as close as possible, and return the difference between them. Which plan does the size limit point to?

  3. Question 3 of 12.

    The interviewer says: "Here are a sensor's daily readings. Find the longest stretch of days where the reading never goes down." Which of these questions have answers that could change your code? Choose every one.

    Choose every option that applies.

  4. Question 4 of 12.

    Minute 6 of a round. The problem: the best profit from buying a stock on one day and selling it on a later day, given the daily prices. The candidate says:

    1. "I have a brute force: every buy day with every later sell day, O(n2). I'm looking for a way not to check every pair."
    2. "Let me try [7, 1, 5, 3] by hand. Buy at 1, sell at 5: a profit of 4."
    3. "Sorting is out, because the buy day has to come before the sell day."

    The interviewer asks: "What would you need to remember about the days you have passed?" The candidate says "Okay," deletes the nested loops and types a new single loop in silence.

    Which moves of the stuck recovery script did the candidate skip? Choose every one.

    Choose every option that applies.

  5. Question 5 of 12.

    This should return True when two different items of nums add up to target. It has one bug.

    def has_pair(nums, target):
        seen = set()
        for x in nums:
            seen.add(x)
            if target - x in seen:
                return True
        return False

    You have time to trace one test. Which one catches the bug?

  6. Question 6 of 12.

    Type exactly what this prints, one value per line.

    def first_over(nums, limit):
        for i in range(len(nums)):
            if nums[i] > limit:
                return i
    
    print(first_over([2, 9, 4], 3))
    print(first_over([2, 9, 4], 9))
    print(first_over([], 0))
    
  7. Question 7 of 12.

    The job load was queued at second 1, running at second 4, deleted at second 6 and queued again at second 10. This is the history and the search from the lesson. Type exactly what it prints.

    def last_at_or_before(times, timestamp):
        lo, hi = 0, len(times)
        while lo < hi:
            mid = (lo + hi) // 2
            if times[mid] > timestamp:
                hi = mid
            else:
                lo = mid + 1
        return lo - 1
    
    times = [1, 4, 6, 10]
    values = ["queued", "running", None, "queued"]
    
    def get(timestamp):
        i = last_at_or_before(times, timestamp)
        if i < 0 or values[i] is None:
            return ""
        return values[i]
    
    print([get(t) for t in [0, 4, 5, 6, 9, 10]])
    
  8. Question 8 of 12.

    This is the store from the lesson with nested transactions. Each begin pushes an empty undo record; set saves a key's old value in the top record before the key's first change there; an inner commit merges its record into the one below, keeping the outer entry when both have the key; rollback pops the top record and puts its old values back.

    set("y", 4)
    begin()        # outer
    begin()        # inner
    set("y", 6)
    commit()       # inner
    set("y", 8)
    rollback()     # outer

    What does get("y") return after the outer rollback?

    get("y")

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

  9. Question 9 of 12.

    Bars of heights [2, 0, 3, 1, 0, 4, 1] stand side by side, each 1 unit wide. Water that reaches either end runs off. How many units of water stay on the row? Use the rule for one bar: the lower of its two walls, minus its height.

    units of water

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

  10. Question 10 of 12.

    This is the first loop of first missing positive, with a counter for the swaps, run on a list with a repeat and a value too large to have a seat. Then a second loop reads the seats. Type exactly what it prints.

    def seat_values(nums):
        n = len(nums)
        swaps = 0
        for i in range(n):
            while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
                seat = nums[i] - 1
                nums[i], nums[seat] = nums[seat], nums[i]
                swaps += 1
        return swaps
    
    nums = [3, 4, 2, 2, 7]
    swaps = seat_values(nums)
    print(nums, swaps)
    for k in range(1, len(nums) + 1):
        if nums[k - 1] != k:
            print(k)
            break
    
  11. Question 11 of 12.

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

    Code itTest it, then fix it

    A teammate wrote second_largest(nums): the second largest distinct value in the list, or None if there is none. It passes their one test ([3, 7, 5] gives 5). Before you press Run, trace it on paper on the edge cases from the lesson's table: all negative, a repeated largest value, all equal, one item, empty. Then fix it so every test passes, and press Watch it run on the repeated largest value.

    ⌘+Enter runsPython starts when a code cell comes near or you run one
    Press Run tests to run your code and the checks.
  12. Question 12 of 12.

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

    Code itEvery missing value

    nums holds n numbers, each between 1 and n, and some values repeat, so some values from 1 to n are missing. Write missing_values(nums), which returns the missing values in increasing order. Use the seats from the lesson: send each value v to seat v - 1, then read which seats do not hold their own value. You may rearrange nums. The last test has 40,000 numbers, so scanning the list once for every value is too slow. 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.