lesson1practice1
Practice 1 — Conceptual
Q1. What is the key property that makes a function recursive?
A. It uses a for loop inside the function
B. It calls itself with a smaller or simpler subproblem
C. It always uses global variables
D. It must use return statements but no parameters
Answer choices: A | B | C | D
Practice 2 — Fill in the missing code (base case)
We want to write a recursive function sum_to_n(n) that returns
1 + 2 + 3 + ... + n using recursion.
def sum_to_nSort the array.
Use two pointers, i at the start and j at the end, moving them based on the sum.
- What is the time complexity of your program?
- What is the space complexity of your program?
Question 4
You are given an array of n integers. Write a program to count the number of triplets (i, j, k) such that arr[i] + arr[j] + arr[k] == 0.
- What is the time complexity of your program?
- What is the space complexity of your program?
Comments