Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the UI + fetch from normal qn collection #66

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getQuestionById = async (req: Request, res: Response): Promise<void

// Create a new question
export const createQuestion = async (req: Request, res: Response): Promise<void> => {
const { title, description, categories, difficulty } = req.body;
const { title, description, categories, difficulty, input1, output1, input2, output2 } = req.body;
console.log(req.body);
try {
// Check if a question with the same title already exists
Expand All @@ -41,7 +41,11 @@ export const createQuestion = async (req: Request, res: Response): Promise<void>
title,
description,
categories,
difficulty
difficulty,
input1,
output1,
input2,
output2
});
console.log(newQuestion);
// Save the new question, questionId will be auto-assigned
Expand Down
10 changes: 9 additions & 1 deletion peerprep/backend/question-service/src/models/questionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ interface IQuestion extends Document {
description: string;
categories: string[];
difficulty: string;
input1: string;
output1: string;
input2: string;
output2: string;
}

// Mongoose schema for the Question model
Expand All @@ -17,7 +21,11 @@ const questionSchema: Schema = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
categories: { type: [String], required: true }, // Array for multiple categories
difficulty: { type: String, required: true }
difficulty: { type: String, required: true },
input1: { type: String, required: true },
output1: { type: String, required: true },
input2: { type: String, required: true},
output2: { type: String, required: true }
});

// Indexing for effecient search
Expand Down
72 changes: 72 additions & 0 deletions peerprep/backend/question-service/src/sampleData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,60 @@ const sampleQuestions = [
s[i] is a printable ascii character.`,
categories: 'strings, algorithms',
difficulty: 'easy',
input1: '["h","e","l","l","o"]',
output1: '["o","l","l","e","h"]',
input2: '["H","a","n","n","a","h"]',
output2: '["h","a","n","n","a","H"]'
},
{
title: 'Linked List Cycle Detection',
description: `Implement a function to detect if a linked list contains a cycle.`,
categories: 'data-structures, algorithms',
difficulty: 'easy',
input1: 'head = [3,2,0,-4], pos = 1',
output1: 'true',
input2: 'head = [1,2], pos = 0',
output2: 'true'
},
{
title: 'Roman to Integer',
description: `Given a roman numeral, convert it to an integer.`,
categories: 'algorithms',
difficulty: 'easy',
input1: 'III',
output1: '3',
input2: 'IV',
output2: '4'
},
{
title: 'Add Binary',
description: `Given two binary strings a and b, return their sum as a binary string.`,
categories: 'bit-manipulation, algorithms',
difficulty: 'easy',
input1: 'a = "11", b = "1"',
output1: '"100"',
input2: 'a = "1010", b = "1011"',
output2: '"10101"'
},
{
title: 'Fibonacci Number',
description: `The Fibonacci numbers, commonly denoted F(n) form a sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given n, calculate F(n).`,
categories: 'recursion, algorithms',
difficulty: 'easy',
input1: '2',
output1: '1',
input2: '3',
output2: '2'
},
{
title: 'Implement Stack using Queues',
description: `Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).`,
categories: 'data-structures',
difficulty: 'easy',
input1: '["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []]',
output1: '[null, null, null, 2, 2, false]',
input2: '["MyStack", "push", "push", "push", "top", "pop", "empty"] [[], [1], [2], [3], [], [], []]',
output2: '[null, null, null, null, 3, 3, false]'
},
{
title: 'Repeated DNA Sequences',
Expand All @@ -59,6 +83,10 @@ const sampleQuestions = [
Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.`,
categories: 'algorithms, bit-manipulation',
difficulty: 'medium',
input1: '"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"',
output1: '["AAAAACCCCC","CCCCCAAAAA"]',
input2: '"AAAAAAAAAAAAA"',
output2: '["AAAAAAAAAA"]'
},
{
title: 'Course Schedule',
Expand All @@ -67,12 +95,20 @@ const sampleQuestions = [
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses. Otherwise, return false.`,
categories: 'data-structures, algorithms',
difficulty: 'medium',
input1: 'numCourses = 2, prerequisites = [[1,0]]',
output1: 'true',
input2: 'numCourses = 2, prerequisites = [[1,0],[0,1]]',
output2: 'false'
},
{
title: 'LRU Cache Design',
description: `Design and implement an LRU (Least Recently Used) cache.`,
categories: 'data-structures',
difficulty: 'medium',
input1: '["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]',
output1: '[null, null, null, 1, null, -1, null, -1, 3, 4]',
input2: '["LRUCache", "put", "put", "put", "put", "get", "get"]\n[[2], [2, 1], [1, 1], [2, 3], [4, 1], [1], [2]]',
output2: '[null, null, null, null, null, -1, 3]'
},
{
title: 'Longest Common Subsequence',
Expand All @@ -83,12 +119,20 @@ const sampleQuestions = [
For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings.`,
categories: 'strings, algorithms',
difficulty: 'medium',
input1: 'text1 = "abcde", text2 = "ace"',
output1: '3',
input2: 'text1 = "abc", text2 = "abc"',
output2: '3'
},
{
title: 'Rotate Image',
description: `You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).`,
categories: 'arrays, algorithms',
difficulty: 'medium',
input1: 'matrix = [[1,2,3],[4,5,6],[7,8,9]]',
output1: '[[7,4,1],[8,5,2],[9,6,3]]',
input2: 'matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]',
output2: '[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]'
},
{
title: 'Airplane Seat Assignment Probability',
Expand All @@ -97,30 +141,50 @@ const sampleQuestions = [
Return the probability that the nth person gets his own seat.`,
categories: 'brainteaser',
difficulty: 'medium',
input1: 'n = 1',
output1: '1.00000',
input2: 'n = 2',
output2: '0.50000'
},
{
title: 'Validate Binary Search Tree',
description: `Given the root of a binary tree, determine if it is a valid binary search tree (BST).`,
categories: 'data-structures, algorithms',
difficulty: 'medium',
input1: '[2,1,3]',
output1: 'true',
input2: '[5,1,4,null,null,3,6]',
output2: 'false'
},
{
title: 'Sliding Window Maximum',
description: `You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position, return the maximum sliding window.`,
categories: 'arrays, algorithms',
difficulty: 'hard',
input1: 'nums = [1,3,-1,-3,5,3,6,7], k = 3',
output1: '[3,3,5,5,6,7]',
input2: 'nums = [1], k = 1',
output2: '[1]'
},
{
title: 'N-Queen Problem',
description: `The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle.`,
categories: 'algorithms',
difficulty: 'hard',
input1: 'n = 4',
output1: '[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]',
input2: 'n = 1',
output2: '["Q"]'
},
{
title: 'Serialize and Deserialize a Binary Tree',
description: `Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection. Design an algorithm to serialize and deserialize a binary tree.`,
categories: 'data-structures, algorithms',
difficulty: 'hard',
input1: 'root = [1,2,3,null,null,4,5]',
output1: '[1,2,3,null,null,4,5]',
input2: 'root = []',
output2: '[]'
},
{
title: 'Wildcard Matching',
Expand All @@ -129,6 +193,10 @@ const sampleQuestions = [
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial).`,
categories: 'strings, algorithms',
difficulty: 'hard',
input1: 's = "aa", p = "a"',
output1: 'false',
input2: 's = "adceb", p = "*a*b"',
output2: 'true'
},
{
title: 'Chalkboard XOR Game',
Expand All @@ -141,6 +209,10 @@ const sampleQuestions = [
Return true if and only if Alice wins the game, assuming both players play optimally.`,
categories: 'brainteaser',
difficulty: 'hard',
input1: 'nums = [1,1,2]',
output1: 'false',
input2: 'nums = [0,1]',
output2: 'true'
}
];

Expand Down
4 changes: 4 additions & 0 deletions peerprep/frontend/src/api/questionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const validateQuestionData = (data: any): data is Question => {
typeof data._id === 'string' &&
typeof data.title === 'string' &&
typeof data.description === 'string' &&
typeof data.input1 === 'string' &&
typeof data.output1 === 'string' &&
typeof data.input2 === 'string' &&
typeof data.output2 === 'string' &&
Array.isArray(data.categories) &&
data.categories.every((cat: any) => typeof cat === 'string') &&
['easy', 'medium', 'hard'].includes(data.difficulty)
Expand Down
12 changes: 12 additions & 0 deletions peerprep/frontend/src/controllers/QuestionController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ class QuestionController {
if (!question.difficulty || !['easy', 'medium', 'hard'].includes(question.difficulty)) {
return new Error("Difficulty must be either 'easy', 'medium', or 'hard'.");
}
if (!question.input1 || question.input1.trim().length === 0) {
return new Error("Input1 is required and cannot be empty.");
}
if (!question.output1 || question.output1.trim().length === 0) {
return new Error("Output1 is required and cannot be empty.");
}
if (!question.input2 || question.input2.trim().length === 0) {
return new Error("Input2 is required and cannot be empty.");
}
if (!question.output2 || question.output2.trim().length === 0) {
return new Error("Output2 is required and cannot be empty.");
}
return null;
};

Expand Down
4 changes: 4 additions & 0 deletions peerprep/frontend/src/models/Question.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export interface Question {
categories: string[];
difficulty: string;
questionId: number;
input1: string;
output1: string;
input2: string;
output2: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { WebsocketProvider } from 'y-websocket';

import { deleteMatchedSession} from "../../api/matchingApi.ts";
import { getQuestionById } from '../../api/questionApi.ts';
import { getTestcasesByTitle, Testcase } from '../../api/testcaseApi.ts';

const CollaborationServiceIntegratedView: React.FC = () => {
const { sessionId } = useParams<{ sessionId: string; }>();
Expand All @@ -39,22 +38,14 @@ const CollaborationServiceIntegratedView: React.FC = () => {
// const [commentoutput, setCommentOutput] = useState<string | null>(null);
// console.log(commentoutput);

const [commentoutput, setCommentOutput] = useState<string | null>(null);
const [testcases, setTestcases] = useState<Testcase>({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});

console.log(commentoutput);

//let topic = 'topic';
//let difficulty = 'difficulty';
// Declare question object
//extract questionID from session id (eg. 670d81daf90653ef4b9162b8-67094dcc6be97361a2e7cb1a-1730832550120-Q672890c43266d81a769bfaee)
const [input1, setInput1] = useState<string>('N/A');
const [output1, setOutput1] = useState<string>('N/A');
const [input2, setInput2] = useState<string>('N/A');
const [output2, setOutput2] = useState<string>('N/A');
const [topics, setTopics] = useState<string>('N/A');
const [difficulty, setDifficulty] = useState<string>('N/A');
const [questionTitle, setQuestionTitle] = useState<string>('N/A');
Expand All @@ -76,6 +67,10 @@ const CollaborationServiceIntegratedView: React.FC = () => {
setDifficulty(response.difficulty); // Set difficulty from API response
setQuestionTitle(response.title);
setQuestionDescription(response.description);
setInput1(response.input1);
setOutput1(response.output1);
setInput2(response.input2);
setOutput2(response.output2);
}
} catch (error) {
console.error('Error fetching matched session:', error);
Expand Down Expand Up @@ -114,44 +109,6 @@ const CollaborationServiceIntegratedView: React.FC = () => {
}
}, [sessionId]);

useEffect(() => {
const fetchTestcases = async () => {
try {
const response = await getTestcasesByTitle(questionTitle);
if (response) {
console.log('Setting fetched testcases:', response);
setTestcases(response);
} else {
console.log('No testcases found, setting default values');
setTestcases({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});
}
} catch (error) {
console.error('Error fetching testcases:', error);
setTestcases({
questionId: 0,
title: "N/A",
input1: "N/A",
output1: "N/A",
input2: "N/A",
output2: "N/A"
});
}
};

if (questionTitle && questionTitle !== 'N/A') {
fetchTestcases();
}
}, [questionTitle]);



const handleLeaveSession = () => {
// Call the API to delete the session
try {
Expand Down Expand Up @@ -359,7 +316,6 @@ const CollaborationServiceIntegratedView: React.FC = () => {
<pre style={{ whiteSpace: 'pre-wrap', wordWrap: 'break-word' }}>{commentoutput}</pre>
</div> */}

{testcases && (
<div className="testcases-table">
<h3>Test Cases</h3>
<table>
Expand All @@ -373,15 +329,14 @@ const CollaborationServiceIntegratedView: React.FC = () => {
</thead>
<tbody>
<tr>
<td>{testcases.input1}</td>
<td>{testcases.output1}</td>
<td>{testcases.input2}</td>
<td>{testcases.output2}</td>
<td>{input1}</td>
<td>{output1}</td>
<td>{input2}</td>
<td>{output2}</td>
</tr>
</tbody>
</table>
</div>
)}
</div >
);
};
Expand Down
Loading
Loading