The "Check If All 1's Are at Least Length K Places Away interview question" is an array distance validation task. You are given a binary array (containing only 0s and 1s) and an integer k. You need to determine if every pair of '1's in the array are separated by at least k zeros. In other words, the distance between the indices of any two 1s must be at least .
Meta and Bloomberg ask the "Check If All 1's Are at Least Length K Places Away coding problem" to evaluate a candidate's ability to maintain state while traversing an array. It tests your logic in tracking the "last seen" position of a specific element and your ability to implement an solution with minimal space. It’s a fundamental "Array interview pattern" problem.
This problem uses the Linear Scan with State Tracking pattern.
last_pos to store the index of the most recently encountered '1'. Initialize it to a value that won't trigger a failure (like -1 or a very small negative number).last_pos is not -1, check the distance: current_index - last_pos - 1.k, return false.last_pos to the current index.Array: [1, 0, 0, 0, 1, 0, 1],
last_pos = 0.last_pos = 4.current - last instead of current - last - 1).Always look for ways to solve array distance problems in a single pass. Keeping track of the "previous occurrence" index is a classic trick that appears in many variations of this "Array interview pattern."