KatInterview

Next Lexicographic Ordering Plus Left-to-Right Calculator

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingnext-permutationtwo-pointersexpression-parsing

Last updated

Rearrange an array in place into its next lexicographic ordering, and evaluate a parenthesis-free arithmetic string with truncating division, both in linear time.

Next Ordering and Quick Calculator

Write one function that solves two independent tasks and returns both results.

Task 1: Advance the ordering

You get a list of integers arr. Rearrange it in place so that it becomes the next arrangement in lexicographic (dictionary) order.

  • Compare two arrangements element by element, from the left. The first position where they differ decides which one is larger.
  • If arr is already the largest possible arrangement of its values, rearrange it into the smallest one, which is ascending order.
  • The list may contain repeated values. The "next" arrangement is the smallest arrangement that is strictly greater than the current one. If no strictly greater arrangement exists (for example [2, 2, 2], or a list of length 0 or 1), wrap around to the ascending arrangement, which may be the list itself.

Aim for O(n) time and O(1) extra space.

Task 2: Evaluate the formula

You get a string formula that contains non-negative integers, the operators +, -, *, /, and optional spaces. There are no parentheses.

  • * and / take precedence over + and -.
  • Operators of equal precedence are applied from left to right.
  • / is integer division that truncates toward zero.
  • A string that is empty or contains only spaces evaluates to 0.
  • The formula is always valid, and no division by zero occurs.
  • Spaces may appear anywhere between tokens, including at the start and end, and they never split a number.

Aim for O(n) time and O(1) extra space.

Return value

Return a dictionary with two keys:

  • "permutation": the rearranged list from Task 1 (the same list object, modified in place).
  • "value": the integer result from Task 2.

Constraints

  • 0 <= len(arr) <= 10^4
  • arr contains integers, and duplicates are allowed.
  • 0 <= len(formula) <= 10^5
  • All numbers in formula are non-negative integers, and every intermediate and final result fits in a 64-bit signed integer.
  • formula contains only digits, spaces, and the characters + - * /.

Examples

Example 1

Input: next_order_and_calc([1, 2, 3], '3 + 4 * 2 / 2')

Output: {'permutation': [1, 3, 2], 'value': 7}

Example 2

Input: next_order_and_calc([3, 2, 1], '10 - 3 * 2')

Output: {'permutation': [1, 2, 3], 'value': 4}

Example 3

Input: next_order_and_calc([1, 3, 2], '7 / 2 + 5')

Output: {'permutation': [2, 1, 3], 'value': 8}

Example 4

Input: next_order_and_calc([1, 2, 3, 4], '100 / 3 * 2 - 7')

Output: {'permutation': [1, 2, 4, 3], 'value': 59}

Hints

Hint 1

For the ordering task, scan from the right to find the last position whose value is strictly smaller than its right neighbour. Everything to the right of it is already in non-increasing order.

Hint 2

Swap that pivot with the rightmost element that is strictly greater than it, then reverse the part after the pivot. If no pivot exists, the reverse covers the whole list.

Hint 3

For the calculator, keep a running total of finished terms plus the current term. When you read an operator, `*` and `/` modify the current term, while `+` and `-` commit it and start a new one. Handle truncation toward zero by dividing absolute values and fixing the sign.