Question bank › Union Find
Dsa Union Find Medium

City Connection Mapping

You are working on a project to map connections between cities. The cities can establish direct connections with one another. You need to support two operations: add a connection between two cities and check if two cities are connected, either directly or indirectly. Write a program that processes a series of operations. Each operation is either 'C x y' to check if cities x and y are connected, or 'A x y' to add a connection between cities x and y. Input format: - The first line contains an integer n (1 ≤ n ≤ 10^5), the number of operations. - The next n lines contain either 'A' or 'C', followed by two integer values representing the cities (1 ≤ x, y ≤ 10^6). Output format: - For each 'C x y' operation, print 'YES' if the cities are connected, otherwise print 'NO'. Example: Input: 4 A 1 2 C 1 2 A 2 3 C 1 3 Output: YES YES

Key concepts

union_findgraphs

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.