Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stateful Bit Manipulation Commands

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

Your question is Stateful Bit Manipulation Commands. 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

Capital One may represent a collection of binary feature states as an array. Given an initial binary array and an ordered list of commands, execute every command and return the final array.

Each command has one of these forms:

  1. [1]: Change the leftmost 0 in the array to 1. If the array contains no zero, do nothing.
  2. [2, index]: Change the bit at index to 0, regardless of its current value.

The array is zero-indexed. Commands must be processed in the given order because earlier commands affect which position is the leftmost zero.

Formal Specification

Implement apply_bit_commands(bits, commands), where bits is a list containing only 0 and 1, and commands is a list of command lists. Return a new list containing the final bit values.

Constraints

  • 1 <= len(bits) <= 10^5
  • 0 <= len(commands) <= 2 * 10^5
  • Each bit is either 0 or 1
  • Every type-2 command has the form [2, index]
  • For every type-2 command, 0 <= index < len(bits)

Function Signature

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