Skip to content

Commit 273e2ff

Browse files
committed
Add seed for qns
1 parent 48bfd05 commit 273e2ff

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

backend/question-service/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"main": "server.ts",
55
"type": "module",
66
"scripts": {
7+
"seed": "tsx src/scripts/seed.ts",
78
"start": "tsx server.ts",
89
"dev": "tsx watch server.ts",
9-
"test": "set NODE_ENV=test && jest",
10+
"test": "export NODE_ENV=test && jest",
1011
"test:watch": "export NODE_ENV=test && jest --watch",
1112
"lint": "eslint ."
1213
},
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { exit } from "process";
2+
import connectDB from "../../config/db";
3+
import Question from "../models/Question";
4+
5+
export async function seedQuestions() {
6+
await connectDB();
7+
8+
const questions = [
9+
{
10+
title: "Serialize and Deserialize Binary Tree",
11+
description:
12+
"Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. \n\n![image](https://firebasestorage.googleapis.com/v0/b/peerprep-c3bd1.appspot.com/o/07148757-21b2-4c20-93e0-d8bef1b3560d?alt=media)",
13+
complexity: "Hard",
14+
category: ["Tree", "Design"],
15+
},
16+
{
17+
title: "Two Sum",
18+
description:
19+
"Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. You may assume that each input would have **exactly one solution**, and you may not use the same element twice. You can return the answer in any order.",
20+
complexity: "Easy",
21+
category: ["Array", "Hash Table"],
22+
},
23+
{
24+
title: "Add Two Numbers",
25+
description:
26+
"You are given two non-empty linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.",
27+
complexity: "Medium",
28+
category: ["Linked List", "Math"],
29+
},
30+
{
31+
title: "Longest Substring Without Repeating Characters",
32+
description:
33+
"Given a string `s`, find the length of the **longest substring** without repeating characters.",
34+
complexity: "Medium",
35+
category: ["Hash Table", "Two Pointers", "String", "Sliding Window"],
36+
},
37+
{
38+
title: "Median of Two Sorted Arrays",
39+
description:
40+
"Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.",
41+
complexity: "Hard",
42+
category: ["Array", "Binary Search", "Divide and Conquer"],
43+
},
44+
{
45+
title: "Longest Palindromic Substring",
46+
description:
47+
"Given a string `s`, return the **longest palindromic substring** in `s`.",
48+
complexity: "Medium",
49+
category: ["String", "Dynamic Programming"],
50+
},
51+
{
52+
title: "ZigZag Conversion",
53+
description:
54+
"The string `PAYPALISHIRING` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: `PAHNAPLSIIGYIR` Write the code that will take a string and make this conversion given a number of rows.",
55+
complexity: "Medium",
56+
category: ["String"],
57+
},
58+
{
59+
title: "Reverse Integer",
60+
description:
61+
"Given a signed 32-bit integer `x`, return `x` with its digits reversed. If reversing `x` causes the value to go outside the signed 32-bit integer range `[-2^31, 2^31 - 1]`, then return 0.",
62+
complexity: "Easy",
63+
category: ["Math"],
64+
},
65+
{
66+
title: "String to Integer (atoi)",
67+
description:
68+
"Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function).",
69+
complexity: "Medium",
70+
category: ["Math", "String"],
71+
},
72+
{
73+
title: "Palindrome Number",
74+
description:
75+
"Given an integer `x`, return `true` if `x` is a palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, `121` is palindrome while `123` is not.",
76+
complexity: "Easy",
77+
category: ["Math"],
78+
},
79+
{
80+
title: "Regular Expression Matching",
81+
description:
82+
"Given an input string `s` and a pattern `p`, implement regular expression matching with support for `'.'` and `'*'` where: - `'.'` Matches any single character.​​​​ - `'*'` Matches zero or more of the preceding element.",
83+
complexity: "Hard",
84+
category: ["String", "Dynamic Programming", "Backtracking"],
85+
},
86+
{
87+
title: "Container With Most Water",
88+
description:
89+
"Given `n` non-negative integers `a1, a2, ..., an`, where each represents a point at coordinate `(i, ai)`. `n` vertical lines are drawn such that the two endpoints of the line `i` is at `(i, ai)` and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.",
90+
complexity: "Medium",
91+
category: ["Array", "Two Pointers"],
92+
},
93+
{
94+
title: "Integer to Roman",
95+
description:
96+
"Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`. Given an integer, convert it to a roman numeral.",
97+
complexity: "Medium",
98+
category: ["Math", "String"],
99+
},
100+
{
101+
title: "Roman to Integer",
102+
description:
103+
"Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`. Given a roman numeral, convert it to an integer.",
104+
complexity: "Easy",
105+
category: ["Math", "String"],
106+
},
107+
];
108+
109+
try {
110+
for (const qn of questions) {
111+
const existingQn = await Question.findOne({ title: qn.title });
112+
if (existingQn) {
113+
continue;
114+
}
115+
await Question.create(qn);
116+
}
117+
console.log("Questions seeded successfully.");
118+
} catch {
119+
console.error("Error creating questions.");
120+
}
121+
exit();
122+
}
123+
124+
seedQuestions();

0 commit comments

Comments
 (0)