← All Python interview questions
Bucket engagement scores into segments
EasyFunctions & LogicDeloitteAccenture
The growth team scores every learner from 0-100 and wants those scores collapsed into four named segments for a dashboard.
Write a function segment(score) that returns:
"Champion"for 80 and above"Engaged"for 60-79"At Risk"for 40-59"Dormant"below 40
For any score outside 0-100, return "Invalid".
This fixture runs before your code on every Run and Submit, so the data is always clean.
scores = [95, 72, 51, 12, 80, 60, 40, 105]- Step 1: Validate before you bucket. Guarding the invalid range first means the rest of the function can assume a clean 0-100 score.
- Step 2: Order the thresholds from highest to lowest. Because each `return` exits immediately, reaching `score >= 60` already implies the score is below 80 — no upper bounds needed.
- Step 3: The final `return "Dormant"` handles everything under 40, so every path returns a value.
- Step 4: Boundary values matter in interviews: 80 is a Champion and 40 is At Risk because the comparisons are inclusive (`>=`).
def segment(score):
if score < 0 or score > 100:
return "Invalid"
if score >= 80:
return "Champion"
if score >= 60:
return "Engaged"
if score >= 40:
return "At Risk"
return "Dormant"Learn the concept first
Stuck on Functions & Logic? These free Python lessons teach it step by step.
More Functions & Logic questions
- This is the only question on this topic for now.
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program