Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Sort [−4, 3, −3, −5, −4, 5, 2] and, for each i, move two pointers to find the triplet sum closest to -2. What is that closest sum?
- A. 0
- B. -3
- C. -2
- D. -1
Correct answer: C. -2
Sorting gives [−5, −4, −4, −3, 2, 3, 5]. Fix i, then if the current sum is below the target move lo right, else move hi left, tracking the smallest |sum − -2|. The closest achievable sum is -2.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- How many contiguous subarrays of [−2, 0, 5, −1, 4, 3, −1, −2, −3] have sum exactly 4? (Prefix sums with a hash map of prefix-sum…
- For s = "cdbccdddbaddca", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- For s = "bcadbdaccdddbd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- How many contiguous subarrays of [1, 3, 8, 1, 3, 6, 0, 0, 9] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [23…