KatInterview

Triple Array Toolkit: Lookup, Increment, Zero Shift

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashingbinary-searchtwo-pointerscarry

Last updated

Combines a binary search on a sorted list, adding one to a digit-array number with carry, and a stable in-place move of zeros to the back.

Write one function that performs three small, unrelated array tasks and returns all three results together.

Inputs

  • sorted_values: a list of integers sorted in nondecreasing order.
  • target: an integer to look for in sorted_values.
  • digits: a list of decimal digits (each 0 to 9) that spells a nonnegative integer, most significant digit first.
  • values: a list of integers.

Task A: Locate the target

Search sorted_values for target and report its index, or -1 if it is absent. Your search should take O(log n) time.

To keep the output deterministic when duplicates exist, use the classic closed-window binary search. Start with lo = 0 and hi = len(sorted_values) - 1. While lo <= hi, set mid = (lo + hi) // 2.

  • If sorted_values[mid] == target, return mid immediately.
  • If sorted_values[mid] < target, set lo = mid + 1.
  • Otherwise, set hi = mid - 1.

If the window becomes empty, the answer is -1.

Task B: Increment the number

Treat digits as a large decimal number and add 1 to it. Return the new digit list, most significant digit first, with no leading zeros added except where the number itself requires them.

  • Do not convert the whole list into an integer. Handle carries digit by digit.
  • Do not change the caller's digits list. Work on a copy.
  • If every digit is 9, the result has one more digit than the input (for example [9, 9] becomes [1, 0, 0]).
  • If digits is empty, treat it as the number zero, so the result is [1].

Task C: Push zeros to the back

Rearrange values in place so that all nonzero elements come first, in their original relative order, followed by all the zeros. Use O(1) extra space. The list you return for this task is the same, modified values list.

Output

Return a list of three items, in this order:

  1. The index from Task A (an int).
  2. The digit list from Task B.
  3. The rearranged values list from Task C.

Constraints

  • 0 <= len(sorted_values), len(digits), len(values) <= 10^5
  • Elements of sorted_values and values, and target, fit in a 32-bit signed integer.
  • Every element of digits is an integer from 0 to 9.
  • sorted_values is sorted in nondecreasing order.

Examples

Example 1

Input: run_array_tasks([1, 3, 5, 7, 9], 7, [1, 2, 3], [0, 1, 0, 3, 12])

Output: [3, [1, 2, 4], [1, 3, 12, 0, 0]]

Example 2

Input: run_array_tasks([1, 3, 5, 7, 9], 4, [9, 9, 9], [0, 0, 0])

Output: [-1, [1, 0, 0, 0], [0, 0, 0]]

Hints

Hint 1

For the search, keep a window [lo, hi] that must contain the target if it exists, and shrink it by comparing against the middle element.

Hint 2

For the increment, scan from the last digit leftward. A 9 turns into 0 and passes the carry on. The first digit below 9 gets +1 and you can stop.

Hint 3

For the zeros, keep a write pointer for the next slot a nonzero element belongs in. Scan once, placing each nonzero at the write pointer, then fill what is left with zeros.