Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to skip leading spaces in the input string.
DSA C
while (str[i] == [1]) { i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '\t' instead of space.
Skipping newline characters instead of spaces.
✗ Incorrect
The function skips all leading space characters ' ' before processing the number.
2fill in blank
mediumComplete the code to check if the current character is a minus sign.
DSA C
if (str[i] == [1]) { sign = -1; i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for '+' instead of '-'.
Not incrementing the index after sign detection.
✗ Incorrect
The minus sign '-' indicates the number is negative, so sign is set to -1.
3fill in blank
hardFix the error in the loop condition to process digits only.
DSA C
while (str[i] [1] '0' && str[i] <= '9') { result = result * 10 + (str[i] - '0'); i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks one character.
Using '!=' which allows non-digit characters.
✗ Incorrect
The loop should continue while the character is greater than or equal to '0' and less than or equal to '9' to process digits.
4fill in blank
hardFill both blanks to correctly handle integer overflow before multiplying and adding.
DSA C
if (result > [1] || (result == [2] && (str[i] - '0') > 7)) { return sign == 1 ? INT_MAX : INT_MIN; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using INT_MIN values instead of INT_MAX for overflow check.
Using INT_MAX % 10 for the equality check instead of INT_MAX / 10.
Not checking the next digit properly.
✗ Incorrect
To prevent overflow, check if result > INT_MAX/10 or (result == INT_MAX/10 && (str[i] - '0') > 7). Note that 7 == INT_MAX % 10.
5fill in blank
hardFill all three blanks to complete the atoi function's main loop and return statement.
DSA C
while (str[i] >= '0' && str[i] <= '9') { if (result > [1] || (result == [2] && (str[i] - '0') > [3])) { return sign == 1 ? INT_MAX : INT_MIN; } result = result * 10 + (str[i] - '0'); i++; } return result * sign;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different values for the first two blanks.
Using 8 instead of 7 for the last digit check.
✗ Incorrect
The loop checks overflow using INT_MAX/10 and INT_MAX/10 for equality, and >7 (last digit of INT_MAX). Then it builds the number and returns with sign.
