In the Create Sorted Array through Instructions interview question, you are given an array instructions. You build a new sorted array one by one. For each element, the "cost" of inserting it is the minimum of:
Google and Akuna Capital use this Hard coding problem to test your knowledge of advanced data structures for range queries. A naive approach (sorting or using a simple list) takes O(N^2), which is too slow for . It evaluates your ability to use Binary Indexed Trees (Fenwick Trees) or Segment Trees to perform point updates and prefix sums in O(log M) time, where M is the maximum value in the input.
This problem uses Binary Indexed Tree (BIT) or Segment Tree.
x in instructions:
x: query(x - 1).x: query(x).i.cost_less = query(x - 1).cost_greater = i - query(x).total_cost += min(cost_less, cost_greater).update(x, 1).instructions = [1, 5, 6, 2]
{1: 1}.{1: 1, 5: 1}.{1: 1, 5: 1, 6: 1}.Fenwick Trees are much easier to implement during an interview than Segment Trees. Practice the update and query functions until you can write them in under 2 minutes.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Good Triplets in an Array | Hard | Solve | |
| Number of Pairs Satisfying Inequality | Hard | Solve | |
| Count of Range Sum | Hard | Solve | |
| Count of Smaller Numbers After Self | Hard | Solve | |
| Reverse Pairs | Hard | Solve |