Skip to content

Commit ead61df

Browse files
committed
More answers
1 parent 3079541 commit ead61df

File tree

7 files changed

+153
-1
lines changed

7 files changed

+153
-1
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function seedQuestions() {
5454
description:
5555
"Given a string `s`, find the length of the **longest substring** without repeating characters.",
5656
complexity: "Medium",
57-
category: ["Strings"],
57+
category: ["Strings", "Algorithms"],
5858
testcaseInputFileUrl: "./src/scripts/testcases/longestSubstringInput.txt",
5959
testcaseOutputFileUrl:
6060
"./src/scripts/testcases/longestSubstringOutput.txt",
@@ -151,6 +151,45 @@ export async function seedQuestions() {
151151
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}`,
152152
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}`,
153153
},
154+
{
155+
title: "Add Binary",
156+
description:
157+
"Given two binary strings `a` and `b`, return their sum as a binary string.\n\n" +
158+
"Each test case consists of two lines:\n" +
159+
"- The first line contains the binary string `a`.\n" +
160+
"- The second line contains the binary string `b`.\n\n" +
161+
"### Explanation\n" +
162+
'- **Test Case 1**: `a = "11"` and `b = "1"`, the sum is `"100"`.\n' +
163+
'- **Test Case 2**: `a = "1010"` and `b = "1011"`, the sum is `"10101"`.',
164+
complexity: "Easy",
165+
category: ["Data Structures", "Strings"],
166+
testcaseInputFileUrl: "./src/scripts/testcases/addBinaryInput.txt",
167+
testcaseOutputFileUrl: "./src/scripts/testcases/addBinaryOutput.txt",
168+
pythonTemplate: `# Please do not modify the main function\ndef main():\n\ta = input().strip()\n\tb = input().strip()\n\tprint(solution(a, b))\n\n\n# Write your code here\ndef solution(a, b):\n\t# Implement your solution here\n\treturn ""\n\n\nif __name__ == "__main__":\n\tmain()\n`,
169+
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 String a = scanner.nextLine().trim();\n String b = scanner.nextLine().trim();\n System.out.println(solution(a, b));\n }\n\n // Write your code here\n public static String solution(String a, String b) {\n // Implement your solution here\n return "";\n }\n}`,
170+
cTemplate: `#include <stdio.h>\n#include <string.h>\n\n// Function to implement\nconst char* solution(const char* a, const char* b) {\n // Implement your solution here\n return "";\n}\n\n// Please do not modify the main function\nint main() {\n char a[1000], b[1000];\n fgets(a, sizeof(a), stdin);\n fgets(b, sizeof(b), stdin);\n // Remove newline from input if exists\n a[strcspn(a, "\\n")] = 0;\n b[strcspn(b, "\\n")] = 0;\n printf("%s\\n", solution(a, b));\n return 0;\n}`,
171+
},
172+
{
173+
title: "Binary Tree Maximum Path Sum",
174+
description:
175+
"Given the `root` of a binary tree, return the **maximum path sum** of any **non-empty path**.\n\n" +
176+
"A path in a binary tree is defined as a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them.\n" +
177+
"A node can only appear once in the path, and the path does not necessarily need to pass through the root.\n\n" +
178+
"### Explanation\n" +
179+
"- ![image](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)\n" +
180+
"- **Test Case 1**: Given the tree `[1, 2, 3]`, the maximum path sum is `6` (2 → 1 → 3).\n\n\n" +
181+
"- ![image](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)\n" +
182+
"- **Test Case 2**: Given the tree `[-10, 9, 20, null, null, 15, 7]`, the maximum path sum is `42` (15 → 20 → 7).",
183+
complexity: "Hard",
184+
category: ["Tree", "Dynamic Programming", "Recursion"],
185+
testcaseInputFileUrl:
186+
"./src/scripts/testcases/binaryTreeMaxPathSumInput.txt",
187+
testcaseOutputFileUrl:
188+
"./src/scripts/testcases/binaryTreeMaxPathSumOutput.txt",
189+
pythonTemplate: `# Definition for a binary tree node.\nclass TreeNode:\n\tdef __init__(self, val=0, left=None, right=None):\n\t\tself.val = val\n\t\tself.left = left\n\t\tself.right = right\n\n# Please do not modify the main function\ndef main():\n\troot = deserialize(input().strip())\n\tprint(solution(root))\n\n\n# Write your code here\ndef solution(root):\n\t# Implement your solution here\n\treturn 0\n\n\n# Helper function to deserialize input\n\ndef deserialize(data):\n\t# Implement a function to build a tree from input\n\treturn None\n\n\nif __name__ == "__main__":\n\tmain()\n`,
190+
javaTemplate: `import java.util.*;\n\nclass TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\tTreeNode(int val) { this.val = val; }\n\tTreeNode(int val, TreeNode left, TreeNode right) {\n\t\tthis.val = val;\n\t\tthis.left = left;\n\t\tthis.right = right;\n\t}\n}\n\npublic class Main {\n\t// Please do not modify the main function\n\tpublic static void main(String[] args) {\n\t\tTreeNode root = deserialize(new Scanner(System.in).nextLine().trim());\n\t\tSystem.out.println(solution(root));\n\t}\n\n\t// Write your code here\n\tpublic static int solution(TreeNode root) {\n\t\t// Implement your solution here\n\t\treturn 0;\n\t}\n\n\t// Helper function to deserialize input\n\tpublic static TreeNode deserialize(String data) {\n\t\t// Implement a function to build a tree from input\n\t\treturn null;\n\t}\n}`,
191+
cTemplate: `#include <stdio.h>\n#include <stdlib.h>\n\nstruct TreeNode {\n\tint val;\n\tstruct TreeNode *left;\n\tstruct TreeNode *right;\n};\n\n// Function to implement\nint solution(struct TreeNode* root) {\n\t// Implement your solution here\n\treturn 0;\n}\n\n// Please do not modify the main function\nint main() {\n\t// Implement tree deserialization if needed\n\treturn 0;\n}`,
192+
},
154193
];
155194

156195
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+
a = input().strip()
4+
b = input().strip()
5+
print(solution(a, b))
6+
7+
8+
# Write your code here
9+
def solution(a, b):
10+
# Make sure a is the longer string
11+
if len(b) > len(a):
12+
a, b = b, a
13+
14+
# Initialize result and carry
15+
result = []
16+
carry = 0
17+
b = b.zfill(len(a)) # Pad the shorter string with leading zeros
18+
19+
# Add binary numbers from the end to the start
20+
for i in range(len(a) - 1, -1, -1):
21+
sum = int(a[i]) + int(b[i]) + carry
22+
result.append(str(sum % 2)) # Append the current binary digit
23+
carry = sum // 2 # Calculate the carry
24+
25+
# If there’s a carry left after the final addition, add it
26+
if carry:
27+
result.append("1")
28+
29+
# Join and reverse result since we appended in reverse order
30+
return "".join(result[::-1])
31+
32+
33+
if __name__ == "__main__":
34+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
11
2+
1
3+
4+
1010
5+
1011
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
100
2+
3+
10101
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
# Please do not modify the main function
10+
def main():
11+
root = deserialize(input().strip())
12+
print(solution(root))
13+
14+
15+
# Write your code here
16+
def solution(root):
17+
def max_gain(node):
18+
nonlocal max_sum
19+
if not node:
20+
return 0
21+
22+
# Recursively calculate the maximum path sum of left and right subtrees
23+
left_gain = max(max_gain(node.left), 0) # Ignore negative paths
24+
right_gain = max(max_gain(node.right), 0) # Ignore negative paths
25+
26+
# Current node max path sum includes both left and right contributions
27+
current_path_sum = node.val + left_gain + right_gain
28+
29+
# Update the maximum path sum found so far
30+
max_sum = max(max_sum, current_path_sum)
31+
32+
# Return the max gain if we continue the same path with this node
33+
return node.val + max(left_gain, right_gain)
34+
35+
max_sum = float("-inf")
36+
max_gain(root)
37+
return max_sum
38+
39+
40+
# Helper function to deserialize input in space-separated format
41+
def deserialize(data):
42+
if not data:
43+
return None
44+
45+
nodes = data.split()
46+
root = TreeNode(int(nodes[0]))
47+
queue = [root]
48+
i = 1
49+
50+
while queue and i < len(nodes):
51+
node = queue.pop(0)
52+
if nodes[i] != "null":
53+
node.left = TreeNode(int(nodes[i]))
54+
queue.append(node.left)
55+
i += 1
56+
if i < len(nodes) and nodes[i] != "null":
57+
node.right = TreeNode(int(nodes[i]))
58+
queue.append(node.right)
59+
i += 1
60+
61+
return root
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 2 3
2+
3+
-10 9 20 null null 15 7
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
6
2+
3+
42

0 commit comments

Comments
 (0)