CS FundamentalsVery hardProduct-company placement

For the function below, what is its cyclomatic complexity, and how many times is a[i] + a[j] == target evaluated for findpair([1, 2, 3, 4, 5], 9)?

For the function below, what is its cyclomatic complexity, and how many times is a[i] + a[j] == target evaluated for find_pair([1, 2, 3, 4, 5], 9)?
def find_pair(a, target):
    for i in range(len(a)):
        for j in range(i + 1, len(a)):
            if a[i] + a[j] == target:
                return (i, j)
    return None
  1. A. V(G) = 4; 10 evaluations
  2. B. V(G) = 3; 10 evaluations
  3. C. V(G) = 4; 20 evaluations
  4. D. V(G) = 4; 9 evaluations

Correct answer: A. V(G) = 4; 10 evaluations

Two loop headers and one if make 3 decisions, so V(G) = 4. Pairs are checked in order (0,1)…(0,4), (1,2)…(1,4), (2,3), (2,4), (3,4); the match 4 + 5 is the 10th check, the last pair.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic