Interview Questions/Coding/Frequent Two Symbol Signal

Frequent Two Symbol Signal

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Frequent Two Symbol Signal

A signal monitor scans a stream and reports the most frequent adjacent two-character token. Tokens are formed from positions i and i+1, so overlapping tokens are counted. If several tokens have the same highest frequency, report the lexicographically smallest one. Process each stream independently. Input Format: The first line contains t, the number of test cases. Each test case contains n and a string s of length n. Output Format: For each test case, print the selected two-character token.

Examples

Example 1
Input:
1
7
ABABABA
Output:
AB

Explanation: AB appears three times and BA also appears three times. The tie is resolved by choosing AB.

Approach hint

Start with the simplest clear approach, explain the trade-off, then move toward the cleaner answer.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp