In this lesson, you’ll use what you learned in the previous sections to solve an easy real world interview question. Easy does not mean trivial, but it means something you might find in a coding challenge or internship interview or a warm up question.
Here’s the question:
def majority_element_indexes(lst):
'''
Return a list of the indexes of the majority element.
Majority element is the element that appears more than
floor(n / 2) times.
If there is no majority element, return []
>>> majority_element_indexes([1, 1, 2])
[0, 1]
>>> majority_element_indexes([1, 2])
[]
>>> majority_element_indexes([])
[]
'''
The entire solution will be available at the end of the course. Try to find a solution to the problem yourself and then watch the video for the solution.
You also heard about Big O analysis, which is a way to analyze the speed and memory usage of a function or block of code. See Wikipedia Time complexity for more information.
AlekS on April 24, 2020
my two solutions, second pretty similar to yours.
ls = [6, 8, 1, 1, 2, 3, 8, 5, 5, 8, 8, 8, 8, 8, 8, 8] ls = [1, 1, 2] ls = [] ls = [2, 2, 2, 1, 1, 3, 2, 2] ls = [1, 3, 2, 2, 4, 5]
def majority_element_v0(lst):
print(majority_element_v0(ls))
def majority_element_v1(lst):
print(majority_element_v1(ls))