Skip to content
Open
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
4 changes: 3 additions & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const auth = require("./middleware/auth");
app.use(
cors({
origin: ["http://localhost:5173"],
methods: ["GET", "POST"],
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
})
);
Expand All @@ -30,11 +30,13 @@ const handleRoom = require("./socket/socketHandler");
const submissionRoutes = require("./routes/submission");
const authRoutes = require("./routes/Auth");
const dashboardRoutes = require("./routes/dashboard");
const questionRoutes = require("./routes/question");

// API Routes
app.use("/api/submissions", auth, submissionRoutes);
app.use("/api", authRoutes);
app.use("/api/dashboard", dashboardRoutes);
app.use("/api/questions", questionRoutes);

// Create HTTP server
const server = http.createServer(app);
Expand Down
70 changes: 52 additions & 18 deletions backend/routes/question.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,82 @@
// routes/questionRoutes.js

const express = require("express");
const router = express.Router();
const Question = require("../models/Question");
const runCode = require("../controllers/runCodeController");

router.post("/run", runCode);

//
// CREATE QUESTION
//
router.post("/", async (req, res) => {
try {
const question = new Question(req.body);
await question.save();
res.json(question);
} catch (err) {
res.status(500).json({ error: err.message });
}
});

//
// GET ALL QUESTIONS
//
router.get("/", async (req, res) => {
try {
const { difficulty, tag } = req.query;

let filter = {};
if (difficulty) filter.difficulty = difficulty;
if (tag) filter.tags = tag;

const questions = await Question.find(filter);
return res.json(questions || []);
} catch (err) {
return res.status(500).json({ error: err.message });
}
});

//
// GET RANDOM QUESTION
//
router.get("/random", async (req, res) => {
try {
const count = await Question.countDocuments();

if (count === 0) {
return res.status(404).json({ message: "No questions found" });
return res.json(null);
}

const random = Math.floor(Math.random() * count);
const question = await Question.findOne().skip(random);

res.json(question);
return res.json(question || null);
} catch (err) {
res.status(500).json({ error: err.message });
return res.status(500).json({ error: err.message });
}
});


//
router.get("/:id", async (req, res) => {
try {
const question = await Question.findById(req.params.id);
res.json(question);
return res.json(question || null);
} catch (err) {
res.status(500).json({ error: err.message });
return res.status(500).json({ error: err.message });
}
});

router.get("/", async (req, res) => {
const { difficulty, tag } = req.query;

let filter = {};

if (difficulty) filter.difficulty = difficulty;
if (tag) filter.tags = tag;

const questions = await Question.find(filter);

res.json(questions);
//
// DELETE
//
router.delete("/:id", async (req, res) => {
try {
await Question.findByIdAndDelete(req.params.id);
res.json({ message: "Deleted successfully" });
} catch (err) {
res.status(500).json({ error: err.message });
}
});

module.exports = router;
207 changes: 207 additions & 0 deletions backend/seed/questions.seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
const mongoose = require("mongoose");
const Question = require("../models/Question");

mongoose.connect("mongodb://localhost:27017/interviewDB");

const questions = [
// ================= EASY (1–20) =================
{
title: "Two Sum",
description: "Find two numbers that add up to target",
difficulty: "easy",
tags: ["array", "hashmap"],
constraints: "O(n)",
examples: [{ input: "nums=[2,7,11,15], target=9", output: "[0,1]", explanation: "2+7=9" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "2,7,11,15,9", output: "[0,1]" }]
},
{
title: "Reverse String",
description: "Reverse a string",
difficulty: "easy",
tags: ["string"],
constraints: "O(1) space",
examples: [{ input: "hello", output: "olleh", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "hello", output: "olleh" }]
},
{
title: "Valid Parentheses",
description: "Check valid brackets",
difficulty: "easy",
tags: ["stack"],
constraints: "O(n)",
examples: [{ input: "()[]{}", output: "true", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "()[]{}", output: "true" }]
},
{
title: "Palindrome Number",
description: "Check if number is palindrome",
difficulty: "easy",
tags: ["math"],
constraints: "O(log n)",
examples: [{ input: "121", output: "true", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "121", output: "true" }]
},
{
title: "Best Time to Buy and Sell Stock",
description: "Max profit from stock prices",
difficulty: "easy",
tags: ["array", "greedy"],
constraints: "O(n)",
examples: [{ input: "[7,1,5,3,6,4]", output: "5", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "7,1,5,3,6,4", output: "5" }]
},

// (continue EASY until 20)
{
title: "Contains Duplicate",
description: "Check duplicates in array",
difficulty: "easy",
tags: ["array"],
constraints: "O(n)",
examples: [{ input: "[1,2,3,1]", output: "true", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "1,2,3,1", output: "true" }]
},
{
title: "Single Number",
description: "Find element appearing once",
difficulty: "easy",
tags: ["bit manipulation"],
constraints: "O(n)",
examples: [{ input: "[2,2,1]", output: "1", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "2,2,1", output: "1" }]
},
{
title: "Merge Two Sorted Lists",
description: "Merge linked lists",
difficulty: "easy",
tags: ["linked list"],
constraints: "O(n)",
examples: [{ input: "1->2, 1->3", output: "1->1->2->3", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "", output: "" }]
},
{
title: "Valid Anagram",
description: "Check if strings are anagrams",
difficulty: "easy",
tags: ["string"],
constraints: "O(n)",
examples: [{ input: "anagram, nagaram", output: "true", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "anagram,nagaram", output: "true" }]
},
{
title: "Binary Search",
description: "Search element in sorted array",
difficulty: "easy",
tags: ["search"],
constraints: "O(log n)",
examples: [{ input: "[1,2,3,4], target=3", output: "2", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "1,2,3,4,3", output: "2" }]
},

// ================= MEDIUM (21–40) =================
{
title: "3Sum",
description: "Find triplets with zero sum",
difficulty: "medium",
tags: ["array", "two pointers"],
constraints: "O(n^2)",
examples: [{ input: "[-1,0,1,2,-1,-4]", output: "[[-1,-1,2],[-1,0,1]]", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "-1,0,1,2,-1,-4", output: "" }]
},
{
title: "Container With Most Water",
description: "Max water container",
difficulty: "medium",
tags: ["two pointers"],
constraints: "O(n)",
examples: [{ input: "[1,8,6,2,5,4,8,3,7]", output: "49", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "1,8,6,2,5,4,8,3,7", output: "49" }]
},
{
title: "Longest Substring Without Repeating Characters",
description: "Find longest substring",
difficulty: "medium",
tags: ["string", "sliding window"],
constraints: "O(n)",
examples: [{ input: "abcabcbb", output: "3", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "abcabcbb", output: "3" }]
},
{
title: "Group Anagrams",
description: "Group similar anagrams",
difficulty: "medium",
tags: ["hashmap"],
constraints: "O(nk log k)",
examples: [{ input: "eat,tea,tan", output: "[[eat,tea],[tan]]", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "eat,tea,tan", output: "" }]
},
{
title: "Search in Rotated Sorted Array",
description: "Search in rotated array",
difficulty: "medium",
tags: ["binary search"],
constraints: "O(log n)",
examples: [{ input: "[4,5,6,7,0,1,2], target=0", output: "4", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "4,5,6,7,0,1,2,0", output: "4" }]
},

// ================= HARD (41–50) =================
{
title: "Median of Two Sorted Arrays",
description: "Find median",
difficulty: "hard",
tags: ["binary search"],
constraints: "O(log n)",
examples: [{ input: "[1,3],[2]", output: "2", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "", output: "" }]
},
{
title: "Trapping Rain Water",
description: "Calculate trapped water",
difficulty: "hard",
tags: ["two pointers"],
constraints: "O(n)",
examples: [{ input: "[0,1,0,2]", output: "1", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "0,1,0,2", output: "1" }]
},
{
title: "Merge k Sorted Lists",
description: "Merge multiple lists",
difficulty: "hard",
tags: ["heap"],
constraints: "O(n log k)",
examples: [{ input: "k lists", output: "merged list", explanation: "" }],
starterCode: { cpp: "", java: "", python: "" },
testCases: [{ input: "", output: "" }]
}
];

const seedDB = async () => {
try {
await Question.deleteMany({});
await Question.insertMany(questions);
console.log("50 Questions seeded successfully");
mongoose.connection.close();
} catch (err) {
console.error(err);
}
};

seedDB();
Loading