File size: 68,470 Bytes
f91a684 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 | export interface CodingQuestion {
id: string;
title: string;
difficulty: 'Easy' | 'Medium' | 'Hard';
category: string;
companies?: string[];
description: string;
starterCode: {
javascript: string;
python: string;
cpp: string;
java: string;
go: string;
};
testCases: {
input: string;
output: string;
params: any[];
expected: any;
}[];
hiddenTestCases: {
input: string;
output: string;
params: any[];
expected: any;
}[];
solution: {
javascript: string;
python: string;
cpp: string;
java: string;
go: string;
explanation: string;
};
}
export const codingQuestions: CodingQuestion[] = [
{
id: 'two-sum',
title: 'Pair Sum Target',
difficulty: 'Easy',
category: 'Array',
description: `Given a collection of integers \`nums\` and a specific \`target\` value, identify the indices of two numbers that combine to reach the \`target\`.
Each input is guaranteed to have exactly one solution, and you cannot use the same element twice.
The order of indices in the result does not matter.`,
starterCode: {
javascript: `/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
};`,
python: `class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
pass`,
cpp: `class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};`,
java: `class Solution {
public int[] twoSum(int[] nums, int target) {
}
}`,
go: `func twoSum(nums []int, target int) []int {
}`
},
testCases: [
{ input: 'nums = [2,7,11,15], target = 9', output: '[0,1]', params: [[2,7,11,15], 9], expected: [0,1] },
{ input: 'nums = [3,2,4], target = 6', output: '[1,2]', params: [[3,2,4], 6], expected: [1,2] }
],
hiddenTestCases: [
{ input: 'nums = [3,3], target = 6', output: '[0,1]', params: [[3,3], 6], expected: [0,1] },
{ input: 'nums = [-1,-2,-3,-4,-5], target = -8', output: '[2,4]', params: [[-1,-2,-3,-4,-5], -8], expected: [2,4] }
],
solution: {
javascript: `var twoSum = function(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
};`,
python: `class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
prevMap = {} # val : index
for i, n in enumerate(nums):
diff = target - n
if diff in prevMap:
return [prevMap[diff], i]
prevMap[n] = i`,
cpp: `class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> prevMap;
for (int i = 0; i < nums.size(); i++) {
int diff = target - nums[i];
if (prevMap.find(diff) != prevMap.end()) {
return {prevMap[diff], i};
}
prevMap[nums[i]] = i;
}
return {};
}
};`,
java: `class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> prevMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (prevMap.containsKey(diff)) {
return new int[] { prevMap.get(diff), i };
}
prevMap.put(nums[i], i);
}
return new int[] {};
}
}`,
go: `func twoSum(nums []int, target int) []int {
prevMap := make(map[int]int)
for i, n := range nums {
diff := target - n
if idx, ok := prevMap[diff]; ok {
return []int{idx, i}
}
prevMap[n] = i
}
return nil
}`,
explanation: "Use a hash map to store the indices of numbers we've seen so far. For each number, check if its complement (target - current) exists in the map."
}
},
{
id: 'stock-profit',
title: 'Optimal Stock Trading',
difficulty: 'Easy',
category: 'Array',
description: `You are given an array \`prices\` where \`prices[i]\` is the price of a given stock on the \`i-th\` day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.`,
starterCode: {
javascript: `/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
};`,
python: `class Solution:
def maxProfit(self, prices: List[int]) -> int:
pass`,
cpp: `class Solution {
public:
int maxProfit(vector<int>& prices) {
}
};`,
java: `class Solution {
public int maxProfit(int[] prices) {
}
}`,
go: `func maxProfit(prices []int) int {
}`
},
testCases: [
{ input: 'prices = [7,1,5,3,6,4]', output: '5', params: [[7,1,5,3,6,4]], expected: 5 },
{ input: 'prices = [7,6,4,3,1]', output: '0', params: [[7,6,4,3,1]], expected: 0 }
],
hiddenTestCases: [
{ input: 'prices = [1,2]', output: '1', params: [[1,2]], expected: 1 },
{ input: 'prices = [2,4,1]', output: '2', params: [[2,4,1]], expected: 2 }
],
solution: {
javascript: `var maxProfit = function(prices) {
let minPrice = Infinity;
let maxProfit = 0;
for (let price of prices) {
if (price < minPrice) minPrice = price;
else if (price - minPrice > maxProfit) maxProfit = price - minPrice;
}
return maxProfit;
};`,
python: `class Solution:
def maxProfit(self, prices: List[int]) -> int:
res = 0
lowest = prices[0]
for price in prices:
if price < lowest:
lowest = price
res = max(res, price - lowest)
return res`,
cpp: `class Solution {
public:
int maxProfit(vector<int>& prices) {
int l = 0, r = 1;
int maxP = 0;
while (r < prices.size()) {
if (prices[l] < prices[r]) {
int profit = prices[r] - prices[l];
maxP = max(maxP, profit);
} else {
l = r;
}
r++;
}
return maxP;
}
};`,
java: `class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price : prices) {
if (price < minPrice) minPrice = price;
else if (price - minPrice > maxProfit) maxProfit = price - minPrice;
}
return maxProfit;
}
}`,
go: `func maxProfit(prices []int) int {
minPrice := 1000000000
maxProfit := 0
for _, price := range prices {
if price < minPrice {
minPrice = price
} else if price - minPrice > maxProfit {
maxProfit = price - minPrice
}
}
return maxProfit
}`,
explanation: "Keep track of the minimum price seen so far and calculate the potential profit at each step. Update the maximum profit if the current profit is higher."
}
},
{
id: 'contains-duplicate',
title: 'Duplicate Finder',
difficulty: 'Easy',
category: 'Array',
description: `Given an integer array \`nums\`, return \`true\` if any value appears at least twice in the array, and return \`false\` if every element is distinct.`,
starterCode: {
javascript: `/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
};`,
python: `class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
pass`,
cpp: `class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
}
};`,
java: `class Solution {
public boolean containsDuplicate(int[] nums) {
}
}`,
go: `func containsDuplicate(nums []int) bool {
}`
},
testCases: [
{ input: 'nums = [1,2,3,1]', output: 'true', params: [[1,2,3,1]], expected: true },
{ input: 'nums = [1,2,3,4]', output: 'false', params: [[1,2,3,4]], expected: false }
],
hiddenTestCases: [
{ input: 'nums = [1,1,1,3,3,4,3,2,4,2]', output: 'true', params: [[1,1,1,3,3,4,3,2,4,2]], expected: true },
{ input: 'nums = []', output: 'false', params: [[]], expected: false }
],
solution: {
javascript: `var containsDuplicate = function(nums) {
const set = new Set(nums);
return set.size !== nums.length;
};`,
python: `class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
hashset = set()
for n in nums:
if n in hashset:
return True
hashset.add(n)
return False`,
cpp: `class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int n : nums) {
if (s.count(n)) return true;
s.insert(n);
}
return false;
}
};`,
java: `class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int n : nums) {
if (set.contains(n)) return true;
set.add(n);
}
return false;
}
}`,
go: `func containsDuplicate(nums []int) bool {
hashset := make(map[int]bool)
for _, n := range nums {
if hashset[n] {
return true
}
hashset[n] = true
}
return false
}`,
explanation: "Use a hash set to keep track of elements already seen. If an element is already in the set, a duplicate exists."
}
},
{
id: 'product-except-self',
title: 'Array Product Exclusion',
difficulty: 'Medium',
category: 'Array',
description: `Given an integer array \`nums\`, return an array \`answer\` such that \`answer[i]\` is equal to the product of all the elements of \`nums\` except \`nums[i]\`.
The product of any prefix or suffix of \`nums\` is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.`,
starterCode: {
javascript: `/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
};`,
python: `class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
pass`,
cpp: `class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
}
};`,
java: `class Solution {
public int[] productExceptSelf(int[] nums) {
}
}`,
go: `func productExceptSelf(nums []int) []int {
}`
},
testCases: [
{ input: 'nums = [1,2,3,4]', output: '[24,12,8,6]', params: [[1,2,3,4]], expected: [24,12,8,6] },
{ input: 'nums = [-1,1,0,-3,3]', output: '[0,0,9,0,0]', params: [[-1,1,0,-3,3]], expected: [0,0,9,0,0] }
],
hiddenTestCases: [
{ input: 'nums = [0,0]', output: '[0,0]', params: [[0,0]], expected: [0,0] },
{ input: 'nums = [1,0]', output: '[0,1]', params: [[1,0]], expected: [0,1] }
],
solution: {
javascript: `var productExceptSelf = function(nums) {
const res = new Array(nums.length).fill(1);
let prefix = 1;
for (let i = 0; i < nums.length; i++) {
res[i] = prefix;
prefix *= nums[i];
}
let postfix = 1;
for (let i = nums.length - 1; i >= 0; i--) {
res[i] *= postfix;
postfix *= nums[i];
}
return res;
};`,
python: `class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1] * (len(nums))
prefix = 1
for i in range(len(nums)):
res[i] = prefix
prefix *= nums[i]
postfix = 1
for i in range(len(nums) - 1, -1, -1):
res[i] *= postfix
postfix *= nums[i]
return res`,
cpp: `class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, 1);
int prefix = 1;
for (int i = 0; i < n; i++) {
res[i] = prefix;
prefix *= nums[i];
}
int postfix = 1;
for (int i = n - 1; i >= 0; i--) {
res[i] *= postfix;
postfix *= nums[i];
}
return res;
}
};`,
java: `class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] res = new int[n];
res[0] = 1;
for (int i = 1; i < n; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = n - 1; i >= 0; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}`,
go: `func productExceptSelf(nums []int) []int {
n := len(nums)
res := make([]int, n)
res[0] = 1
for i := 1; i < n; i++ {
res[i] = res[i-1] * nums[i-1]
}
right := 1
for i := n - 1; i >= 0; i-- {
res[i] *= right
right *= nums[i]
}
return res
}`,
explanation: "Calculate prefix products in one pass and suffix products in another pass. The result for each index is the product of its prefix and suffix."
}
},
{
id: 'max-subarray',
title: 'Maximum Sum Segment',
difficulty: 'Medium',
category: 'Array',
description: `Given an integer array \`nums\`, find the subarray with the largest sum, and return its sum.`,
starterCode: {
javascript: `/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
};`,
python: `class Solution:
def maxSubArray(self, nums: List[int]) -> int:
pass`,
cpp: `class Solution {
public:
int maxSubArray(vector<int>& nums) {
}
};`,
java: `class Solution {
public int maxSubArray(int[] nums) {
}
}`,
go: `func maxSubArray(nums []int) int {
}`
},
testCases: [
{ input: 'nums = [-2,1,-3,4,-1,2,1,-5,4]', output: '6', params: [[-2,1,-3,4,-1,2,1,-5,4]], expected: 6 },
{ input: 'nums = [1]', output: '1', params: [[1]], expected: 1 }
],
hiddenTestCases: [
{ input: 'nums = [5,4,-1,7,8]', output: '23', params: [[5,4,-1,7,8]], expected: 23 },
{ input: 'nums = [-1]', output: '-1', params: [[-1]], expected: -1 }
],
solution: {
javascript: `var maxSubArray = function(nums) {
let maxSub = nums[0];
let curSum = 0;
for (let n of nums) {
if (curSum < 0) curSum = 0;
curSum += n;
maxSub = Math.max(maxSub, curSum);
}
return maxSub;
};`,
python: `class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxSub = nums[0]
curSum = 0
for n in nums:
if curSum < 0:
curSum = 0
curSum += n
maxSub = max(maxSub, curSum)
return maxSub`,
cpp: `class Solution {
public:
int maxSubArray(vector<int>& nums) {
int maxSub = nums[0];
int curSum = 0;
for (int n : nums) {
if (curSum < 0) curSum = 0;
curSum += n;
maxSub = max(maxSub, curSum);
}
return maxSub;
}
};`,
java: `class Solution {
public int maxSubArray(int[] nums) {
int maxSub = nums[0];
int curSum = 0;
for (int n : nums) {
if (curSum < 0) curSum = 0;
curSum += n;
maxSub = Math.max(maxSub, curSum);
}
return maxSub;
}
}`,
go: `func maxSubArray(nums []int) int {
maxSub := nums[0]
curSum := 0
for _, n := range nums {
if curSum < 0 {
curSum = 0
}
curSum += n
if curSum > maxSub {
maxSub = curSum
}
}
return maxSub
}`,
explanation: "Use Kadane's Algorithm: Iterate through the array, keeping track of the current sum. If the current sum becomes negative, reset it to zero. The maximum sum encountered during the process is the result."
}
},
{
id: 'three-sum',
title: 'Triplet Sum Zero',
difficulty: 'Medium',
category: 'Array',
description: `Given an integer array \`nums\`, return all the triplets \`[nums[i], nums[j], nums[k]]\` such that \`i != j\`, \`i != k\`, and \`j != k\`, and \`nums[i] + nums[j] + nums[k] == 0\`.
Notice that the solution set must not contain duplicate triplets.`,
starterCode: {
javascript: `/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
};`,
python: `class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
pass`,
cpp: `class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
}
};`,
java: `class Solution {
public List<List<Integer>> threeSum(int[] nums) {
}
}`,
go: `func threeSum(nums []int) [][]int {
}`
},
testCases: [
{ input: 'nums = [-1,0,1,2,-1,-4]', output: '[[-1,-1,2],[-1,0,1]]', params: [[-1,0,1,2,-1,-4]], expected: [[-1,-1,2],[-1,0,1]] },
{ input: 'nums = [0,1,1]', output: '[]', params: [[0,1,1]], expected: [] },
{ input: 'nums = [0,0,0]', output: '[[0,0,0]]', params: [[0,0,0]], expected: [[0,0,0]] }
],
hiddenTestCases: [
{ input: 'nums = [-2,0,0,2,2]', output: '[[-2,0,2]]', params: [[-2,0,0,2,2]], expected: [[-2,0,2]] },
{ input: 'nums = []', output: '[]', params: [[]], expected: [] }
],
solution: {
javascript: `var threeSum = function(nums) {
const res = [];
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
let l = i + 1, r = nums.length - 1;
while (l < r) {
const sum = nums[i] + nums[l] + nums[r];
if (sum > 0) r--;
else if (sum < 0) l++;
else {
res.push([nums[i], nums[l], nums[r]]);
l++;
while (nums[l] === nums[l - 1] && l < r) l++;
}
}
}
return res;
};`,
python: `class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
res = []
nums.sort()
for i, a in enumerate(nums):
if i > 0 and a == nums[i - 1]:
continue
l, r = i + 1, len(nums) - 1
while l < r:
threeSum = a + nums[l] + nums[r]
if threeSum > 0:
r -= 1
elif threeSum < 0:
l += 1
else:
res.append([a, nums[l], nums[r]])
l += 1
while nums[l] == nums[l - 1] and l < r:
l += 1
return res`,
cpp: `class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int l = i + 1, r = nums.size() - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum > 0) r--;
else if (sum < 0) l++;
else {
res.push_back({nums[i], nums[l], nums[r]});
l++;
while (nums[l] == nums[l - 1] && l < r) l++;
}
}
}
return res;
}
};`,
java: `class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < nums.length && nums[i] <= 0; ++i)
if (i == 0 || nums[i - 1] != nums[i]) {
twoSumII(nums, i, res);
}
return res;
}
void twoSumII(int[] nums, int i, List<List<Integer>> res) {
int lo = i + 1, hi = nums.length - 1;
while (lo < hi) {
int sum = nums[i] + nums[lo] + nums[hi];
if (sum < 0) {
++lo;
} else if (sum > 0) {
--hi;
} else {
res.add(Arrays.asList(nums[i], nums[lo++], nums[hi--]));
while (lo < hi && nums[lo] == nums[lo - 1])
++lo;
}
}
}
}`,
go: `func threeSum(nums []int) [][]int {
sort.Ints(nums)
res := [][]int{}
for i := 0; i < len(nums)-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
l, r := i+1, len(nums)-1
for l < r {
sum := nums[i] + nums[l] + nums[r]
if sum < 0 {
l++
} else if sum > 0 {
r--
} else {
res = append(res, []int{nums[i], nums[l], nums[r]})
l++
for l < r && nums[l] == nums[l-1] {
l++
}
}
}
}
return res
}`,
explanation: "Sort the array first. Iterate through the array and for each element, use two pointers (left and right) to find pairs that sum to the negative of the current element. Skip duplicates to ensure unique triplets."
}
},
{
id: 'max-product-subarray',
title: 'Maximum Product Subarray',
difficulty: 'Medium',
category: 'Array',
description: `Given an integer array \`nums\`, find a contiguous non-empty subarray within the array that has the largest product, and return the product.`,
starterCode: {
javascript: `var maxProduct = function(nums) {\n \n};`,
python: `class Solution:\n def maxProduct(self, nums: List[int]) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n \n }\n};`,
java: `class Solution {\n public int maxProduct(int[] nums) {\n \n }\n}`,
go: `func maxProduct(nums []int) int {\n \n}`
},
testCases: [{ input: 'nums = [2,3,-2,4]', output: '6', params: [[2,3,-2,4]], expected: 6 }],
hiddenTestCases: [{ input: 'nums = [-2,0,-1]', output: '0', params: [[-2,0,-1]], expected: 0 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming to keep track of both the maximum and minimum product ending at the current position.' }
},
{
id: 'min-rotated-sorted',
title: 'Find Minimum in Rotated Sorted Array',
difficulty: 'Medium',
category: 'Array',
description: `Suppose an array of length \`n\` sorted in ascending order is rotated between \`1\` and \`n\` times. Find the minimum element of this array.`,
starterCode: {
javascript: `var findMin = function(nums) {\n \n};`,
python: `class Solution:\n def findMin(self, nums: List[int]) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int findMin(vector<int>& nums) {\n \n }\n};`,
java: `class Solution {\n public int findMin(int[] nums) {\n \n }\n}`,
go: `func findMin(nums []int) int {\n \n}`
},
testCases: [{ input: 'nums = [3,4,5,1,2]', output: '1', params: [[3,4,5,1,2]], expected: 1 }],
hiddenTestCases: [{ input: 'nums = [4,5,6,7,0,1,2]', output: '0', params: [[4,5,6,7,0,1,2]], expected: 0 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use binary search to find the inflection point where the array is rotated.' }
},
{
id: 'search-rotated-sorted',
title: 'Search in Rotated Sorted Array',
difficulty: 'Medium',
category: 'Array',
description: `Given the array \`nums\` after the possible rotation and an integer \`target\`, return the index of \`target\` if it is in \`nums\`, or \`-1\` if it is not in \`nums\`.`,
starterCode: {
javascript: `var search = function(nums, target) {\n \n};`,
python: `class Solution:\n def search(self, nums: List[int], target: int) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int search(vector<int>& nums, int target) {\n \n }\n};`,
java: `class Solution {\n public int search(int[] nums, int target) {\n \n }\n}`,
go: `func search(nums []int, target int) int {\n \n}`
},
testCases: [{ input: 'nums = [4,5,6,7,0,1,2], target = 0', output: '4', params: [[4,5,6,7,0,1,2], 0], expected: 4 }],
hiddenTestCases: [{ input: 'nums = [4,5,6,7,0,1,2], target = 3', output: '-1', params: [[4,5,6,7,0,1,2], 3], expected: -1 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a modified binary search that accounts for the rotation by identifying which half of the array is sorted.' }
},
{
id: 'sum-two-integers',
title: 'Sum of Two Integers',
difficulty: 'Medium',
category: 'Binary',
description: `Given two integers \`a\` and \`b\`, return the sum of the two integers without using the operators \`+\` and \`-\`.`,
starterCode: {
javascript: `var getSum = function(a, b) {\n \n};`,
python: `class Solution:\n def getSum(self, a: int, b: int) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int getSum(int a, int b) {\n \n }\n};`,
java: `class Solution {\n public int getSum(int a, int b) {\n \n }\n}`,
go: `func getSum(a int, b int) int {\n \n}`
},
testCases: [{ input: 'a = 1, b = 2', output: '3', params: [1, 2], expected: 3 }],
hiddenTestCases: [{ input: 'a = 2, b = 3', output: '5', params: [2, 3], expected: 5 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use bitwise XOR for addition and bitwise AND followed by a left shift for the carry.' }
},
{
id: 'number-of-1-bits',
title: 'Number of 1 Bits',
difficulty: 'Easy',
category: 'Binary',
description: `Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).`,
starterCode: {
javascript: `var hammingWeight = function(n) {\n \n};`,
python: `class Solution:\n def hammingWeight(self, n: int) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int hammingWeight(uint32_t n) {\n \n }\n};`,
java: `class Solution {\n public int hammingWeight(int n) {\n \n }\n}`,
go: `func hammingWeight(num uint32) int {\n \n}`
},
testCases: [{ input: 'n = 11', output: '3', params: [11], expected: 3 }],
hiddenTestCases: [{ input: 'n = 128', output: '1', params: [128], expected: 1 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use bitwise AND with (n-1) to repeatedly clear the least significant set bit until the number becomes zero.' }
},
{
id: 'counting-bits',
title: 'Counting Bits',
difficulty: 'Easy',
category: 'Binary',
description: `Given an integer \`n\`, return an array \`ans\` of length \`n + 1\` such that for each \`i\` (\`0 <= i <= n\`), \`ans[i]\` is the number of \`1\`'s in the binary representation of \`i\`.`,
starterCode: {
javascript: `var countBits = function(n) {\n \n};`,
python: `class Solution:\n def countBits(self, n: int) -> List[int]:\n pass`,
cpp: `class Solution {\npublic:\n vector<int> countBits(int n) {\n \n }\n};`,
java: `class Solution {\n public int[] countBits(int n) {\n \n }\n}`,
go: `func countBits(n int) []int {\n \n}`
},
testCases: [{ input: 'n = 2', output: '[0,1,1]', params: [2], expected: [0,1,1] }],
hiddenTestCases: [{ input: 'n = 5', output: '[0,1,1,2,1,2]', params: [5], expected: [0,1,1,2,1,2] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming: the number of set bits in i is 1 + the number of set bits in (i & (i-1)).' }
},
{
id: 'missing-number',
title: 'Missing Number',
difficulty: 'Easy',
category: 'Binary',
description: `Given an array \`nums\` containing \`n\` distinct numbers in the range \`[0, n]\`, return the only number in the range that is missing from the array.`,
starterCode: {
javascript: `var missingNumber = function(nums) {\n \n};`,
python: `class Solution:\n def missingNumber(self, nums: List[int]) -> int:\n pass`,
cpp: `class Solution {\npublic:\n int missingNumber(vector<int>& nums) {\n \n }\n};`,
java: `class Solution {\n public int missingNumber(int[] nums) {\n \n }\n}`,
go: `func missingNumber(nums []int) int {\n \n}`
},
testCases: [{ input: 'nums = [3,0,1]', output: '2', params: [[3,0,1]], expected: 2 }],
hiddenTestCases: [{ input: 'nums = [0,1]', output: '2', params: [[0,1]], expected: 2 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Calculate the expected sum of numbers from 0 to n and subtract the actual sum of the array elements.' }
},
{
id: 'reverse-bits',
title: 'Reverse Bits',
difficulty: 'Easy',
category: 'Binary',
description: `Reverse bits of a given 32 bits unsigned integer.`,
starterCode: {
javascript: `var reverseBits = function(n) {\n \n};`,
python: `class Solution:\n def reverseBits(self, n: int) -> int:\n pass`,
cpp: `class Solution {\npublic:\n uint32_t reverseBits(uint32_t n) {\n \n }\n};`,
java: `class Solution {\n public int reverseBits(int n) {\n \n }\n}`,
go: `func reverseBits(num uint32) uint32 {\n \n}`
},
testCases: [{ input: 'n = 43261596', output: '964176192', params: [43261596], expected: 964176192 }],
hiddenTestCases: [{ input: 'n = 1', output: '2147483648', params: [1], expected: 2147483648 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Iterate 32 times, shifting the result left and adding the last bit of n, then shifting n right.' }
},
{
id: 'climbing-stairs',
title: 'Climbing Stairs',
difficulty: 'Easy',
category: 'DP',
description: `You are climbing a staircase. It takes \`n\` steps to reach the top. Each time you can either climb \`1\` or \`2\` steps. In how many distinct ways can you climb to the top?`,
starterCode: { javascript: 'var climbStairs = function(n) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'n = 2', output: '2', params: [2], expected: 2 }],
hiddenTestCases: [{ input: 'n = 3', output: '3', params: [3], expected: 3 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'This is a classic Fibonacci sequence problem. The number of ways to reach step n is the sum of ways to reach step n-1 and n-2.' }
},
{
id: 'coin-change-ii',
title: 'Coin Change II',
difficulty: 'Medium',
category: 'DP',
description: `You are given an integer array \`coins\` representing coins of different denominations and an integer \`amount\` representing a total amount of money. Return the number of combinations that make up that amount.`,
starterCode: { javascript: 'var change = function(amount, coins) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'amount = 5, coins = [1,2,5]', output: '4', params: [5, [1,2,5]], expected: 4 }],
hiddenTestCases: [{ input: 'amount = 3, coins = [2]', output: '0', params: [3, [2]], expected: 0 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming where dp[i] represents the number of ways to make amount i. Iterate through each coin and update the dp array.' }
},
{
id: 'longest-increasing-subsequence',
title: 'Longest Increasing Subsequence',
difficulty: 'Medium',
category: 'DP',
description: `Given an integer array \`nums\`, return the length of the longest strictly increasing subsequence.`,
starterCode: { javascript: 'var lengthOfLIS = function(nums) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'nums = [10,9,2,5,3,7,101,18]', output: '4', params: [[10,9,2,5,3,7,101,18]], expected: 4 }],
hiddenTestCases: [{ input: 'nums = [0,1,0,3,2,3]', output: '4', params: [[0,1,0,3,2,3]], expected: 4 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming where dp[i] is the length of the LIS ending at index i, or use binary search with a tails array for O(n log n) complexity.' }
},
{
id: 'longest-common-subsequence',
title: 'Longest Common Subsequence',
difficulty: 'Medium',
category: 'DP',
description: `Given two strings \`text1\` and \`text2\`, return the length of their longest common subsequence.`,
starterCode: { javascript: 'var longestCommonSubsequence = function(text1, text2) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'text1 = "abcde", text2 = "ace"', output: '3', params: ["abcde", "ace"], expected: 3 }],
hiddenTestCases: [{ input: 'text1 = "abc", text2 = "abc"', output: '3', params: ["abc", "abc"], expected: 3 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a 2D DP table where dp[i][j] represents the LCS of text1[0...i] and text2[0...j].' }
},
{
id: 'word-break',
title: 'Word Break',
difficulty: 'Medium',
category: 'DP',
description: `Given a string \`s\` and a dictionary of strings \`wordDict\`, return \`true\` if \`s\` can be segmented into a space-separated sequence of one or more dictionary words.`,
starterCode: { javascript: 'var wordBreak = function(s, wordDict) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "leetcode", wordDict = ["leet","code"]', output: 'true', params: ["leetcode", ["leet","code"]], expected: true }],
hiddenTestCases: [{ input: 's = "applepenapple", wordDict = ["apple","pen"]', output: 'true', params: ["applepenapple", ["apple","pen"]], expected: true }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming where dp[i] is true if s[0...i] can be segmented. For each i, check all possible prefixes s[j...i].' }
},
{
id: 'combination-sum',
title: 'Combination Sum',
difficulty: 'Medium',
category: 'DP',
description: `Given an array of distinct integers \`candidates\` and a target integer \`target\`, return a list of all unique combinations of \`candidates\` where the chosen numbers sum to \`target\`.`,
starterCode: { javascript: 'var combinationSum = function(candidates, target) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'candidates = [2,3,6,7], target = 7', output: '[[2,2,3],[7]]', params: [[2,3,6,7], 7], expected: [[2,2,3],[7]] }],
hiddenTestCases: [{ input: 'candidates = [2,3,5], target = 8', output: '[[2,2,2,2],[2,3,3],[3,5]]', params: [[2,3,5], 8], expected: [[2,2,2,2],[2,3,3],[3,5]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use backtracking to explore all possible combinations. Since we can reuse elements, the recursive call should start from the same index.' }
},
{
id: 'max-sum-non-adjacent',
title: 'Maximum Sum of Non-Adjacent Elements',
difficulty: 'Medium',
category: 'DP',
description: `Given an array of positive integers, find the maximum sum of a subsequence such that no two elements in the subsequence are adjacent in the array.`,
starterCode: { javascript: 'var maxSumNonAdjacent = function(nums) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'nums = [3, 2, 7, 10]', output: '13', params: [[3, 2, 7, 10]], expected: 13 }],
hiddenTestCases: [{ input: 'nums = [3, 2, 5, 10, 7]', output: '15', params: [[3, 2, 5, 10, 7]], expected: 15 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'This is similar to the House Robber problem. For each element, you can either include it (and add it to the max sum excluding the previous element) or exclude it.' }
},
{
id: 'house-robber',
title: 'House Robber',
difficulty: 'Medium',
category: 'DP',
description: `You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. The only constraint stopping you from robbing each of them is that adjacent houses have security systems connected.`,
starterCode: { javascript: 'var rob = function(nums) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'nums = [1,2,3,1]', output: '4', params: [[1,2,3,1]], expected: 4 }],
hiddenTestCases: [{ input: 'nums = [2,7,9,3,1]', output: '12', params: [[2,7,9,3,1]], expected: 12 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming: dp[i] = max(dp[i-1], dp[i-2] + nums[i]).' }
},
{
id: 'decode-ways',
title: 'Decode Ways',
difficulty: 'Medium',
category: 'DP',
description: `A message containing letters from A-Z can be encoded into numbers using the following mapping: 'A' -> "1", 'B' -> "2", ..., 'Z' -> "26". Given a string \`s\` containing only digits, return the number of ways to decode it.`,
starterCode: { javascript: 'var numDecodings = function(s) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "12"', output: '2', params: ["12"], expected: 2 }],
hiddenTestCases: [{ input: 's = "226"', output: '3', params: ["226"], expected: 3 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use dynamic programming where dp[i] is the number of ways to decode s[0...i]. Check if s[i] and s[i-1...i] form valid decodings.' }
},
{
id: 'insert-interval',
title: 'Insert Interval',
difficulty: 'Medium',
category: 'Interval',
description: `You are given an array of non-overlapping intervals \`intervals\` where \`intervals[i] = [starti, endi]\` sorted in ascending order by \`starti\`. You are also given an interval \`newInterval = [start, end]\`.`,
starterCode: { javascript: 'var insert = function(intervals, newInterval) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'intervals = [[1,3],[6,9]], newInterval = [2,5]', output: '[[1,5],[6,9]]', params: [[[1,3],[6,9]], [2,5]], expected: [[1,5],[6,9]] }],
hiddenTestCases: [{ input: 'intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]', output: '[[1,2],[3,10],[12,16]]', params: [[[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8]], expected: [[1,2],[3,10],[12,16]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Iterate through intervals, adding those that end before the new interval starts. Then merge the new interval with overlapping ones, and finally add the remaining intervals.' }
},
{
id: 'merge-intervals',
title: 'Merge Intervals',
difficulty: 'Medium',
category: 'Interval',
description: `Given an array of \`intervals\` where \`intervals[i] = [starti, endi]\`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.`,
starterCode: { javascript: 'var merge = function(intervals) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'intervals = [[1,3],[2,6],[8,10],[15,18]]', output: '[[1,6],[8,10],[15,18]]', params: [[[1,3],[2,6],[8,10],[15,18]]], expected: [[1,6],[8,10],[15,18]] }],
hiddenTestCases: [{ input: 'intervals = [[1,4],[4,5]]', output: '[[1,5]]', params: [[[1,4],[4,5]]], expected: [[1,5]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Sort intervals by start time. Iterate and merge an interval with the previous one if they overlap.' }
},
{
id: 'non-overlapping-intervals',
title: 'Non-overlapping Intervals',
difficulty: 'Medium',
category: 'Interval',
description: `Given an array of intervals \`intervals\` where \`intervals[i] = [starti, endi]\`, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.`,
starterCode: { javascript: 'var eraseOverlapIntervals = function(intervals) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'intervals = [[1,2],[2,3],[3,4],[1,3]]', output: '1', params: [[[1,2],[2,3],[3,4],[1,3]]], expected: 1 }],
hiddenTestCases: [{ input: 'intervals = [[1,2],[1,2],[1,2]]', output: '2', params: [[[1,2],[1,2],[1,2]]], expected: 2 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Sort intervals by end time. Use a greedy approach to keep the interval that ends earliest to leave more room for others.' }
},
{
id: 'repeating-missing-number',
title: 'Find the Repeating and Missing Number',
difficulty: 'Medium',
category: 'Interval',
description: `Given an unsorted array of size \`n\`. Array elements are in the range from \`1\` to \`n\`. One number from set \`{1, 2, ..., n}\` is missing and one number occurs twice in the array. Find these two numbers.`,
starterCode: { javascript: 'var findTwoElement = function(arr) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'arr = [3, 1, 3]', output: '[3, 2]', params: [[3, 1, 3]], expected: [3, 2] }],
hiddenTestCases: [{ input: 'arr = [4, 3, 6, 2, 1, 1]', output: '[1, 5]', params: [[4, 3, 6, 2, 1, 1]], expected: [1, 5] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use mathematical equations (sum of n numbers and sum of squares) or bitwise XOR to find the two numbers.' }
},
{
id: 'clone-graph',
title: 'Clone Graph',
difficulty: 'Medium',
category: 'Graph',
description: `Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph.`,
starterCode: { javascript: 'var cloneGraph = function(node) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'adjList = [[2,4],[1,3],[2,4],[1,3]]', output: '[[2,4],[1,3],[2,4],[1,3]]', params: [[[2,4],[1,3],[2,4],[1,3]]], expected: [[2,4],[1,3],[2,4],[1,3]] }],
hiddenTestCases: [{ input: 'adjList = [[]]', output: '[[]]', params: [[[]]], expected: [[]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a hash map to store the mapping from original nodes to their clones. Perform a BFS or DFS traversal to clone the graph.' }
},
{
id: 'course-schedule-i',
title: 'Course Schedule I',
difficulty: 'Medium',
category: 'Graph',
description: `There are a total of \`numCourses\` courses you have to take, labeled from \`0\` to \`numCourses - 1\`. Some courses may have prerequisites. Return \`true\` if you can finish all courses.`,
starterCode: { javascript: 'var canFinish = function(numCourses, prerequisites) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'numCourses = 2, prerequisites = [[1,0]]', output: 'true', params: [2, [[1,0]]], expected: true }],
hiddenTestCases: [{ input: 'numCourses = 2, prerequisites = [[1,0],[0,1]]', output: 'false', params: [2, [[1,0],[0,1]]], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'This is a cycle detection problem in a directed graph. Use Kahn\'s algorithm (BFS) or DFS to detect if a cycle exists.' }
},
{
id: 'pacific-atlantic-water-flow',
title: 'Pacific Atlantic Water Flow',
difficulty: 'Medium',
category: 'Graph',
description: `There is an \`m x n\` rectangular island that borders both the Pacific Ocean and the Atlantic Ocean. Find all grid coordinates where water can flow to both oceans.`,
starterCode: { javascript: 'var pacificAtlantic = function(heights) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]', output: '[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]', params: [[[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]], expected: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] }],
hiddenTestCases: [{ input: 'heights = [[2,1],[1,2]]', output: '[[0,0],[0,1],[1,0],[1,1]]', params: [[[2,1],[1,2]]], expected: [[0,0],[0,1],[1,0],[1,1]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Perform DFS/BFS starting from the edges of the Pacific and Atlantic oceans. The intersection of cells reachable from both oceans is the answer.' }
},
{
id: 'number-of-islands',
title: 'Number of Islands',
difficulty: 'Medium',
category: 'Graph',
description: `Given an \`m x n\` 2D binary grid \`grid\` which represents a map of '1's (land) and '0's (water), return the number of islands.`,
starterCode: { javascript: 'var numIslands = function(grid) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]', output: '1', params: [[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]], expected: 1 }],
hiddenTestCases: [{ input: 'grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]', output: '3', params: [[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]], expected: 3 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Iterate through each cell. When you find a "1", increment the count and perform DFS/BFS to mark all connected "1"s as visited.' }
},
{
id: 'longest-consecutive-sequence',
title: 'Longest Consecutive Sequence',
difficulty: 'Medium',
category: 'Graph',
description: `Given an unsorted array of integers \`nums\`, return the length of the longest consecutive elements sequence.`,
starterCode: { javascript: 'var longestConsecutive = function(nums) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'nums = [100,4,200,1,3,2]', output: '4', params: [[100,4,200,1,3,2]], expected: 4 }],
hiddenTestCases: [{ input: 'nums = [0,3,7,2,5,8,4,6,0,1]', output: '9', params: [[0,3,7,2,5,8,4,6,0,1]], expected: 9 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a hash set for O(1) lookups. For each number, if it\'s the start of a sequence (i.e., num-1 is not in the set), count the length of the sequence.' }
},
{
id: 'alien-dictionary',
title: 'Alien Dictionary',
difficulty: 'Hard',
category: 'Graph',
description: `There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you. You are given a list of strings \`words\` from the alien language's dictionary, where the strings in \`words\` are sorted lexicographically by the rules of this new language.`,
starterCode: { javascript: 'var alienOrder = function(words) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'words = ["wrt","wrf","er","ett","rftt"]', output: '"wertf"', params: [["wrt","wrf","er","ett","rftt"]], expected: "wertf" }],
hiddenTestCases: [{ input: 'words = ["z","x"]', output: '"zx"', params: [["z","x"]], expected: "zx" }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Build a directed graph where an edge exists from char A to char B if A comes before B. Perform a topological sort on the graph.' }
},
{
id: 'graph-valid-tree',
title: 'Graph Valid Tree',
difficulty: 'Medium',
category: 'Graph',
description: `Given \`n\` nodes labeled from \`0\` to \`n-1\` and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.`,
starterCode: { javascript: 'var validTree = function(n, edges) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'n = 5, edges = [[0,1],[0,2],[0,3],[1,4]]', output: 'true', params: [5, [[0,1],[0,2],[0,3],[1,4]]], expected: true }],
hiddenTestCases: [{ input: 'n = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]', output: 'false', params: [5, [[0,1],[1,2],[2,3],[1,3],[1,4]]], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'A graph is a tree if it is connected and has no cycles. Use DFS/BFS or Union-Find to check these conditions.' }
},
{
id: 'reverse-linked-list',
title: 'Reverse a Linked List',
difficulty: 'Easy',
category: 'Linked List',
description: `Given the head of a singly linked list, reverse the list, and return the reversed list.`,
starterCode: { javascript: 'var reverseList = function(head) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'head = [1,2,3,4,5]', output: '[5,4,3,2,1]', params: [[1,2,3,4,5]], expected: [5,4,3,2,1] }],
hiddenTestCases: [{ input: 'head = [1,2]', output: '[2,1]', params: [[1,2]], expected: [2,1] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use three pointers: prev, curr, and next. Iterate through the list and reverse the pointers.' }
},
{
id: 'detect-loop-linked-list',
title: 'Detect a Loop in Linked List',
difficulty: 'Easy',
category: 'Linked List',
description: `Given head, the head of a linked list, determine if the linked list has a cycle in it.`,
starterCode: { javascript: 'var hasCycle = function(head) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'head = [3,2,0,-4], pos = 1', output: 'true', params: [[3,2,0,-4], 1], expected: true }],
hiddenTestCases: [{ input: 'head = [1], pos = -1', output: 'false', params: [[1], -1], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use Floyd\'s Cycle-Finding Algorithm (tortoise and hare). If the fast pointer catches up to the slow pointer, a cycle exists.' }
},
{
id: 'merge-two-sorted-lists',
title: 'Merge Two Sorted Lists',
difficulty: 'Easy',
category: 'Linked List',
description: `Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.`,
starterCode: { javascript: 'var mergeTwoLists = function(list1, list2) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'list1 = [1,2,4], list2 = [1,3,4]', output: '[1,1,2,3,4,4]', params: [[1,2,4], [1,3,4]], expected: [1,1,2,3,4,4] }],
hiddenTestCases: [{ input: 'list1 = [], list2 = [0]', output: '[0]', params: [[], [0]], expected: [0] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a dummy head node and a current pointer. Compare the heads of the two lists and append the smaller one to the merged list.' }
},
{
id: 'merge-k-sorted-lists',
title: 'Merge K Sorted Lists',
difficulty: 'Hard',
category: 'Linked List',
description: `You are given an array of \`k\` linked-lists \`lists\`, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it.`,
starterCode: { javascript: 'var mergeKLists = function(lists) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'lists = [[1,4,5],[1,3,4],[2,6]]', output: '[1,1,2,3,4,4,5,6]', params: [[[1,4,5],[1,3,4],[2,6]]], expected: [1,1,2,3,4,4,5,6] }],
hiddenTestCases: [{ input: 'lists = []', output: '[]', params: [[]], expected: [] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a min-priority queue to keep track of the heads of all lists. Alternatively, use a divide and conquer approach to merge lists in pairs.' }
},
{
id: 'remove-nth-node-from-back',
title: 'Remove Nth Node From End of List',
difficulty: 'Medium',
category: 'Linked List',
description: `Given the head of a linked list, remove the nth node from the end of the list and return its head.`,
starterCode: { javascript: 'var removeNthFromEnd = function(head, n) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'head = [1,2,3,4,5], n = 2', output: '[1,2,3,5]', params: [[1,2,3,4,5], 2], expected: [1,2,3,5] }],
hiddenTestCases: [{ input: 'head = [1], n = 1', output: '[]', params: [[1], 1], expected: [] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use two pointers: fast and slow. Move the fast pointer n steps ahead, then move both until fast reaches the end. The slow pointer will be at the node before the one to be removed.' }
},
{
id: 'set-matrix-zeroes',
title: 'Set Matrix Zeroes',
difficulty: 'Medium',
category: 'Matrix',
description: `Given an \`m x n\` integer matrix \`matrix\`, if an element is \`0\`, set its entire row and column to \`0\`'s. You must do it in place.`,
starterCode: { javascript: 'var setZeroes = function(matrix) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'matrix = [[1,1,1],[1,0,1],[1,1,1]]', output: '[[1,0,1],[0,0,0],[1,0,1]]', params: [[[1,1,1],[1,0,1],[1,1,1]]], expected: [[1,0,1],[0,0,0],[1,0,1]] }],
hiddenTestCases: [{ input: 'matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]', output: '[[0,0,0,0],[0,4,5,0],[0,3,1,0]]', params: [[[0,1,2,0],[3,4,5,2],[1,3,1,5]]], expected: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use the first row and first column of the matrix as markers to indicate if that row or column should be zeroed.' }
},
{
id: 'spiral-matrix',
title: 'Spiral Matrix',
difficulty: 'Medium',
category: 'Matrix',
description: `Given an \`m x n\` matrix, return all elements of the matrix in spiral order.`,
starterCode: { javascript: 'var spiralOrder = function(matrix) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'matrix = [[1,2,3],[4,5,6],[7,8,9]]', output: '[1,2,3,6,9,8,7,4,5]', params: [[[1,2,3],[4,5,6],[7,8,9]]], expected: [1,2,3,6,9,8,7,4,5] }],
hiddenTestCases: [{ input: 'matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]', output: '[1,2,3,4,8,12,11,10,9,5,6,7]', params: [[[1,2,3,4],[5,6,7,8],[9,10,11,12]]], expected: [1,2,3,4,8,12,11,10,9,5,6,7] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Maintain four boundaries (top, bottom, left, right) and iterate in a spiral pattern, updating the boundaries as you go.' }
},
{
id: 'rotate-matrix',
title: 'Rotate Image',
difficulty: 'Medium',
category: 'Matrix',
description: `You are given an \`n x n\` 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place.`,
starterCode: { javascript: 'var rotate = function(matrix) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'matrix = [[1,2,3],[4,5,6],[7,8,9]]', output: '[[7,4,1],[8,5,2],[9,6,3]]', params: [[[1,2,3],[4,5,6],[7,8,9]]], expected: [[7,4,1],[8,5,2],[9,6,3]] }],
hiddenTestCases: [{ input: 'matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]', output: '[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]', params: [[[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]], expected: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Transpose the matrix (swap matrix[i][j] with matrix[j][i]) and then reverse each row.' }
},
{
id: 'max-depth-bt',
title: 'Maximum Depth of Binary Tree',
difficulty: 'Easy',
category: 'Tree',
description: `Given the root of a binary tree, return its maximum depth.`,
starterCode: { javascript: 'var maxDepth = function(root) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'root = [3,9,20,null,null,15,7]', output: '3', params: [[3,9,20,null,null,15,7]], expected: 3 }],
hiddenTestCases: [{ input: 'root = [1,null,2]', output: '2', params: [[1,null,2]], expected: 2 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use recursion: the depth of a tree is 1 + the maximum depth of its left and right subtrees.' }
},
{
id: 'identical-trees',
title: 'Check if Two Trees are Identical',
difficulty: 'Easy',
category: 'Tree',
description: `Given the roots of two binary trees \`p\` and \`q\`, write a function to check if they are the same or not.`,
starterCode: { javascript: 'var isSameTree = function(p, q) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'p = [1,2,3], q = [1,2,3]', output: 'true', params: [[1,2,3], [1,2,3]], expected: true }],
hiddenTestCases: [{ input: 'p = [1,2], q = [1,null,2]', output: 'false', params: [[1,2], [1,null,2]], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Two trees are identical if their roots have the same value and their left and right subtrees are also identical.' }
},
{
id: 'invert-bt',
title: 'Invert/Flip Binary Tree',
difficulty: 'Easy',
category: 'Tree',
description: `Given the root of a binary tree, invert the tree, and return its root.`,
starterCode: { javascript: 'var invertTree = function(root) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'root = [4,2,7,1,3,6,9]', output: '[4,7,2,9,6,3,1]', params: [[4,2,7,1,3,6,9]], expected: [4,7,2,9,6,3,1] }],
hiddenTestCases: [{ input: 'root = [2,1,3]', output: '[2,3,1]', params: [[2,1,3]], expected: [2,3,1] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Swap the left and right children of each node recursively.' }
},
{
id: 'max-path-sum',
title: 'Maximum Path Sum',
difficulty: 'Hard',
category: 'Tree',
description: `A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. Find the maximum path sum of any non-empty path.`,
starterCode: { javascript: 'var maxPathSum = function(root) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'root = [1,2,3]', output: '6', params: [[1,2,3]], expected: 6 }],
hiddenTestCases: [{ input: 'root = [-10,9,20,null,null,15,7]', output: '42', params: [[-10,9,20,null,null,15,7]], expected: 42 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'For each node, calculate the maximum path sum passing through it (left gain + node value + right gain) and update a global maximum.' }
},
{
id: 'level-order-traversal',
title: 'Level Order Traversal',
difficulty: 'Medium',
category: 'Tree',
description: `Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).`,
starterCode: { javascript: 'var levelOrder = function(root) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'root = [3,9,20,null,null,15,7]', output: '[[3],[9,20],[15,7]]', params: [[3,9,20,null,null,15,7]], expected: [[3],[9,20],[15,7]] }],
hiddenTestCases: [{ input: 'root = [1]', output: '[[1]]', params: [[1]], expected: [[1]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a queue to perform a Breadth-First Search (BFS). For each level, process all nodes currently in the queue.' }
},
{
id: 'serialize-deserialize-bt',
title: 'Serialize and De-serialize BT',
difficulty: 'Hard',
category: '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. Design an algorithm to serialize and deserialize a binary tree.`,
starterCode: { javascript: 'var serialize = function(root) {\n \n};\nvar deserialize = function(data) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'root = [1,2,3,null,null,4,5]', output: '[1,2,3,null,null,4,5]', params: [[1,2,3,null,null,4,5]], expected: [1,2,3,null,null,4,5] }],
hiddenTestCases: [{ input: 'root = []', output: '[]', params: [[]], expected: [] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a pre-order traversal for serialization, marking null nodes with a special character. Use a queue for deserialization to reconstruct the tree.' }
},
{
id: 'longest-substring-without-repeating',
title: 'Longest Substring Without Repeating Characters',
difficulty: 'Medium',
category: 'String',
description: `Given a string \`s\`, find the length of the longest substring without repeating characters.`,
starterCode: { javascript: 'var lengthOfLongestSubstring = function(s) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "abcabcbb"', output: '3', params: ["abcabcbb"], expected: 3 }],
hiddenTestCases: [{ input: 's = "bbbbb"', output: '1', params: ["bbbbb"], expected: 1 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a sliding window with two pointers and a hash set or map to keep track of characters in the current window.' }
},
{
id: 'longest-repeating-character-replacement',
title: 'Longest Repeating Character Replacement',
difficulty: 'Medium',
category: 'String',
description: `You are given a string \`s\` and an integer \`k\`. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most \`k\` times.`,
starterCode: { javascript: 'var characterReplacement = function(s, k) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "ABAB", k = 2', output: '4', params: ["ABAB", 2], expected: 4 }],
hiddenTestCases: [{ input: 's = "AABABBA", k = 1', output: '4', params: ["AABABBA", 1], expected: 4 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a sliding window. The condition for a valid window is (window length - frequency of most frequent character <= k).' }
},
{
id: 'minimum-window-substring',
title: 'Minimum Window Substring',
difficulty: 'Hard',
category: 'String',
description: `Given two strings \`s\` and \`t\` of lengths \`m\` and \`n\` respectively, return the minimum window substring of \`s\` such that every character in \`t\` (including duplicates) is included in the window.`,
starterCode: { javascript: 'var minWindow = function(s, t) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "ADOBECODEBANC", t = "ABC"', output: '"BANC"', params: ["ADOBECODEBANC", "ABC"], expected: "BANC" }],
hiddenTestCases: [{ input: 's = "a", t = "a"', output: '"a"', params: ["a", "a"], expected: "a" }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a sliding window and two frequency maps. Expand the window until all characters of t are included, then contract it as much as possible.' }
},
{
id: 'valid-anagram',
title: 'Valid Anagram',
difficulty: 'Easy',
category: 'String',
description: `Given two strings \`s\` and \`t\`, return \`true\` if \`t\` is an anagram of \`s\`, and \`false\` otherwise.`,
starterCode: { javascript: 'var isAnagram = function(s, t) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "anagram", t = "nagaram"', output: 'true', params: ["anagram", "nagaram"], expected: true }],
hiddenTestCases: [{ input: 's = "rat", t = "car"', output: 'false', params: ["rat", "car"], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Two strings are anagrams if they have the same characters with the same frequencies. Use a frequency map or sort both strings.' }
},
{
id: 'group-anagrams',
title: 'Group Words by Anagrams',
difficulty: 'Medium',
category: 'String',
description: `Given an array of strings \`strs\`, group the anagrams together. You can return the answer in any order.`,
starterCode: { javascript: 'var groupAnagrams = function(strs) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'strs = ["eat","tea","tan","ate","nat","bat"]', output: '[["bat"],["nat","tan"],["ate","eat","tea"]]', params: [["eat","tea","tan","ate","nat","bat"]], expected: [["bat"],["nat","tan"],["ate","eat","tea"]] }],
hiddenTestCases: [{ input: 'strs = [""]', output: '[[""]]', params: [[""]], expected: [[""]] }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a hash map where the key is the sorted version of the string (or a frequency count) and the value is a list of anagrams.' }
},
{
id: 'balanced-parenthesis',
title: 'Balanced Paranthesis',
difficulty: 'Easy',
category: 'String',
description: `Given a string \`s\` containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.`,
starterCode: { javascript: 'var isValid = function(s) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "()"', output: 'true', params: ["()"], expected: true }],
hiddenTestCases: [{ input: 's = "()[]{}"', output: 'true', params: ["()[]{}"], expected: true }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Use a stack. For each opening bracket, push it onto the stack. For each closing bracket, check if it matches the top of the stack.' }
},
{
id: 'palindrome-number',
title: 'Palindrome Number',
difficulty: 'Easy',
category: 'String',
description: `Given an integer \`x\`, return \`true\` if \`x\` is a palindrome, and \`false\` otherwise.`,
starterCode: { javascript: 'var isPalindrome = function(x) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 'x = 121', output: 'true', params: [121], expected: true }],
hiddenTestCases: [{ input: 'x = -121', output: 'false', params: [-121], expected: false }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Reverse the second half of the number and compare it with the first half, or convert the number to a string and use two pointers.' }
},
{
id: 'longest-palindromic-substring',
title: 'Longest Palindromic Substring',
difficulty: 'Medium',
category: 'String',
description: `Given a string \`s\`, return the longest palindromic substring in \`s\`.`,
starterCode: { javascript: 'var longestPalindrome = function(s) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "babad"', output: '"bab"', params: ["babad"], expected: "bab" }],
hiddenTestCases: [{ input: 's = "cbbd"', output: '"bb"', params: ["cbbd"], expected: "bb" }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Expand around each character (and each pair of adjacent characters) to find the longest palindrome centered at that position.' }
},
{
id: 'palindromic-substrings',
title: 'Palindromic Substrings',
difficulty: 'Medium',
category: 'String',
description: `Given a string \`s\`, return the number of palindromic substrings in it.`,
starterCode: { javascript: 'var countSubstrings = function(s) {\n \n};', python: '', cpp: '', java: '', go: '' },
testCases: [{ input: 's = "abc"', output: '3', params: ["abc"], expected: 3 }],
hiddenTestCases: [{ input: 's = "aaa"', output: '6', params: ["aaa"], expected: 6 }],
solution: { javascript: '', python: '', cpp: '', java: '', go: '', explanation: 'Similar to finding the longest palindromic substring, expand around each center and count all palindromes found.' }
}
];
|