This code is meant to find the median of a list of integers. The list is assumed to be of odd length.
This is based on this HackerRank question.
I'm doing this to improve my style and to improve my knowledge of fundamental algorithms/data structures for an upcoming coding interview.
from heapq import heappush, heappop
def median(l):
heap = []
for i in l:
heappush(heap, i)
cur_int = None
mid = len(l) // 2
for i in range(0, mid+1):
cur_int = heappop(heap)
median = cur_int
return median
I think this algorithm is O(n) time and space complexity. Please correct me if I'm wrong. I'm also open to any suggestions to improving its complexities or general performance.