Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Button Press Program

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

Your question is Button Press Program. 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

A Bosch Rexroth operator panel records button identifiers as operators interact with a machine. Given the recorded button presses and a command sequence, find the starting index of the first contiguous occurrence of that command in the log.

Return -1 if the command sequence does not occur. If it appears multiple times, return the earliest starting index. Button identifiers are case-sensitive integers, and a button may appear more than once in either input.

Formal Specification

Implement find_button_press_sequence(presses, command).

  • Input: presses, a list of integers representing the recorded button presses, and command, a non-empty list of integers representing the target sequence.
  • Output: An integer containing the first zero-based index where command begins in presses, or -1 when no match exists.

Constraints

  • 1 <= len(command) <= len(presses) <= 10^5
  • 0 <= presses[i], command[i] <= 10^9
  • Return the earliest valid match
  • The target sequence is non-empty

Function Signature

def find_button_press_sequence(presses, command):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output