Code Room
CodingHardcod-g180
Subject Gcd lcmLevel Senior–Staff~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an integer n (1 <= n <= 100000), count the number of unordered pairs (i, j) with 1 <= i < j <= n such that gcd(i, j) == 1 (i and j are coprime). For example, for n = 5 there are 9 coprime pairs. A naive O(n^2) gcd over all pairs is too slow at the upper bound.

Implement
count_coprime_pairs(n: int) → int
Examples
in[5]out9
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.