Skip to content

Commit 3079541

Browse files
committed
More questions and answers
1 parent ffd87c8 commit 3079541

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Question Service Seed
2+
3+
Questions, test cases, code templates, and solutions are generated using ChatGPT.
4+
5+
Disclaimer: They may not be fully accurate.
6+

backend/question-service/src/scripts/seed.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ export async function seedQuestions() {
130130
javaTemplate: `import java.util.*;\n\npublic class Main {\n // Please do not modify the main function\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n String s = scanner.nextLine().trim();\n int numRows = Integer.parseInt(scanner.nextLine().trim());\n System.out.println(solution(s, numRows));\n }\n\n // Write your code here\n public static String solution(String s, int numRows) {\n // Implement your solution here\n return "";\n }\n}`,
131131
cTemplate: `#include <stdio.h>\n#include <string.h>\n\n// Function to implement\nconst char* solution(const char* s, int numRows) {\n // Implement your solution here\n return "";\n}\n\n// Please do not modify the main function\nint main() {\n char s[1000];\n int numRows;\n fgets(s, sizeof(s), stdin);\n s[strcspn(s, "\\n")] = 0; // Remove newline\n scanf("%d", &numRows);\n printf("%s\\n", solution(s, numRows));\n return 0;\n}`,
132132
},
133+
{
134+
title: "Reverse Integer",
135+
description:
136+
"Given a signed 32-bit integer `x`, return `x` with its digits reversed.\n\n" +
137+
"If reversing `x` causes the value to go outside the signed 32-bit integer range `[-2^31, 2^31 - 1]`, then return 0.\n\n" +
138+
"### Input\n" +
139+
"Each test case consists of a single integer `x`.\n\n" +
140+
"### Output\n" +
141+
"For each test case, output a single integer which is the reversed integer, or 0 if the result overflows.\n\n" +
142+
"### Explanation\n" +
143+
"- **Test Case 1**: `x = 123` reverses to `321`.\n" +
144+
"- **Test Case 2**: `x = -123` reverses to `-321`.\n" +
145+
"- **Test Case 3**: `x = 120` reverses to `21`.\n",
146+
complexity: "Easy",
147+
category: ["Strings"],
148+
testcaseInputFileUrl: "./src/scripts/testcases/reverseIntegerInput.txt",
149+
testcaseOutputFileUrl: "./src/scripts/testcases/reverseIntegerOutput.txt",
150+
pythonTemplate: `# Please do not modify the main function\ndef main():\n\tx = int(input().strip())\n\tprint(solution(x))\n\n\n# Write your code here\ndef solution(x):\n\t# Implement your solution here\n\treturn 0\n\n\nif __name__ == "__main__":\n\tmain()\n`,
151+
javaTemplate: `import java.util.Scanner;\n\npublic class Main {\n // Please do not modify the main function\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n int x = scanner.nextInt();\n System.out.println(solution(x));\n }\n\n // Write your code here\n public static int solution(int x) {\n // Implement your solution here\n return 0;\n }\n}`,
152+
cTemplate: `#include <stdio.h>\n#include <limits.h>\n\n// Function to implement\nint solution(int x) {\n // Implement your solution here\n return 0;\n}\n\n// Please do not modify the main function\nint main() {\n int x;\n scanf("%d", &x);\n printf("%d\\n", solution(x));\n return 0;\n}`,
153+
},
133154
];
134155

135156
try {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Please do not modify the main function
2+
def main():
3+
s = input().strip()
4+
print(solution(s))
5+
6+
7+
# Write your code here
8+
def solution(s):
9+
# Helper function to expand around the center and find palindromic substrings
10+
def expand_around_center(left, right):
11+
while left >= 0 and right < len(s) and s[left] == s[right]:
12+
left -= 1
13+
right += 1
14+
# Return the longest palindrome substring from the current center
15+
return s[left + 1 : right]
16+
17+
longest_palindrome = ""
18+
19+
for i in range(len(s)):
20+
# Check for odd-length palindromes
21+
odd_palindrome = expand_around_center(i, i)
22+
# Check for even-length palindromes
23+
even_palindrome = expand_around_center(i, i + 1)
24+
25+
# Update the longest palindrome if a longer one is found
26+
longest_palindrome = max(
27+
longest_palindrome, odd_palindrome, even_palindrome, key=len
28+
)
29+
30+
return longest_palindrome
31+
32+
33+
if __name__ == "__main__":
34+
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Please do not modify the main function
2+
def main():
3+
s = input().strip()
4+
print(solution(s))
5+
6+
7+
# Write your code here
8+
def solution(s):
9+
char_map = {}
10+
max_len = 0
11+
start = 0
12+
13+
for i, char in enumerate(s):
14+
# If the character is already in the substring, update the start position
15+
if char in char_map and char_map[char] >= start:
16+
start = char_map[char] + 1
17+
# Update the character's latest position
18+
char_map[char] = i
19+
# Update the max length if the current substring length is greater
20+
max_len = max(max_len, i - start + 1)
21+
22+
return max_len
23+
24+
25+
if __name__ == "__main__":
26+
main()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Please do not modify the main function
2+
def main():
3+
import sys
4+
5+
input = sys.stdin.read().strip().split("\n\n")
6+
for case in input:
7+
lines = case.split("\n")
8+
nums1 = list(map(int, lines[0].split()))
9+
nums2 = list(map(int, lines[1].split()))
10+
print(f"{solution(nums1, nums2):.1f}")
11+
12+
13+
# Write your code here
14+
def solution(nums1, nums2):
15+
# Merge the two sorted arrays
16+
merged = sorted(nums1 + nums2)
17+
n = len(merged)
18+
19+
# Calculate the median
20+
if n % 2 == 1: # Odd number of elements
21+
return merged[n // 2]
22+
else: # Even number of elements
23+
return (merged[n // 2 - 1] + merged[n // 2]) / 2
24+
25+
26+
if __name__ == "__main__":
27+
main()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Please do not modify the main function
2+
def main():
3+
x = int(input().strip())
4+
print(solution(x))
5+
6+
7+
# Write your code here
8+
def solution(x):
9+
# Define the 32-bit integer boundaries
10+
INT_MIN, INT_MAX = -(2**31), 2**31 - 1
11+
12+
# Check if x is negative and handle reversal accordingly
13+
sign = -1 if x < 0 else 1
14+
x *= sign
15+
16+
# Reverse the integer by converting to string, reversing, and converting back to int
17+
reversed_x = int(str(x)[::-1]) * sign
18+
19+
# Check for overflow
20+
if reversed_x < INT_MIN or reversed_x > INT_MAX:
21+
return 0
22+
23+
return reversed_x
24+
25+
26+
if __name__ == "__main__":
27+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
123
2+
3+
-123
4+
5+
120
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
321
2+
3+
-321
4+
5+
21

0 commit comments

Comments
 (0)