Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
For an array of n elements, how many contiguous subarrays are there, and how many subsequences (including the empty one)?
- A. n(n + 1)/2 and 2ⁿ
- B. n² and 2ⁿ − 1
- C. 2ⁿ and n(n + 1)/2
- D. n(n − 1)/2 and n!
Correct answer: A. n(n + 1)/2 and 2ⁿ
A subarray is fixed by its start ≤ end: n(n + 1)/2 choices. A subsequence keeps or drops each element independently: 2ⁿ including the empty one.
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
- A monotonic deque reports the maximum of every window of size 3 in [19, 6, 7, 16, 10, 2, 12, 8, 17]. What is the SUM of all reported…
- Two pointers start at both ends of the sorted array [3, 4, 5, 6, 8, 17, 18, 23, 26, 27]. How many pairs (i < j) have a[i] + a[j] ≤ 40?…
- What does this loop-counting code print? (Python) python n, c = 20, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "beeebdddcbadad" with all characters…
- What is the length of the smallest substring of "bcdadddaacbac" that contains every distinct character of the string at least once?…