Pull-up and pull-down resistor configuration in Embedded C - Time & Space Complexity
When configuring pull-up or pull-down resistors in embedded C, we often write code to set pin modes and states.
We want to understand how the time it takes to run this code changes as we configure more pins.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
if (config[i] == 1) {
// Configure pin with pull-up resistor
setPinMode(i, INPUT_PULLUP);
} else {
// Configure pin with pull-down resistor
setPinMode(i, INPUT_PULLDOWN);
}
}
This code sets the resistor configuration for n pins, choosing pull-up or pull-down based on an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each pin to configure its resistor.
- How many times: Exactly n times, once per pin.
Each pin requires one configuration step, so the total work grows directly with the number of pins.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 configuration calls |
| 100 | 100 configuration calls |
| 1000 | 1000 configuration calls |
Pattern observation: Doubling the number of pins doubles the work needed.
Time Complexity: O(n)
This means the time to configure resistors grows in direct proportion to the number of pins.
[X] Wrong: "Configuring multiple pins with pull-up or pull-down resistors happens instantly regardless of how many pins there are."
[OK] Correct: Each pin requires a separate configuration step, so more pins mean more time spent.
Understanding how configuration loops scale helps you write efficient embedded code and explain your reasoning clearly in interviews.
"What if we batch configure pins in groups instead of one by one? How would the time complexity change?"