ProgrammingModerateAmazon OA · Flipkart coding round
What is the usual fastest way to read n = 10^6 integers from one input line in Python for a judge?
- A. Read the whole input with sys.stdin.buffer.read().split() and convert the needed tokens with map(int, ...)
- B. Call input() once per number inside a loop of 10^6 steps
- C. Use eval(input()) on the whole line and unpack the result
- D. Read the file character by character with sys.stdin.read(1)
Correct answer: A. Read the whole input with sys.stdin.buffer.read().split() and convert the needed tokens with map(int, ...)
Reading all bytes in one call and splitting avoids a Python-level call per number. eval is slow and unsafe, and character reads add overhead per character.
More Python Programming questions
- What is printed? python import heapq print(heapq.nsmallest(2, [5, 1, 4, 1]), list(reversed(range(3))), list(reversed('ab')))
- What is printed? python print(any(map(str.isdigit, 'ab1')), all(map(str.isalpha, 'ab1')))
- What is printed? python print(sorted({'b': 1, 'a': 2}.items()), sum([1, 2, 3], start=10))
- What is printed? python print(max(3, 7, key=lambda v: -v), min('a', 'B'))
- What is printed? python print(str(True) + str(1 == 1.0), type(True + True).name)
- What is printed? python print(round(-0.5), round(0.5), round(1.5), round(-1.5))