Bird
Raised Fist0
Interview Prepdatabase-management-systemsmediumAmazonGoogleMicrosoftFlipkartSwiggyPhonePe

1NF to BCNF - Step-by-Step Normalization

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize Relation Schema

We start with the original relation R having attributes A, B, C, D and functional dependencies A->B, B->C, C->D. This is represented as a single node in the B-tree.

💡 Initialization sets the base schema before any normalization. Understanding the starting point is crucial to follow subsequent decompositions.
Line:relation = Relation(['A','B','C','D'], fds=[('A','B'), ('B','C'), ('C','D')])
💡 The entire schema is initially one node containing all attributes and dependencies.
📊
1NF to BCNF - Step-by-Step Normalization - Watch the Algorithm Execute, Step by Step
Watching this step-by-step visualization reveals how normalization systematically removes redundancy and anomalies by decomposing relations based on functional dependencies, which is hard to grasp from code or text alone.
Step 1/10
·Active fill★Answer cell
traverse
ABCD
leaf
Starting with full relation R(A,B,C,D) and its functional dependencies.
compareKey: A->B
ABCD
leaf
Checking FD A->B for BCNF violation; A is not a superkey.
splitKey: A->B↑ promoted: A
A
AB
leaf
ACD
leaf
Decomposed relation on FD A->B; promoted A to parent node.
compareKey: C->D
A
AB
leaf
ACD
leaf
Checking FD C->D in subrelation R2 for BCNF violation.
splitKey: C->D↑ promoted: C
A
AB
leaf
C
CD
leaf
AC
leaf
Decomposed R2 on FD C->D; promoted C to parent node.
compare
A
AB
leaf
C
CD
leaf
AC
leaf
Checked R4 for BCNF violations; none found.
compare
A
AB
leaf
C
CD
leaf
AC
leaf
Node 3 with key C is BCNF compliant after decomposition.
compare
A
AB
leaf
C
CD
leaf
AC
leaf
Checked R1 for BCNF violations; none found.
traverse
AC
AB
leaf
C
CD
leaf
Final BCNF schema with relations R1(A,B), R4(A,C), and R3(C,D).

Key Takeaways

✓ Normalization decomposes relations by isolating violating functional dependencies into separate relations.

This insight is hard to see from code alone because the iterative splitting and promotion of keys is abstract without visualization.

✓ Each decomposed relation must be checked independently for BCNF compliance, leading to recursive decomposition.

Understanding the recursive nature of normalization is easier when watching how each node is processed and split.

✓ The final BCNF schema is a tree of relations connected by keys, ensuring lossless join and dependency preservation.

Visualizing the final tree structure clarifies how normalization maintains data integrity across decomposed relations.

Practice

(1/5)
1. You need to optimize a database query that frequently performs exact-match lookups on a large, unordered dataset. Which index type is most appropriate to use?
easy
A. B+ tree index, because it supports efficient range queries.
B. Hash index, because it provides O(1) average-time equality lookups.
C. B+ tree index, because it provides faster equality lookups than hash indexes.
D. Hash index, because it supports ordered traversal for range queries.

Solution

  1. Step 1: Identify the query type

    The query performs exact-match lookups, which hash indexes optimize efficiently.
  2. Step 2: Understand hash index strengths

    Hash indexes provide average O(1) time for equality lookups but do not support ordered traversal.
  3. Step 3: Understand B+ tree strengths

    B+ trees support ordered traversal and range queries but equality lookups are O(log n), slower than hash indexes.
  4. Step 4: Evaluate distractors

    B+ tree index, because it supports efficient range queries. is incorrect because range queries are not the focus here. B+ tree index, because it provides faster equality lookups than hash indexes. wrongly claims B+ trees are faster for equality lookups. Hash index, because it supports ordered traversal for range queries. incorrectly states hash indexes support ordered traversal.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Exact-match queries -> hash index [OK]
Hint: Exact-match -> hash index; range -> B+ tree
Common Mistakes:
  • Confusing hash indexes as supporting range queries
  • Assuming B+ trees are always faster for equality lookups
  • Believing hash indexes maintain order
2. You have a sales dataset with multiple products across categories. You need to compute total sales per product, rank products within each category by sales, and filter categories based on total sales exceeding a threshold. Which approach guarantees an efficient and correct solution?
easy
A. Use a greedy algorithm to pick top-selling products without grouping or ranking.
B. Manually iterate over sales records in application code, aggregate totals, then sort and rank products.
C. Use SQL GROUP BY to aggregate sales, HAVING to filter categories, and window functions like RANK() to rank products within categories.
D. Use dynamic programming to compute cumulative sales and ranks iteratively.

Solution

  1. Step 1: Identify aggregation and filtering needs

    The problem requires grouping sales by product and category, filtering categories by total sales, and ranking products within categories.
  2. Step 2: Recognize SQL features that fit

    GROUP BY aggregates sales per product-category, HAVING filters aggregated categories, and window functions like RANK() rank products within partitions.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Window functions combined with GROUP BY and HAVING handle all requirements efficiently [OK]
Hint: Window functions + GROUP BY + HAVING solve aggregation and ranking [OK]
Common Mistakes:
  • Confusing greedy or DP approaches for aggregation tasks
3. What is the time complexity of the optimal SQL approach that uses DISTINCT salaries ordered descending with LIMIT and OFFSET to find the second highest salary, assuming n rows in the Employee table?
medium
A. O(n log n) due to sorting distinct salaries before applying LIMIT and OFFSET.
B. O(n^2) because DISTINCT requires comparing each salary with every other salary.
C. O(n) because we only scan the table once to find the max salary.
D. O(log n) because indexing on Salary allows binary search for the second highest.

Solution

  1. Step 1: Identify operations involved

    The query selects DISTINCT salaries and orders them descending, which requires sorting.
  2. Step 2: Analyze complexity of sorting distinct salaries

    Sorting distinct salaries takes O(m log m), where m ≤ n. In worst case, m = n, so O(n log n).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting dominates complexity, scanning alone is not enough [OK]
Hint: Sorting distinct salaries dominates time complexity [OK]
Common Mistakes:
  • Assuming O(n) because of single scan
  • Assuming DISTINCT is O(n^2) due to pairwise comparisons
4. What is the time complexity of performing a nested loop join between two tables with sizes n and m respectively, assuming no indexes and a simple equality join condition?
medium
A. O(n + m), since each table is scanned once
B. O(n * m), because each row in the first table is compared to every row in the second
C. O(n log m), assuming the second table is sorted
D. O(n), if a hash map is used for lookups

Solution

  1. Step 1: Analyze nested loops

    The outer loop runs n times, and for each iteration, the inner loop runs m times, leading to n * m comparisons.
  2. Step 2: Confirm no indexing or hashing

    Since no hash map or index is used, no faster lookup is possible, so complexity remains O(n * m).
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Nested loops multiply iteration counts [OK]
Hint: Nested loops multiply table sizes for complexity [OK]
Common Mistakes:
  • Assuming linear time due to scanning once
  • Confusing sorting with hashing benefits
5. If a database system needs to support very high-frequency insertions and deletions along with range queries, how does the B+ Tree structure help maintain performance, and what challenges might arise?
hard
A. B+ Tree stores data in internal nodes to reduce tree height, minimizing disk I/O during updates.
B. B+ Tree's linked leaf nodes allow efficient range queries, but frequent splits and merges can cause fragmentation and increased disk I/O.
C. B+ Tree avoids node splitting by always merging nodes during insertions and deletions, ensuring stable performance.
D. B+ Tree uses a fixed node size that prevents fragmentation, so performance remains constant regardless of update frequency.

Solution

  1. Step 1: Understand B+ Tree leaf linkage benefits

    Linked leaf nodes enable efficient sequential access for range queries.
  2. Step 2: Consider update overhead

    High-frequency insertions/deletions cause node splits and merges, which can fragment data and increase disk I/O.
  3. Step 3: Analyze incorrect options

    B+ Tree stores data in internal nodes to reduce tree height, minimizing disk I/O during updates. is false; B+ Tree stores data only in leaves. B+ Tree avoids node splitting by always merging nodes during insertions and deletions, ensuring stable performance. is incorrect because merges alone cannot avoid splits. B+ Tree uses a fixed node size that prevents fragmentation, so performance remains constant regardless of update frequency. is wrong; fixed node size does not prevent fragmentation from splits/merges.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    B+ Tree balances efficient range queries with update overhead due to structural changes.
Hint: Linked leaves -> fast range queries; splits/merges -> update overhead
Common Mistakes:
  • Assuming B+ Tree stores data in internal nodes
  • Believing merges prevent splits entirely
  • Thinking fixed node size eliminates fragmentation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard