ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
def longest(s):
last = {}
l = best = 0
for r, ch in enumerate(s):
if ch in last and last[ch] >= l:
l = last[ch] + 1
last[ch] = r
best = max(best, r - l + 1)
return best
print(longest('abba'), longest('pwwkew'), longest(''))- A. 3 3 0
- B. 2 3 0
- C. 2 4 0
- D. 2 3 1
Correct answer: B. 2 3 0
In abba the longest repeat-free window is 2 characters, and in pwwkew it is wke with 3. The left edge moves only when the repeated character is still inside the window, and the empty string gives 0.
More Python Programming questions
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…
- A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?
- What is printed? python def ways(coins, amt): dp = [0] (amt + 1) dp[0] = 1 for c in coins: for v in range(c, amt + 1): dp[v] += dp[v - c]…
- What is printed? python def lcs(a, b): dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1): for j in range(1…
- What is printed? python items = [(60, 10), (100, 20), (120, 30)] cap = 50 best = 0 w = 0 for v, wt in sorted(items, key=lambda x: x[0] /…
- What is printed? python def mincapacity(w, days): lo, hi = max(w), sum(w) while lo < hi: mid = (lo + hi) // 2 need, cur = 1, 0 for x in w…