diff --git a/src/components/HealthBar.tsx b/src/components/Quiz/HealthBar.tsx similarity index 100% rename from src/components/HealthBar.tsx rename to src/components/Quiz/HealthBar.tsx diff --git a/src/components/Quiz/index.tsx b/src/components/Quiz/index.tsx index 8048397..93abcad 100644 --- a/src/components/Quiz/index.tsx +++ b/src/components/Quiz/index.tsx @@ -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'; diff --git a/src/questions/example-question-two.md b/src/questions/example-question-two.md new file mode 100644 index 0000000..156c7fe --- /dev/null +++ b/src/questions/example-question-two.md @@ -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?