Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Convert 24 Hours to Seconds
00:00
5 left

Convert 24 Hours to Seconds

EasyPython

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):
Interviewer

Your question is Convert 24 Hours to Seconds. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.