Count reverse-complement DNA strand pairs
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests hashing, strings and counting.
You are given a list of DNA strands. Each strand is a non-empty string over the alphabet A, C, G, T.
The reverse complement of a strand is formed by reversing it and then replacing each base with its partner: A with T, T with A, C with G, and G with C. For example, the reverse complement of AACG is CGTT.
Two strands at indices i < j form a special pair when strands[j] is the reverse complement of strands[i]. Because taking the reverse complement twice gives back the original strand, the relation is symmetric.
Implement:
-
count_special_pairs(strands: list[str]) -> int: the number of special pairs. -
list_special_pairs(strands: list[str]) -> list[tuple[int, int]]: every special pair(i, j)withi < j, sorted.
Raise ValueError if a strand is empty or contains a character outside ACGT.
Constraints
0 <= len(strands) <= 10^5- The total length of all strands is at most
10^6. - Strands may repeat. Every copy counts as a separate index.
Example 1
strands = ["AACG", "CGTT", "GGA", "TCC", "CGTT"]
count_special_pairs(strands) -> 3
list_special_pairs(strands) -> [(0, 1), (0, 4), (2, 3)]
CGTT is the reverse complement of AACG (twice), and TCC is the reverse complement of GGA.
Example 2
strands = ["ACGT", "ACGT", "AC", "CA"]
count_special_pairs(strands) -> 1
ACGT is its own reverse complement, so its two copies form one pair. AC and CA are only reverses of each other, so they do not form a pair (the reverse complement of AC is GT).
Clarifying questions
- Is lowercase input invalid, or should it be normalised?
- Can the list of pairs be huge (many copies of one strand and its complement)? Its size can reach about n^2 / 4.
- Is
ValueErrorexpected even if the bad strand never takes part in a pair?
What a strong answer covers
- Validates every strand first, then computes each reverse complement with a translation table in O(length).
- Counts in one left-to-right pass: before adding strand
jto a counter, adds the count of its reverse complement seen so far, which handles self-complementary strands (ACGT) and repeats in O(total length). - Lists pairs with a map from strand to the indices seen so far, emitting
(i, j)for each earlier index of the complement, then sorts; notes the output can be quadratic in size. - Distinguishes reverse from reverse complement (
ACandCAare not a pair). - Tests both examples, an empty list, self-complementary repeats, and invalid characters.
Follow-ups
- Also count pairs where one strand is the reverse complement of a substring of the other.
- Allow up to one mismatch in a pair.
- Process a stream of strands too large for memory (hash the canonical form, external sort).
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
