Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT
Vertical lines have heights [1, 8, 6, 2, 5, 4, 8, 3, 7, 9, 1, 6] at positions 0 to 11. Choose two lines; the water they hold is min(height) × distance. What is the maximum water that can be held?
- A. 59
- B. 11
- C. 49
- D. 64
Correct answer: D. 64
Two pointers from both ends: the shorter wall limits the water, so moving the taller pointer can never help; move the shorter one. Taking the tallest line with the farthest partner is not optimal.
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
- What is the maximum possible sum of entries of a non-empty contiguous rectangular submatrix of [[1, -2, 3, 0], [-1, 4, -2, 2], [2, -3, 5…
- A fence has 5 posts and each post is painted one of k = 3 colours. No three consecutive posts may share the same colour. How many…
- Target string "abcbc" must be built by concatenating words from the list ['ab', 'bc', 'abc', 'a', 'b', 'c', 'cb']; each word can be…
- A truck carries at most 5 boxes. Box types are given as (number of boxes of this type, units per box): [(1, 3), (2, 2), (3, 1), (2, 4)]…
- How many contiguous subarrays of 2 3 4 1 6 9 have a product divisible by 6?
- How many contiguous subarrays of 5 2 7 1 8 3 6 4 have an ODD length and a sum divisible by 7?