LearnFrenzy
Sign in Take a free test

Python Coding Interview Questions · Predict the Output · Question 3

bisect_left and bisect_right Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Python program?

main.py
import bisect
a = [10, 20, 20, 30]
print(bisect.bisect_left(a, 20), bisect.bisect_right(a, 20), bisect.bisect_left(a, 25), bisect.bisect_left(a, 5))
  • A 1 2 3 0
  • B 2 3 3 0
  • C 1 3 3 0 Correct
  • D 1 3 2 -1
Correct Answer: Option C

bisect_left returns the first position where 20 could go (before the equal items), bisect_right the position after them; 25 would be inserted at index 3.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links