-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple correct answers
- Loading branch information
Showing
8 changed files
with
90 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { ParsedQuestion } from './markdownParser'; | ||
|
||
export function isCorrectAnswer(question: ParsedQuestion, selectedIndex: number): boolean { | ||
if (question.correctAnswer !== undefined) { | ||
return selectedIndex + 1 === question.correctAnswer; | ||
} | ||
if (question.correctAnswers !== undefined) { | ||
return question.correctAnswers.includes(selectedIndex + 1); | ||
} | ||
throw new Error('Question has no correct answer defined'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,28 @@ | ||
--- | ||
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) | ||
title: The Integration Inquisitor | ||
description: The Integration Inquisitor will test your knowledge of posting compositions to an OpenEHR system. | ||
level: 2 | ||
correctAnswers: [2, 4] | ||
timeLimit: 5 | ||
--- | ||
|
||
## 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. | ||
Which HTTP method and content type should you use when posting a composition to an OpenEHR system? | ||
|
||
## 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) | ||
``` | ||
- GET request with application/json content type | ||
- POST request with application/xml content type | ||
- PUT request with text/plain content type | ||
- POST request with application/json content type | ||
|
||
## 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. | ||
When posting a new composition to an OpenEHR system, you typically use a POST request with application/json or application/xml content type. This allows you to send structured data in a format that's widely supported and easy to work with. | ||
|
||
## 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? | ||
Think about which HTTP method is typically used for creating new resources. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters