Skip to content

Latest commit

 

History

History
142 lines (110 loc) · 3.89 KB

example-question.md

File metadata and controls

142 lines (110 loc) · 3.89 KB
title description level correctAnswer difficulty timeLimit
The Ultimate Coding Challenge
Test your programming skills across various languages and concepts!
1
2
Intermediate
20

Context

Introduction

Welcome to the Ultimate Coding Challenge! This quest will test your knowledge of programming concepts, algorithms, and problem-solving skills. Are you ready to prove your coding prowess?

Test of code highlighting:

inline code: console.log('Hello, Alice!');

def decorator(func):
  def wrapper(*args, **kwargs):
    print("Before function call")
    result = func(*args, **kwargs)
    print("After function call")
    return result
  return wrapper

Question

You're tasked with implementing a function to find the most frequent element in an array. The function should work efficiently for large arrays. Consider the following requirements:

  1. The function should handle arrays of integers.
  2. If there are multiple elements with the same highest frequency, return any one of them.
  3. The function should have a time complexity better than O(n^2).
func mostFrequent(arr []int) int {
    maxCount := 0
    result := arr[0]
    for i := range arr {
        count := 0
        for j := range arr {
            if arr[i] == arr[j] {
                count++
            }
        }
        if count > maxCount {
            maxCount = count
            result = arr[i]
        }
    }
    return result
}

How would you implement this function?

Outro

Efficient array manipulation and understanding of data structures are crucial skills for any programmer. This problem tests your ability to balance time complexity with code readability.

Answers

  • Use nested loops to count occurrences of each element:
def most_frequent(arr):
    max_count = 0
    result = arr[0]
    for i in range(len(arr)):
        count = 0
        for j in range(len(arr)):
            if arr[i] == arr[j]:
                count += 1
        if count > max_count:
            max_count = count
            result = arr[i]
    return result
  • Use a hash map to count occurrences in a single pass:
from collections import defaultdict

def most_frequent(arr):
    count = defaultdict(int)
    for num in arr:
        count[num] += 1
    return max(count, key=count.get)
  • Sort the array and count consecutive elements:
def most_frequent(arr):
    arr.sort()
    max_count = 1
    res = arr[0]
    curr_count = 1
    for i in range(1, len(arr)):
        if arr[i] == arr[i-1]:
            curr_count += 1
        else:
            if curr_count > max_count:
                max_count = curr_count
                res = arr[i-1]
            curr_count = 1
    return res
  • Use a set to eliminate duplicates, then count occurrences:
def most_frequent(arr):
    return max(set(arr), key=arr.count)

Explanation

The correct answer is option 2: Using a hash map to count occurrences in a single pass.

This solution is optimal because:

  1. It has a time complexity of O(n), where n is the length of the array.
  2. It only requires a single pass through the array.
  3. The space complexity is O(k), where k is the number of unique elements in the array.

Here's a breakdown of how the function works:

  1. defaultdict(int) creates a dictionary where any new key is automatically initialized with a value of 0.
  2. The loop for num in arr: iterates through each element in the array.
  3. count[num] += 1 increments the count for each number.
  4. max(count, key=count.get) returns the key (number) with the highest count.

This solution efficiently handles large arrays and meets all the specified requirements.

Hint

Think about data structures that allow for fast lookups and updates. How can you keep track of counts while only passing through the array once?