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?

  1. A. Use a while True loop with a try and except around input
  2. B. Read everything with sys.stdin.buffer.read().split() once, then convert the tokens
  3. C. Replace int() with eval() for each line
  4. 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

Exams that ask this topic