Data Structures & AlgorithmsHardTCS NQT · Infosys
What is the length of the longest contiguous substring of the binary string "1100100110111000101" that has equally many 0s and 1s?
- A. 16
- B. 9
- C. 20
- D. 18
Correct answer: D. 18
Map each 1 to +1 and each 0 to −1 and look for the longest subarray with sum 0, which is a pair of equal prefix balances. Recording the LAST index for each balance instead of the first gives the shorter lengths.
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 smallest positive period p of the string abcabcabca, meaning the smallest p such that s[i] = s[i+p] for every valid i?
- How many distinct cyclic rotations does the string abcabc have (rotations are the strings obtained by moving a prefix to the end…
- How many subarrays of [1, 2, 1, 2, 2, 1] read the same forwards and backwards (as sequences of values)?
- What is the length of the longest palindromic substring of xabacdcabay?
- What is the minimum number of cuts needed to partition the string abacdcdad into palindromic pieces?
- How many times does the pattern aba occur in ababababa when occurrences may overlap?