Data Structures & AlgorithmsModerateFlipkart/Uber OA · placement
Run-length encoding: each maximal run is written as the character followed by its length. What is printed?
Run-length encoding: each maximal run is written as the character followed by its length. What is printed?
s = "yyyyyyyyxx"
out, i = [], 0
while i < len(s):
j = i
while j < len(s) and s[j] == s[i]: j += 1
out.append(s[i] + str(j - i)); i = j
enc = "".join(out)
print(enc, len(enc) < len(s))- A. y8x2 True
- B. y2y2y2y2x2 False
- C. y8x4 True
- D. y8x3 True
Correct answer: A. y8x2 True
Runs are maximal, so equal neighbouring characters merge into one group; the encoding only helps when runs are long. Output: y8x2 True.
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
- In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…
- Counting sort on [1, 1, 2, 1, 0, 3, 3, 0, 2, 0] (values 0–4) builds count[], turns it into cumulative counts, then scans the input from…
- What is the length of the shortest contiguous subarray of [1, 3, 1, 8, 7, 3, 4, 7, 1, 2] whose sum is at least 20? (All values are…
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [0…