Table: Elements
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| symbol | varchar |
| type | enum |
| electrons | int |
+-------------+---------+
symbol is the primary key (column with unique values) for this table.
Each row of this table contains information of one element.
type is an ENUM (category) of type ('Metal', 'Nonmetal', 'Noble')
- If type is Noble, electrons is 0.
- If type is Metal, electrons is the number of electrons that one atom of this element can give.
- If type is Nonmetal, electrons is the number of electrons that one atom of this element needs.
Two elements can form a bond if one of them is 'Metal' and the other is 'Nonmetal'.
Write a solution to find all the pairs of elements that can form a bond.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Elements table: +--------+----------+-----------+ | symbol | type | electrons | +--------+----------+-----------+ | He | Noble | 0 | | Na | Metal | 1 | | Ca | Metal | 2 | | La | Metal | 3 | | Cl | Nonmetal | 1 | | O | Nonmetal | 2 | | N | Nonmetal | 3 | +--------+----------+-----------+ Output: +-------+----------+ | metal | nonmetal | +-------+----------+ | La | Cl | | Ca | Cl | | Na | Cl | | La | O | | Ca | O | | Na | O | | La | N | | Ca | N | | Na | N | +-------+----------+ Explanation: Metal elements are La, Ca, and Na. Nonmeal elements are Cl, O, and N. Each Metal element pairs with a Nonmetal element in the output table.
Problem Overview: The table Elements stores chemical elements and their types (such as Metal or Nonmetal). A chemical bond forms when a metal pairs with a nonmetal. Your task is to return every valid metal–nonmetal pair.
Approach 1: Cross Join with Type Filtering (O(m × n) time, O(1) space)
The simplest way to generate all valid bonds is to create combinations of elements and then filter them by type. Use a self join (or CROSS JOIN) on the Elements table to pair every row with every other row. After generating these combinations, apply a WHERE condition to keep only rows where one element is a metal and the other is a nonmetal.
The key insight: ionic bonds form specifically between metals and nonmetals. Instead of checking every possible rule, restrict the result set with two filters: e1.type = 'Metal' and e2.type = 'Nonmetal'. This ensures that the first column always represents the metal and the second column the nonmetal. Since the query pairs each metal with every nonmetal, the database effectively computes m × n combinations, where m is the number of metals and n is the number of nonmetals.
This approach works well because the dataset in interview-style SQL problems is typically small. The database engine handles the join efficiently, and the query remains extremely readable. The logic maps directly to the problem statement: list all metals, list all nonmetals, and combine them.
Conceptually, this problem is a straightforward application of SQL joins and filtering. It also demonstrates how a cross join can generate all pair combinations before applying constraints. Understanding this pattern helps with many database interview questions where relationships must be formed between rows in the same table.
Recommended for interviews: The self-join or cross-join filtering approach is exactly what interviewers expect. It shows you understand how to combine rows within a table and apply precise filtering conditions. There is no meaningful brute force alternative in SQL—the join itself expresses the intended logic clearly and efficiently.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Cross Join + Filter | O(m × n) | O(1) | Best for generating every metal–nonmetal pair directly |
| Self Join with Conditions | O(m × n) | O(1) | Preferred when explicitly joining the same table with aliases |
Leetcode 2480 - Form a Chemical Bond CROSS JOIN() - Solved & Explained by Everyday Data Science • Everyday Data Science • 578 views views
Watch 1 more video solutions →Practice Form a Chemical Bond with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor