Form a Chemical Bond - Problem
๐งช Chemical Bonding Lab Challenge
You're working in a chemistry lab and need to determine which elements can form ionic bonds! In chemistry, ionic bonds form when electrons are transferred between atoms - specifically when a Metal (which donates electrons) pairs with a Nonmetal (which accepts electrons).
Given a database table Elements containing information about different chemical elements:
| Column Name | Type |
|---|---|
| symbol | varchar |
| type | enum ('Metal', 'Nonmetal', 'Noble') |
| electrons | int |
Rules:
- ๐ฅ Noble gases are chemically inert (electrons = 0) and don't form bonds
- โก Metals can give electrons (positive electron value)
- ๐ Nonmetals can accept electrons (positive electron value represents need)
- ๐ซ Bond formation: Only Metal + Nonmetal pairs can form ionic bonds
Your task is to find all possible element pairs that can form chemical bonds and return them in a result table.
Input & Output
example_1.sql โ Basic Elements
$
Input:
Elements table:
| symbol | type | electrons |
|--------|----------|----------|
| Na | Metal | 1 |
| Cl | Nonmetal | 1 |
| He | Noble | 0 |
โบ
Output:
| metal | nonmetal |
|-------|----------|
| Na | Cl |
๐ก Note:
Na (Metal) can form a bond with Cl (Nonmetal). He (Noble gas) cannot form bonds, so it's not included in any pairs.
example_2.sql โ Multiple Elements
$
Input:
Elements table:
| symbol | type | electrons |
|--------|----------|----------|
| Li | Metal | 1 |
| Na | Metal | 1 |
| F | Nonmetal | 1 |
| Cl | Nonmetal | 1 |
| Ar | Noble | 0 |
โบ
Output:
| metal | nonmetal |
|-------|----------|
| Li | Cl |
| Li | F |
| Na | Cl |
| Na | F |
๐ก Note:
Each metal (Li, Na) can bond with each nonmetal (F, Cl), creating 4 possible bonds. Ar (Noble) forms no bonds.
example_3.sql โ Edge Case
$
Input:
Elements table:
| symbol | type | electrons |
|--------|----------|----------|
| He | Noble | 0 |
| Ne | Noble | 0 |
โบ
Output:
| metal | nonmetal |
|-------|----------|
๐ก Note:
No bonds possible since there are only Noble gases, which are chemically inert and don't form ionic bonds.
Visualization
Tap to expand
Understanding the Visualization
1
Separate by Type
Group elements into Metals (electron donors), Nonmetals (electron acceptors), and Noble gases (inert)
2
Apply Bonding Rules
Only Metals and Nonmetals can form ionic bonds through electron transfer
3
Generate All Combinations
Create every possible Metal-Nonmetal pair using cross join
4
Output Bond Pairs
Return the complete list of elements that can form chemical bonds
Key Takeaway
๐ฏ Key Insight: Ionic bonds form through electron transfer between metals (which lose electrons) and nonmetals (which gain electrons). The optimal SQL solution filters elements by type first, then uses a cross join to efficiently generate all valid Metal-Nonmetal combinations.
Time & Space Complexity
Time Complexity
O(m ร n)
Where m is number of metals and n is number of nonmetals
โ Linear Growth
Space Complexity
O(m ร n)
Result set contains exactly m ร n valid bond combinations
โก Linearithmic Space
Constraints
- 1 โค number of elements โค 1000
- Element symbols are unique and 1-2 characters long
- type is one of: 'Metal', 'Nonmetal', 'Noble'
- electrons = 0 if and only if type = 'Noble'
- electrons โฅ 0 for all elements
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code