User authentication basics in Raspberry Pi - Time & Space Complexity
When checking user login details, the time it takes depends on how many users we check. We want to understand how this time grows as more users are added.
How does the program's work increase when the number of users grows?
Analyze the time complexity of the following code snippet.
users = ["alice", "bob", "carol", "dave"]
passwords = ["123", "abc", "xyz", "pass"]
input_user = "bob"
input_pass = "abc"
for i in range(len(users)):
if users[i] == input_user and passwords[i] == input_pass:
print("Access granted")
break
else:
print("Access denied")
This code checks if the entered username and password match any stored user data by going through the list one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of users and passwords to compare each entry.
- How many times: Up to once for each user in the list, until a match is found or the list ends.
As the number of users grows, the program may need to check more entries before finding a match or giving up.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of users. More users mean more work.
Time Complexity: O(n)
This means the time to check login grows in a straight line with the number of users. Double the users, double the checks.
[X] Wrong: "The program always checks all users no matter what."
[OK] Correct: The loop stops as soon as it finds a matching user and password, so it may check fewer users.
Understanding how checking user data grows with more users helps you explain how programs handle bigger data smoothly. This skill shows you can think about program speed and efficiency.
"What if we stored users in a special way that lets us find them instantly? How would the time complexity change?"
Practice
Solution
Step 1: Understand user authentication
User authentication is about checking who you are before access is granted.Step 2: Identify the correct purpose
Only To verify the identity of a user before allowing access describes verifying identity before access, which matches authentication.Final Answer:
To verify the identity of a user before allowing access -> Option BQuick Check:
User authentication = verify identity [OK]
- Confusing authentication with performance or internet speed
- Thinking authentication installs software
- Mixing authentication with network connection
Solution
Step 1: Identify correct comparison operator
In Python, '==' checks equality; '=' assigns values.Step 2: Check logical operators for authentication
Both username and password must match, so use 'and' not 'or'.Final Answer:
if username == 'pi' and password == 'raspberry': -> Option CQuick Check:
Use '==' and 'and' for correct check [OK]
- Using '=' instead of '==' for comparison
- Using 'or' instead of 'and' to check both credentials
- Mixing assignment and comparison operators
username = 'pi'
password = 'raspberry'
input_user = 'pi'
input_pass = 'wrongpass'
if input_user == username and input_pass == password:
print('Access granted')
else:
print('Access denied')Solution
Step 1: Compare input username and password
input_user matches username 'pi', but input_pass is 'wrongpass', not 'raspberry'.Step 2: Evaluate the if condition
Since password check fails, condition is false, so else block runs.Final Answer:
Access denied -> Option DQuick Check:
Password mismatch means access denied [OK]
- Assuming partial match grants access
- Confusing syntax error with logic error
- Ignoring else block output
username = 'pi'
password = 'raspberry'
input_user = input('Enter username: ')
input_pass = input('Enter password: ')
if input_user = username and input_pass = password:
print('Access granted')
else:
print('Access denied')Solution
Step 1: Check the if condition syntax
The code uses '=' which assigns values, but '==' is needed to compare values.Step 2: Verify other syntax parts
Colons are present, input() usage is correct, and print() has parentheses.Final Answer:
Using '=' instead of '==' in if condition -> Option AQuick Check:
Use '==' to compare, not '=' [OK]
- Confusing assignment '=' with comparison '=='
- Forgetting colon after if statement
- Misusing input() or print() syntax
Solution
Step 1: Understand the goal of locking out after 3 failures
We need to count failed tries and stop allowing login after 3.Step 2: Evaluate options for implementing this
Using a counter variable to track attempts is the correct and simple method.Final Answer:
Use a counter variable to track failed attempts and stop after 3 -> Option AQuick Check:
Counting attempts controls login lockout [OK]
- Changing password automatically without user consent
- Allowing access despite wrong passwords
- Restarting device unnecessarily on failure
Ready to prove it?
Pass the test and this pattern moves unknown
