0
0
Intro to Computingfundamentals~10 mins

Algorithm efficiency basics (fast vs slow) in Intro to Computing - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sum of numbers from 1 to n efficiently in O(1) time.

Intro to Computing
def sum_to_n(n):
    return n * (n [1] 1) // 2

print(sum_to_n(5))
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Confusing the operators in the formula.
2fill in blank
medium

Complete the O(n) code to find the largest number in the list (linear scan).

Intro to Computing
numbers = [3, 7, 2, 9, 5]
max_num = numbers[0]
for num in numbers:
    if num [1] max_num:
        max_num = num
print(max_num)
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' finds minimum instead.
Using '==' doesn't update properly.
3fill in blank
hard

Complete the slow O(n^2) nested loop code to check for duplicates.

Intro to Computing
def has_duplicate(nums):
    for i in range(len(nums)):
        for j in range([1], len(nums)):
            if nums[i] == nums[j]:
                return True
    return False

print(has_duplicate([1,2,2,3]))
Drag options to blanks, or click blank then click option'
Ai
Bi+1
C0
Dlen(nums)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 causes redundant checks.
Using i includes self-comparison.
4fill in blank
hard

Complete the fast O(n) code using a set to check for duplicates.

Intro to Computing
def has_duplicate_fast(nums):
    seen = set()
    for num in nums:
        if num [1] seen:
            return True
        seen.[2](num)
    return False

print(has_duplicate_fast([1,2,2,3]))
Drag options to blanks, or click blank then click option'
Ain
Badd
C>
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operator or method.
Forgetting to add after check.
5fill in blank
hard

Fill blanks for O(n) dict comprehension to map words to lengths (efficient single pass).

Intro to Computing
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<=
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong filter includes short words.
Using word as value instead of length.