EMChamp Blog

Coding Problem 2 | CodeFights - incorrectPasscode Attempts

This problem basically asks you to check if a user has failed 10 times in a row to log into a system. If he has failed 10 times return true, if not return false. I thought this was a good problem because you had to read carefully to understand the edge cases. I kept getting the final 2 tests wrong before actually tracing the code and realizing that I was off by 1 when checking how many times a user has logged it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

boolean incorrectPasscodeAttempts(String passcode, String[] attempts) {
if(attempts.length == 0) {
return false;
}

int numAttempts = 0;
boolean overTenFailures=false;
for (String entry : attempts) {
if (passcode.equals(entry)) {
numAttempts = 0;
}
else if (numAttempts >= 9){
overTenFailures=true;
numAttempts++;
}
else{
numAttempts++;
}
}

return overTenFailures;
}