Form a Chemical Bond - Problem

You are given a table Elements that contains information about chemical elements.

The table has the following structure:

  • symbol (varchar): Primary key representing the element symbol
  • type (enum): The type of element - 'Metal', 'Nonmetal', or 'Noble'
  • electrons (int): Number of electrons the element can give or needs

Rules:

  • If type is 'Noble', electrons is 0
  • If type is 'Metal', electrons is the number of electrons that one atom can give
  • If type is 'Nonmetal', electrons is the number of electrons that one atom needs

Chemical Bond Formation: Two elements can form a bond if one is 'Metal' and the other is 'Nonmetal'.

Write a SQL query to find all pairs of elements that can form a chemical bond. Return the result in any order.

Table Schema

Elements
Column Name Type Description
symbol PK varchar Element symbol (primary key)
type enum Element type: 'Metal', 'Nonmetal', or 'Noble'
electrons int Electrons given (Metal) or needed (Nonmetal), 0 for Noble
Primary Key: symbol
Note: Each row represents one chemical element with bonding properties

Input & Output

Example 1 — Basic Chemical Bonding
Input Table:
symbol type electrons
Na Metal 1
Cl Nonmetal 1
He Noble 0
Output:
metal nonmetal
Na Cl
💡 Note:

Sodium (Na) is a metal that can give 1 electron, and Chlorine (Cl) is a nonmetal that needs 1 electron. They can form an ionic bond. Helium (He) is a noble gas and cannot form bonds with other elements.

Example 2 — Multiple Bonding Possibilities
Input Table:
symbol type electrons
Li Metal 1
Ca Metal 2
F Nonmetal 1
O Nonmetal 2
Output:
metal nonmetal
Ca F
Ca O
Li F
Li O
💡 Note:

All metals (Li, Ca) can form bonds with all nonmetals (F, O), regardless of their electron counts. The result shows all possible Metal-Nonmetal pairs sorted alphabetically.

Example 3 — No Bonding Possible
Input Table:
symbol type electrons
Ne Noble 0
Ar Noble 0
Output:
metal nonmetal
💡 Note:

Only noble gases are present in the table. Since bonds can only form between metals and nonmetals, no chemical bonds are possible, resulting in an empty result set.

Constraints

  • 1 ≤ number of elements ≤ 100
  • type is one of 'Metal', 'Nonmetal', or 'Noble'
  • 0 ≤ electrons ≤ 8
  • symbol consists of 1-2 uppercase letters

Visualization

Tap to expand
Form a Chemical Bond INPUT Symbol Type e- Na Metal 1 Cl Nonmetal 7 Fe Metal 2 O Nonmetal 6 K Metal 1 Element Types: Na Fe K Metals Cl O Nonmetals ALGORITHM STEPS 1 Separate by Type Group metals and nonmetals 2 Cross Join Pair each metal with nonmetal 3 Filter Valid Bonds Metal + Nonmetal = Bond 4 Return Pairs Output all valid combinations SELECT m.symbol, n.symbol FROM Elements m CROSS JOIN Elements n WHERE m.type = 'Metal' AND n.type = 'Nonmetal' M N = Bond FINAL RESULT Metal Nonmetal Na Cl Na O Fe Cl Fe O K Cl K O 6 Valid Bonds Found Ionic Compounds: Na Cl NaCl Fe O FeO K O KO OK - All pairs valid Key Insight: Chemical bonds form when metals (electron donors) pair with nonmetals (electron acceptors). Use CROSS JOIN to generate all combinations, then WHERE clause filters for valid Metal-Nonmetal pairs. Time Complexity: O(n x m) where n = metals count, m = nonmetals count. TutorialsPoint - Form a Chemical Bond | Optimal Solution
Asked in
Google 15 Microsoft 12
23.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