Day of Week Namer - Problem

Given a number between 1 and 7 (inclusive), return the corresponding day of the week name.

Mapping:

  • 1 → "Monday"
  • 2 → "Tuesday"
  • 3 → "Wednesday"
  • 4 → "Thursday"
  • 5 → "Friday"
  • 6 → "Saturday"
  • 7 → "Sunday"

For any input outside the range 1-7, return "Invalid".

Note: This problem focuses on conditional logic and input validation.

Input & Output

Example 1 — Valid Weekday
$ Input: dayNumber = 3
Output: Wednesday
💡 Note: Input 3 corresponds to Wednesday in the week mapping (1=Monday, 2=Tuesday, 3=Wednesday, etc.)
Example 2 — Weekend Day
$ Input: dayNumber = 7
Output: Sunday
💡 Note: Input 7 corresponds to Sunday, the last day in our 1-7 mapping system
Example 3 — Invalid Input
$ Input: dayNumber = 8
Output: Invalid
💡 Note: Input 8 is outside the valid range of 1-7, so we return 'Invalid' to handle the error gracefully

Constraints

  • Input will be an integer
  • Valid range is 1 ≤ dayNumber ≤ 7
  • Invalid inputs should return "Invalid"

Visualization

Tap to expand
INPUT5Day NumberValid range: 1-7Examples:1 → Monday3 → Wednesday7 → Sunday8 → InvalidALGORITHM1Validate input range2Use direct lookup3Return day nameLookup Array:[0] "" (unused)[1] "Monday"[2] "Tuesday"[3] "Wednesday"[4] "Thursday"[5] "Friday" ←[6] "Saturday"[7] "Sunday"RESULTFridayOutput StringInput 5 maps toindex 5 in arrayTime: O(1)Space: O(1)Key Insight:Using array indexing or switch statements provides O(1) direct accessinstead of sequential condition checking, making lookup instant.TutorialsPoint - Day of Week Namer | Array Lookup Approach
Asked in
Microsoft 15 IBM 12 Oracle 8
15.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen