Skip to main content

Form a Chemical Bond - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min read
Practice this problem

Problem Statement

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.

Approach Overview

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.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Cross Join + FilterO(m × n)O(1)Best for generating every metal–nonmetal pair directly
Self Join with ConditionsO(m × n)O(1)Preferred when explicitly joining the same table with aliases

Video Solution

Leetcode 2480 - Form a Chemical Bond CROSS JOIN() - Solved & Explained by Everyday Data ScienceEveryday Data Science622 views views

Frequently Asked Questions

Is Form a Chemical Bond easy or hard?
Form a Chemical Bond is classified as an Easy database problem. It mainly tests understanding of SQL joins and filtering conditions rather than complex query logic or advanced database concepts.
Form a Chemical Bond Python/Java solution
The canonical solution is written in SQL (MySQL) because the problem is tagged as a Database question. Instead of Python or Java logic, the task is solved with a query that joins the Elements table to itself and filters for Metal and Nonmetal types.
How to solve Form a Chemical Bond in O(n)?
An O(n) solution is not typical because the task requires pairing metals with nonmetals, which naturally creates combinations. The SQL solution uses a join that effectively evaluates m × n pairs. Database engines optimize the join internally, but the conceptual complexity remains proportional to the number of generated pairs.
What is the best approach for Form a Chemical Bond?
The best approach is a self join or cross join on the Elements table with filters for element types. Select rows where one element is a Metal and the other is a Nonmetal. This directly generates all valid chemical bond pairs and keeps the query simple and readable.
Is Form a Chemical Bond asked at Google/Amazon/Meta?
This problem represents the type of SQL join question commonly asked in data engineering or analytics interviews at companies like Amazon, Google, and Meta. The focus is understanding joins, filtering conditions, and generating row combinations.
What data structure is used in Form a Chemical Bond?
The problem relies on relational database tables and SQL join operations rather than traditional in-memory data structures. The main concept is a self join that combines rows from the same table based on filtering conditions.
What is the time complexity of Form a Chemical Bond?
The time complexity is O(m × n), where m is the number of metals and n is the number of nonmetals. The join pairs every metal with every nonmetal. Space complexity is O(1) because the query does not require additional data structures beyond the result set.

Ready to solve this problem?

Practice Form a Chemical Bond with our built-in code editor and test cases.

Practice on FleetCode