Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Convert 24 Hours to Seconds

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Convert 24 Hours to Seconds. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

NXP Semiconductors firmware components often convert clock readings into a single elapsed-seconds value for scheduling and timestamp comparisons. Implement a function that converts either 24-hour or 12-hour time into seconds since midnight.

Formal Specification

Define convert_to_seconds(hours, minutes, seconds, period):

  1. When period is "24H", interpret hours using 24-hour time. Valid hours are 0 through 23 for ordinary clock times. Also accept 24:00:00 and return 86400 to represent exactly one full day.
  2. When period is "AM" or "PM", interpret hours using 12-hour time. Convert 12 AM to hour 0, and convert 12 PM to hour 12.
  3. Return the number of seconds from midnight as an integer.
  4. Inputs satisfy the stated constraints, so input validation is not required.

Constraints

  • period is one of "24H", "AM", or "PM"
  • For "24H", 0 <= hours <= 24, 0 <= minutes <= 59, and 0 <= seconds <= 59
  • 24:00:00 is the only valid 24-hour input with hours = 24
  • For "AM" or "PM", 1 <= hours <= 12
  • For "AM" or "PM", 0 <= minutes <= 59 and 0 <= seconds <= 59

Function Signature

def convert_to_seconds(hours, minutes, seconds, period):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output