Skip to content

Commit e2370aa

Browse files
committed
add question generation functionality and update types for better structure
1 parent abc2267 commit e2370aa

File tree

4 files changed

+66
-105
lines changed

4 files changed

+66
-105
lines changed

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import HistoryModal from "@/components/modals/history-modal"
1212
import LoadingQuestionModal from "@/components/modals/loading-question-modal"
1313
import { useState, useEffect, useCallback } from "react"
1414
import type { Question, UserAnswer, QuestionHistory } from "@/lib/types"
15-
import { generateRandomQuestion } from "@/lib/data"
15+
import { generateRandomQuestion } from "@/lib/question"
1616
import { useTranslation } from "@/lib/i18n"
1717
import { useToast } from "@/hooks/use-toast"
1818
// Update the onSubmit function to use the OpenAI API for evaluation

lib/data.ts

Lines changed: 0 additions & 83 deletions
This file was deleted.

lib/question.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { generateQuestion } from "./api"
2+
import {Question, QuestionCategory, QuestionDifficulty, QuestionType} from "./types"
3+
4+
// In-memory storage for generated questions
5+
let generatedQuestions: Question[] = []
6+
7+
export async function generateRandomQuestion(): Promise<Question> {
8+
const type = Object.keys(QuestionType).filter(key => isNaN(Number(key))) as QuestionType[]
9+
const category =Object.keys(QuestionCategory).filter(key => isNaN(Number(key))) as QuestionCategory[]
10+
const difficulty = Object.keys(QuestionDifficulty).filter(key => isNaN(Number(key))) as QuestionDifficulty[]
11+
const randomType = type[Math.floor(Math.random() * type.length)]
12+
const randomCategory = category[Math.floor(Math.random() * category.length)]
13+
const randomDifficulty = difficulty[Math.floor(Math.random() * difficulty.length)]
14+
return await generateQuestion(randomType, randomCategory, randomDifficulty)
15+
}
16+
17+
// Add a new function to add a generated question to our collection
18+
export function addGeneratedQuestion(question: Question): void {
19+
generatedQuestions.push(question)
20+
}

lib/types.ts

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11
export interface Question {
2-
id: string
3-
type: "Coding" | "Question"
4-
category: string
5-
difficulty: "Easy" | "Medium" | "Hard"
6-
translations: {
7-
[key: string]: {
8-
title: string
9-
description: string
10-
topic: string
2+
id: string
3+
type: QuestionType
4+
category: string
5+
difficulty: "Easy" | "Medium" | "Hard"
6+
translations: {
7+
[key: string]: {
8+
title: string
9+
description: string
10+
topic: string
11+
}
1112
}
12-
}
13-
testCases?: Array<{
14-
input: any
15-
output: any
16-
}>
13+
testCases?: Array<{
14+
input: any
15+
output: any
16+
}>
17+
}
18+
19+
export enum QuestionType {
20+
Coding = "Coding",
21+
Question = "Question"
22+
}
23+
24+
export enum QuestionDifficulty {
25+
Easy = "Easy",
26+
Medium = "Medium",
27+
Hard = "Hard"
28+
}
29+
30+
export enum QuestionCategory {
31+
Algorithms = "Algorithms",
32+
TCP = "TCP",
33+
UDP = "UDP",
34+
DataStructures = "Data Structures",
35+
LeetCode = "LeetCode",
36+
Java = "Java",
37+
SystemDesign = "System Design",
38+
Behavioral = "Behavioral",
39+
OperationSystem = "Operation System",
40+
Spring = "Spring"
1741
}
1842

1943
export interface UserAnswer {
20-
content: string
44+
content: string
2145
}
2246

2347
export interface QuestionHistory {
24-
id: string
25-
title: string
26-
timestamp: string
27-
answered: boolean
28-
language: string
29-
question: Question
48+
id: string
49+
title: string
50+
timestamp: string
51+
answered: boolean
52+
language: string
53+
question: Question
3054
}
3155

0 commit comments

Comments
 (0)