Facebook | Phone | Find the Quarter Majority
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-05-12Added By Anonymous
Source: Link
Company: Meta (Facebook)
Role: e3, e4
Stage: Phone Screen
Location: Seattle
Year: 2020
Date: May 11, 2020
Location: Seattle, USA
Given a sorted array of size
Example 1:
Example 2:
Example 3:
Initialy, I've offer 2 solution
1. Use Map with key and count pair and go through the Map and return the true, if any of the count >= 1/4*length;
2. Use for loop with two pointer(first, prev) , increase the count, if values are same and return true when you see the count >=1/4* length;
Both the solutions are O(N) time complexity. But, she was expecting the O(log n) solution and she given below HINT:
I understood it was a binary search apporach, but couldn't make it happen and time is crossed.
---
Related problems:
* Majority Element
Role: e3, e4
Stage: Phone Screen
Location: Seattle
Year: 2020
Date: May 11, 2020
Location: Seattle, USA
Given a sorted array of size
n, find the majority element. The majority element is the element that appears more than n/4 times. You may assume that the array is non-empty and the majority element always exist in the array.Example 1:
Input: [1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7]
Output: 3Example 2:
Input: [1, 1, 2, 2, 3, 3, 4, 5, 7, 7, 7]
Output: 7Example 3:
Input: [1, 1, 2, 3]
Output: 1Initialy, I've offer 2 solution
1. Use Map with key and count pair and go through the Map and return the true, if any of the count >= 1/4*length;
2. Use for loop with two pointer(first, prev) , increase the count, if values are same and return true when you see the count >=1/4* length;
Both the solutions are O(N) time complexity. But, she was expecting the O(log n) solution and she given below HINT:
int windowSize = arr.length*(1/4)a[0] == a[0+windowSize-1]a[0] != a[0+windowSize-1]I understood it was a binary search apporach, but couldn't make it happen and time is crossed.
---
Related problems:
* Majority Element