ProgrammingModerateAmazon OA · Flipkart coding round
A Python solution reads n = 10^6 numbers with `input()` in a loop, calling `int(input())` for each line. Which change is the most effective for a judge with strict time limits?
- A. Use a while True loop with a try and except around input
- B. Read everything with sys.stdin.buffer.read().split() once, then convert the tokens
- C. Replace int() with eval() for each line
- D. Call print() inside the input loop to flush the buffer
Correct answer: B. Read everything with sys.stdin.buffer.read().split() once, then convert the tokens
input() and print() flush and go through the text layer each time. Reading all of stdin at once and splitting into tokens reduces the per-line overhead by a large factor.
More Python Programming questions
- What is printed? python import math print(math.gcd(12, 18, 27), math.lcm(4, 6), math.gcd())
- What is printed? python print(divmod(-(10 20), 7))
- What is printed? python import heapq h = [4, 1, 3] heapq.heapify(h) print(h[0], sorted(h))
- What is printed? python from collections import deque d = deque([1, 2, 3, 4, 5], maxlen=3) d.append(6) d.appendleft(0) print(list(d))
- What is printed? python x = [0] 3 y = [[0] 2] 2 y[0][1] = 7 print(x, y)
- What is printed? python import bisect vals = [10, 20, 30] print(bisect.bisectleft(vals, 0), bisect.bisectleft(vals, 35)…