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 NameType
symbolvarchar
typeenum ('Metal', 'Nonmetal', 'Noble')
electronsint

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
โšก METALS(Electron Donors)NaCaAl๐Ÿ”‹ NONMETALS(Electron Acceptors)ClOF๐Ÿ˜ด NOBLE GASES(Inert - No Bonds)HeNe๐Ÿ’ซ CHEMICAL BONDS FORMEDNa + Cl โ†’ Na-Cl Na + O โ†’ Na-O Na + F โ†’ Na-FCa + Cl โ†’ Ca-Cl Ca + O โ†’ Ca-O Ca + F โ†’ Ca-FAl + Cl โ†’ Al-Cl Al + O โ†’ Al-O Al + F โ†’ Al-FTotal: 9 possible ionic bondsEach metal can bond with each nonmetal (3 ร— 3 = 9)โœ— No bonds
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

n
2n
โœ“ Linear Growth
Space Complexity
O(m ร— n)

Result set contains exactly m ร— n valid bond combinations

n
2n
โšก 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
Asked in
Google 12 Amazon 8 Microsoft 6 Oracle 15
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen