DRY stands for "Don't Repeat Yourself". It means avoiding duplication of code or logic to make systems easier to maintain and less error-prone.
Click to reveal answer
beginner
Why is following the DRY principle important in system design?
Following DRY helps reduce bugs, makes code easier to update, and improves clarity by having a single source of truth for any piece of logic or data.
Click to reveal answer
beginner
Give a real-life example that explains the DRY principle.
Imagine writing a recipe once and sharing it with friends instead of writing the same recipe multiple times. If you want to change the recipe, you only update it once, and everyone gets the update.
Click to reveal answer
intermediate
How can DRY be applied in low-level design (LLD)?
In LLD, DRY can be applied by creating reusable functions, classes, or modules that handle common tasks instead of repeating similar code in multiple places.
Click to reveal answer
intermediate
What problems can occur if DRY is not followed?
If DRY is ignored, code duplication can cause inconsistent behavior, harder maintenance, more bugs, and wasted effort when updating multiple copies of the same logic.
Click to reveal answer
What is the main goal of the DRY principle?
AMake code longer for clarity
BWrite code as fast as possible
CUse many different programming languages
DAvoid repeating code or logic
✗ Incorrect
DRY means "Don't Repeat Yourself", so the main goal is to avoid repeating code or logic.
Which of these is a benefit of following DRY?
AMore bugs due to repeated code
BMore complex code structure
CEasier maintenance and updates
DSlower system performance
✗ Incorrect
DRY reduces duplication, making maintenance and updates easier.
In low-level design, how can DRY be implemented?
ABy copying code blocks multiple times
BBy creating reusable functions or classes
CBy writing everything in one big function
DBy avoiding comments
✗ Incorrect
Reusable functions or classes help avoid code repetition, following DRY.
What can happen if you do NOT follow DRY?
AInconsistent behavior and more bugs
BYou save time updating code
CCode becomes easier to maintain
DCode runs faster
✗ Incorrect
Ignoring DRY leads to duplicated code, causing inconsistent behavior and more bugs.
Which analogy best explains DRY?
AWriting the same recipe once and sharing it
BWriting different recipes for every meal
CCopying recipes by hand multiple times
DThrowing away old recipes
✗ Incorrect
Sharing one recipe instead of rewriting it multiple times illustrates DRY.
Explain the DRY principle and why it is important in system design.
Think about how repeating code can cause problems.
You got /3 concepts.
Describe how you would apply the DRY principle when designing a system at a low level.
Consider how to organize code to avoid copying the same logic.
You got /3 concepts.
Practice
(1/5)
1. What is the main goal of the DRY principle in system design?
easy
A. To avoid repeating code or design to keep systems simple
B. To write as much code as possible for clarity
C. To duplicate modules for faster development
D. To create multiple versions of the same component
Solution
Step 1: Understand the meaning of DRY
DRY stands for "Don't Repeat Yourself," which means avoiding repeated code or design.
Step 2: Identify the goal of DRY
The goal is to keep systems simple, maintainable, and reduce bugs by reusing components.
Final Answer:
To avoid repeating code or design to keep systems simple -> Option A
Quick Check:
DRY = Avoid repetition [OK]
Hint: DRY means write once, use many times [OK]
Common Mistakes:
Thinking DRY means writing more code
Believing duplication speeds development
Confusing DRY with writing multiple versions
2. Which of the following is a correct way to follow DRY in system design?
easy
A. Creating reusable components or modules
B. Ignoring code reuse to save time
C. Writing separate code for each similar task
D. Copy-pasting the same code in multiple modules
Solution
Step 1: Identify DRY best practice
DRY encourages reuse by creating components or modules that can be used multiple times.
Step 2: Evaluate options
Copy-pasting or writing separate code repeats work, which breaks DRY. Ignoring reuse wastes effort.
Final Answer:
Creating reusable components or modules -> Option A
Quick Check:
Reuse = DRY [OK]
Hint: Reuse code with modules/components [OK]
Common Mistakes:
Copy-pasting code instead of reusing
Writing separate code for similar tasks
Ignoring reuse to save time
3. Consider this pseudocode snippet following DRY:
function calculateTax(amount) {
return amount * 0.1;
}
function calculateTotal(amount) {
return amount + calculateTax(amount);
}
print(calculateTotal(100));
What is the output?
medium
A. 10
B. 100
C. 110
D. Syntax Error
Solution
Step 1: Calculate tax for amount 100
Tax = 100 * 0.1 = 10
Step 2: Calculate total using tax
Total = 100 + 10 = 110
Final Answer:
110 -> Option C
Quick Check:
100 + 10 = 110 [OK]
Hint: Add tax to amount using reusable function [OK]
Common Mistakes:
Forgetting to add tax to amount
Confusing tax amount with total
Assuming syntax error without checking code
4. You see this code snippet violating DRY:
function sendEmailToUser1() {
// send email logic
}
function sendEmailToUser2() {
// send email logic
}
What is the best fix to follow DRY?
medium
A. Copy-paste send email logic in more functions
B. Create one function sendEmail(user) and reuse it
C. Keep both functions as is for clarity
D. Remove email sending completely
Solution
Step 1: Identify repeated code
Both functions have the same email sending logic repeated.
Step 2: Refactor to reusable function
Create one function with a parameter for user to reuse the logic.
Final Answer:
Create one function sendEmail(user) and reuse it -> Option B
Quick Check:
Refactor repeated code = DRY [OK]
Hint: Use parameters to reuse functions [OK]
Common Mistakes:
Leaving duplicate functions unchanged
Copy-pasting code more times
Removing needed functionality
5. You design a system with multiple services needing user authentication. To follow DRY, which approach is best?
hard
A. Implement authentication logic separately in each service
B. Skip authentication to reduce complexity
C. Copy authentication code into each service for speed
D. Create a shared authentication service used by all services
Solution
Step 1: Identify repeated authentication logic
Multiple services need the same authentication, so repeating code breaks DRY.
Step 2: Use a shared authentication service
Centralizing authentication logic in one service allows reuse and easier maintenance.
Final Answer:
Create a shared authentication service used by all services -> Option D
Quick Check:
Shared service = DRY in system design [OK]
Hint: Centralize common logic in shared services [OK]
Common Mistakes:
Duplicating authentication code in each service
Skipping authentication for simplicity
Copy-pasting code instead of sharing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.