Find the Greatest of Three Numbers
Filed under Cognizant · Also reported at L & T Infotech
Free read 2 of 3Compare the first two numbers and store the larger one in a temporary variable.
The question
Given three integers, determine and return the largest value among them without using built-in library functions if possible.
Constraints
- Inputs are 32-bit signed integers.
- Numbers can be positive, negative, or zero.
Examples
- Input
- a=10, b=25, c=15
- Output
- 25
- Why
- 25 is greater than both 10 and 15.
Approach
Compare the first two numbers and store the larger one in a temporary variable.
Compare the temporary variable with the third number to find the absolute maximum.
Solution
O(1) time, O(1) space
class Solution:
def findMax(self, a: int, b: int, c: int) -> int:
# Simple nested comparison
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
Tips
- What they are testing: Tests basic conditional logic and comparison operators.
- Common mistake: Not handling cases where two or three numbers are equal.
Related Cognizant questions
Collected from public candidate posts and coding forums. Updated Nov 2026.