CS FundamentalsVery hardProduct-company placement · Amazon-style
For the function below, what are its cyclomatic complexity and the number of times a[j] > a[j + 1] is evaluated for the input [2, 1, 3, 4, 5, 6]?
For the function below, what are its cyclomatic complexity and the number of times a[j] > a[j + 1] is evaluated for the input [2, 1, 3, 4, 5, 6]?
def bubble(a):
n = len(a)
for i in range(n - 1):
swapped = False
for j in range(n - 1 - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
swapped = True
if not swapped:
break
return a- A. V(G) = 4, 15 comparisons
- B. V(G) = 5, 5 comparisons
- C. V(G) = 5, 9 comparisons
- D. V(G) = 4, 9 comparisons
Correct answer: C. V(G) = 5, 9 comparisons
Decisions: two for loops and two ifs, so V(G) = 5. Pass 1 makes 5 comparisons and one swap; pass 2 makes 4 comparisons with no swap and breaks, giving 9. 15 is the no-early-exit count; 5 assumes an already sorted list.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- Assertion (A): A suite with 100% branch coverage can still miss a defect where a required check was never coded. Reason (R): White-box…
- How many of these are static testing activities: (1) a peer code review, (2) running a linter such as ESLint, (3) executing unit tests…
- Assertion (A): A Fagan inspection is more formal than a walkthrough. Reason (R): An inspection is led by a trained moderator, follows…
- An integer quantity field accepts 1 to 100. The discount is 0% for 1-10, 10% for 11-50 and 20% for 51-100. Using two-value boundary value…
- A mutant changes if (x > 10 && x < 20) to if (x >= 10 && x < 20). Which single test input kills (distinguishes) this mutant?
- After a change to functions f1, f3 and f5, a team wants the smallest regression subset that exercises all three. Tests cover: T1 {f1, f2}…