Magicsheet logo

Symmetric Coordinates

Medium
100%
Updated 6/1/2025

Asked by 1 Company

Topics

Symmetric Coordinates

What is this problem about?

The Symmetric Coordinates interview question is a SQL-based challenge that focuses on identifying pairs of matching values in a table. You are typically given a table Functions with two columns, X and Y. Two pairs (X1, Y1) and (X2, Y2) are considered symmetric if X1 = Y2 and X2 = Y1. Your goal is to output all unique symmetric pairs (X, Y) where X <= Y, sorted by X.

Why is this asked in interviews?

This question is asked by companies like Mitsogo to test a candidate's SQL proficiency, specifically regarding self-joins and grouping. It evaluates whether you can handle cases where a pair might be its own symmetric partner (e.g., X=10, Y=10). It requires a combination of joining, filtering, and aggregation to ensure that you don't overcount or miss any valid pairs based on the problem's specific constraints.

Algorithmic pattern used

The primary pattern is the Database Self-Join and Filtering.

  1. You perform a self-join where f1.X = f2.Y and f1.Y = f2.X.
  2. To avoid double-counting and handle the X <= Y requirement, you add a filter f1.X <= f1.Y.
  3. To handle cases where X = Y, you need to check if that specific pair appears at least twice in the original table. This is often handled using a GROUP BY and HAVING COUNT(*) > 1 logic within the join or as a separate union.

Example explanation

Input:

  • (20, 20)
  • (20, 20)
  • (23, 25)
  • (25, 23) Symmetric pairs:
  • (20, 20) is symmetric with its second instance. (Valid)
  • (23, 25) is symmetric with (25, 23). (Valid, output as (23, 25)) Result:
  • (20, 20)
  • (23, 25) Note: If (20, 20) only appeared once, it wouldn't be a "pair" with another coordinate, so it wouldn't be symmetric.

Common mistakes candidates make

The most frequent mistake is not handling the X = Y case correctly. If you just join where f1.X = f2.Y and f1.Y = f2.X, a single row of (10, 10) will join with itself, but it doesn't have a distinct symmetric partner unless there is a second (10, 10) row. Another error is forgetting to sort the final output or failing to filter for X <= Y, leading to duplicate pairs in different orders (e.g., both (23, 25) and (25, 23)).

Interview preparation tip

To excel in the Symmetric Coordinates coding problem, practice writing CTEs or subqueries to isolate counts of each pair. Understanding the Database interview pattern for self-joins is essential. Also, make sure you are comfortable with conditional logic in SQL, as you often need to treat X < Y and X = Y cases differently.

Similar Questions