Skip to content

Commit

Permalink
Merge pull request #1 from gmickel:move-healthbar-component
Browse files Browse the repository at this point in the history
Move-healthbar-component
  • Loading branch information
gmickel authored Jul 19, 2024
2 parents 07607f2 + 6531f13 commit a92ad20
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/components/Quiz/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip';
import HealthBar from '@/components/HealthBar';
import HealthBar from '@/components/Quiz/HealthBar';
import useSoundEffects from '@/hooks/useSoundEffects';
import type { ParsedQuestion } from '@/lib/markdownParser.ts';
import { loadAllQuestions } from '@/lib/markdownParser.ts';
Expand Down
141 changes: 141 additions & 0 deletions src/questions/example-question-two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: The Ultimate Coding Challenge
description: Test your programming skills across various languages and concepts!
level: 2 # Questions will be sorted by level
correctAnswer: 2
difficulty: "Expert" # Beginner, Intermediate, or Expert (optional)
---

## 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!');`

```python
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).

```go
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:

```python
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:

```python
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:

```python
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:

```python
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?

0 comments on commit a92ad20

Please sign in to comment.