{ "exportType": "devscribe_collection", "version": "1.0", "exportedAt": 1785728889195, "data": { "_id": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "name": "DSA 30 Problems", "createdAt": 1785210624223, "updatedAt": 1785385669906, "parentId": null, "github_repo": "avinashiitb/DSA-30-Problems", "github_branch": "main", "github_path": "/", "github_last_sync_sha": "6c20cb41a588774c1dc16f9243e31e6887a8f7ba", "github_sync_enabled": 1, "github_encryption_enabled": 0, "github_passphrase": "", "subfolders": [ { "_id": "387058c7-0591-440d-9354-64e2cde35186", "name": "1. Arrays", "createdAt": 1785210624223, "updatedAt": 1785296720233, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "01c828da-90d9-4dba-b086-b6e8a7fc91ef", "name": "1: Two Sum", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "62b2723d-0dd7-4fcf-8717-72f90c0cb363", "title": "Two Sum Code", "folderId": "01c828da-90d9-4dba-b086-b6e8a7fc91ef", "time": 1785728066172, "createdAt": 1785210624223, "updatedAt": 1785728066174, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Solution {\n public void solve(int[] A, int target) {\n HashMap Map = new HashMap<>();\n\n for(int i = 0; i< A.length; i++) {\n int X = target - A[i];\n if(Map.containsKey(X)) {\n int j = Map.get(X);\n System.out.println(\"Pair: [\" + j + \", \" + i + \"]\");\n }\n Map.put(A[i], i);\n }\n \n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"nums\"); \n int target = Input.getInt(\"target\");\n int[] output = Input.getIntArray(\"output\");\n \n System.out.println(\"Expected: \"+ Arrays.toString(output));\n \n Solution sol = new Solution();\n\n sol.solve(nums, target);\n }\n}", "language": "java", "output": "Expected: [0, 1]\nPair: [0, 1]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785044934411_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785044934411_0_0", "key": "nums", "value": "[2, 7, 11, 15]" }, { "id": "in_1785044934411_0_1", "key": "target", "value": "9" }, { "id": "in_1785044934411_0_2", "key": "output", "value": "[0, 1]" } ] }, { "id": "tc_1785044934411_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785044934411_1_0", "key": "nums", "value": "[3, 2, 4]" }, { "id": "in_1785044934411_1_1", "key": "target", "value": "6" }, { "id": "in_1785044934411_1_2", "key": "output", "value": "[1, 2]" } ] }, { "id": "tc_1785044934411_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785044934411_2_0", "key": "nums", "value": "[3, 3]" }, { "id": "in_1785044934411_2_1", "key": "target", "value": "6" }, { "id": "in_1785044934411_2_2", "key": "output", "value": "[0, 1]" } ] }, { "id": "tc_1785044934411_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785044934411_3_0", "key": "nums", "value": "[-3, 4, 3, 90]" }, { "id": "in_1785044934411_3_1", "key": "target", "value": "0" }, { "id": "in_1785044934411_3_2", "key": "output", "value": "[0, 2]" } ] }, { "id": "tc_1785044934411_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785044934411_4_0", "key": "nums", "value": "[-1, -2, -3, -4, -5]" }, { "id": "in_1785044934411_4_1", "key": "target", "value": "-8" }, { "id": "in_1785044934411_4_2", "key": "output", "value": "[2, 4]" } ] }, { "id": "tc_1785044934411_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785044934411_5_0", "key": "nums", "value": "[0, 4, 3, 0]" }, { "id": "in_1785044934411_5_1", "key": "target", "value": "0" }, { "id": "in_1785044934411_5_2", "key": "output", "value": "[0, 3]" } ] }, { "id": "tc_1785044934411_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785044934411_6_0", "key": "nums", "value": "[1000000, 500000, 300000, 700000]" }, { "id": "in_1785044934411_6_1", "key": "target", "value": "1700000" }, { "id": "in_1785044934411_6_2", "key": "output", "value": "[0, 3]" } ] }, { "id": "tc_1785044934411_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785044934411_7_0", "key": "nums", "value": "[5, 75, 25]" }, { "id": "in_1785044934411_7_1", "key": "target", "value": "100" }, { "id": "in_1785044934411_7_2", "key": "output", "value": "[1, 2]" } ] }, { "id": "tc_1785044934411_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785044934411_8_0", "key": "nums", "value": "[1, 2, 3, 4, 5]" }, { "id": "in_1785044934411_8_1", "key": "target", "value": "9" }, { "id": "in_1785044934411_8_2", "key": "output", "value": "[3, 4]" } ] }, { "id": "tc_1785044934411_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785044934411_9_0", "key": "nums", "value": "[8, 1, 6, 2, 9]" }, { "id": "in_1785044934411_9_1", "key": "target", "value": "10" }, { "id": "in_1785044934411_9_2", "key": "output", "value": "[3, 0]" } ] }, { "id": "tc_1785044934411_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785044934411_10_0", "key": "nums", "value": "[]" }, { "id": "in_1785044934411_10_1", "key": "target", "value": "5" }, { "id": "in_1785044934411_10_2", "key": "output", "value": "[]" } ] }, { "id": "tc_1785044934411_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785044934411_11_0", "key": "nums", "value": "[5]" }, { "id": "in_1785044934411_11_1", "key": "target", "value": "5" }, { "id": "in_1785044934411_11_2", "key": "output", "value": "[]" } ] }, { "id": "tc_1785044934411_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785044934411_12_0", "key": "nums", "value": "[1, 2, 3]" }, { "id": "in_1785044934411_12_1", "key": "target", "value": "10" }, { "id": "in_1785044934411_12_2", "key": "output", "value": "[]" } ] } ], "selectedTestCaseId": "tc_1785044934411_2" } } ] }, { "_id": "ffe835a9-49d5-41a7-9995-afad20f5ff0e", "title": "Two Sum Doc", "folderId": "01c828da-90d9-4dba-b086-b6e8a7fc91ef", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "199d95cf-9c25-4bcd-aded-f6cda555ee0c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "1.Two Sum", "styles": {} } ], "children": [] }, { "id": "b5173bf4-425e-4116-a1b1-42f3e168408d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "17b4e5c7-afa3-4fc0-8400-2b81d3c96505", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an array of integers ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": " and an integer ", "styles": {} }, { "type": "text", "text": "target", "styles": { "code": true } }, { "type": "text", "text": ", return the indices of the two numbers such that they add up to the target.", "styles": {} } ], "children": [] }, { "id": "6389cc3d-7b6c-4fe2-b898-c48ea91bf3cb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You may assume that each input has exactly one solution, and you may not use the same element twice.", "styles": {} } ], "children": [] }, { "id": "9359a18c-5034-4685-a003-15a608c23e09", "type": "divider", "props": {}, "children": [] }, { "id": "baeeee66-d183-40db-9c28-0f1928ba8524", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "28955b7c-6e0a-47ba-b572-f95a5f765faf", "type": "column_list", "props": {}, "children": [ { "id": "afce5c61-6ba0-4571-87ef-7496050cc442", "type": "column", "props": { "width": "1" }, "children": [ { "id": "b11d31df-001a-4f6f-b960-7bf029fd9fbf", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Input", "styles": {} } ], "children": [] }, { "id": "50f6a9d2-ed1a-493a-9344-a934d31585ba", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [2, 7, 11, 15]\ntarget = 9", "wordWrap": "false" }, "children": [] }, { "id": "d484d347-ef0f-4be5-af7c-6744c7e58771", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "02e1bb54-58dd-4e47-a413-3406cf316c95", "type": "column", "props": { "width": "1" }, "children": [ { "id": "9de526b4-e1a5-4930-8b31-94efb1686cd7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Output", "styles": {} } ], "children": [] }, { "id": "905f6bd3-f36c-4e1b-a681-eae21fc6cacb", "type": "codeBlock", "props": { "language": "javascript", "code": "[0, 1]", "wordWrap": "true" }, "children": [] }, { "id": "6d2d6f92-4e56-4e46-8216-4d1e36ae697a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] }, { "id": "20d236cc-9f06-4335-983f-b1e6c8200398", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "40195ca1-0b75-4175-9e2a-927d0eb8ea7a", "type": "column", "props": { "width": "1" }, "children": [ { "id": "afce3d09-7607-4bfe-9261-e442f6355dad", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Explanation", "styles": {} } ], "children": [] }, { "id": "5ed9a85e-1232-46df-ab93-8ae5acb3580b", "type": "codeBlock", "props": { "language": "javascript", "code": "nums[0] + nums[1] = 2 + 7 = 9", "wordWrap": "true" }, "children": [] }, { "id": "e8964efb-9e97-4246-a1a6-fa4e5563c01f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] }, { "id": "e38995a0-cfb6-43e5-a6cc-a3e04182df32", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] }, { "id": "c4e8ac3c-ad13-4b7b-ba74-3d36d69ccf4e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "0c2fc1be-47a3-476c-ac4d-9772bfc99bf3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "f576ec0c-baf8-4938-840d-11e7b56bb837", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of checking every pair (which takes ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " time), use a ", "styles": {} }, { "type": "text", "text": "HashMap", "styles": { "bold": true } }, { "type": "text", "text": " to store numbers and its index that have already been visited.", "styles": {} } ], "children": [] }, { "id": "c5089f99-c187-4ed7-a001-ff6171e1cb16", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "For each number Check whether the complement ", "styles": {} }, { "type": "text", "text": "(target - num)", "styles": { "code": true } }, { "type": "text", "text": " already exists in the HashMap.", "styles": {} } ], "children": [] }, { "id": "378e1ba2-580f-4a94-8573-57535774f5ba", "type": "divider", "props": {}, "children": [] }, { "id": "3c0a7ebb-04e0-4511-82fa-2704685b0845", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "10595a8d-d67f-4b8a-87c0-22d9977141ee", "type": "addDoc", "props": { "fileId": "62b2723d-0dd7-4fcf-8717-72f90c0cb363", "title": "Two Sum Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "2943fb1d-f123-49ec-a03d-3351d4238f36", "type": "divider", "props": {}, "children": [] }, { "id": "8dc8edff-92fc-4662-b421-3da978f63ea9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "8d9769d3-694f-41e6-a531-59cf7d3c19d6", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "15360e82-bb99-4975-b84c-bcd049df091c", "type": "divider", "props": {}, "children": [] }, { "id": "4964bf9c-78fc-44c8-893d-d8230d5304dc", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "0605c00e-79d6-4267-ba5b-5ff1a6ea7431", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Whenever you're asked to find a pair of elements that satisfy a condition, consider using a ", "styles": {} }, { "type": "text", "text": "HashMap", "styles": { "bold": true } }, { "type": "text", "text": ". It often reduces the time complexity from ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " to ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " by enabling constant-time lookups.", "styles": {} } ], "children": [] } ] } } ] } ] }, { "_id": "af9ad9a9-9d04-4cae-be2a-3aa916f199bd", "name": "2: Greedy - Buy Stock", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "2e36f777-4a25-4fac-9ad5-46f45aac9ccd", "title": "Greedy Stock Code", "folderId": "af9ad9a9-9d04-4cae-be2a-3aa916f199bd", "time": 1785585946617, "createdAt": 1785210624223, "updatedAt": 1785585946620, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n public int solve(int[] A) {\n int Min = Integer.MAX_VALUE;\n int Max = A[0];\n\n int Profit = 0;\n if(A.length == 0) return 0;\n \n for(int i = 0; i< A.length; i++) {\n if(A[i] < Min) {\n Min = A[i];\n Max = A[i];\n } else if(Max < A[i]) {\n Max = A[i];\n }\n Profit = Math.max(Profit, Max - Min);\n }\n\n return Profit;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"prices\"); \n int output = Input.getInt(\"output\");\n System.out.println(\"Expected: \"+ output);\n \n Solution sol = new Solution();\n\n int Profit = sol.solve(nums);\n\n System.out.println(\"Profit: \" + Profit);\n }\n}", "language": "java", "output": "Expected: 5\nProfit: 5\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785045036896_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785045036896_0_0", "key": "prices", "value": "[7,1,5,3,6,4]" }, { "id": "in_1785045036896_0_1", "key": "output", "value": "5" } ] }, { "id": "tc_1785045036896_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785045036896_1_0", "key": "prices", "value": "[7,6,4,3,1]" }, { "id": "in_1785045036896_1_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045036896_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785045036896_2_0", "key": "prices", "value": "[2,4,1]" }, { "id": "in_1785045036896_2_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785045036896_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785045036896_3_0", "key": "prices", "value": "[2,1,2,1,0,1,2]" }, { "id": "in_1785045036896_3_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785045036896_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785045036896_4_0", "key": "prices", "value": "[1,2]" }, { "id": "in_1785045036896_4_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785045036896_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785045036896_5_0", "key": "prices", "value": "[2,1]" }, { "id": "in_1785045036896_5_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045036896_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785045036896_6_0", "key": "prices", "value": "[3,3,3,3]" }, { "id": "in_1785045036896_6_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045036896_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785045036896_7_0", "key": "prices", "value": "[1,6,2,8,4,10]" }, { "id": "in_1785045036896_7_1", "key": "output", "value": "9" } ] }, { "id": "tc_1785045036896_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785045036896_8_0", "key": "prices", "value": "[5]" }, { "id": "in_1785045036896_8_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045036896_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785045036896_9_0", "key": "prices", "value": "[]" }, { "id": "in_1785045036896_9_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045036896_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785045036896_10_0", "key": "prices", "value": "[10,9,8,7,6]" }, { "id": "in_1785045036896_10_1", "key": "output", "value": "0" } ] } ], "selectedTestCaseId": "tc_1785045036896_0" } } ] }, { "_id": "2e4ce09b-5bda-4a2e-a8f1-6affc351bd93", "title": "Greedy Stock", "folderId": "af9ad9a9-9d04-4cae-be2a-3aa916f199bd", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "a1b7f320-a711-4f95-ae26-7f1de8be774f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "2. Best Time to Buy and Sell Stock", "styles": {} } ], "children": [] }, { "id": "5372bf69-09c5-4d99-b11c-4aab2f5618dd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "4c2f6684-497f-4f0a-98ef-4904317b6022", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are given an array ", "styles": {} }, { "type": "text", "text": "prices", "styles": { "code": true } }, { "type": "text", "text": " where ", "styles": {} }, { "type": "text", "text": "prices[i]", "styles": { "code": true } }, { "type": "text", "text": " is the price of a given stock on the ", "styles": {} }, { "type": "text", "text": "iᵗʰ", "styles": { "code": true } }, { "type": "text", "text": " day.", "styles": {} } ], "children": [] }, { "id": "03b0d5f1-fe79-45b9-85da-a2f858dd5da3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You want to maximize your profit by choosing ", "styles": {} }, { "type": "text", "text": "one day to buy", "styles": { "bold": true } }, { "type": "text", "text": " the stock and ", "styles": {} }, { "type": "text", "text": "a different future day to sell", "styles": { "bold": true } }, { "type": "text", "text": " the stock.", "styles": {} } ], "children": [] }, { "id": "611363db-8cea-412d-87a5-3616a6134c2c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the maximum profit you can achieve. If no profit is possible, return ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "04debdd4-e911-421c-8cbe-ab21aba49618", "type": "divider", "props": {}, "children": [] }, { "id": "d58a35ea-573e-4799-8d8e-7e8ba666c970", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "95805594-138f-408e-8c20-1bcbb052d2b4", "type": "column_list", "props": {}, "children": [ { "id": "e2f9df9b-31f8-46e1-ac16-3b33fc7cac40", "type": "column", "props": { "width": "1" }, "children": [ { "id": "dcbaf9c7-88f5-4b48-8ff6-98ed694ea03e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "ce0775eb-c9ad-44bd-894b-63069fed0aec", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "f5067873-de1c-4ad6-9893-6282e0072232", "type": "codeBlock", "props": { "language": "javascript", "code": "prices = [7,1,5,3,6,4]" }, "children": [] }, { "id": "eef9bbbd-66a5-4e7a-8699-4becf550e96f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "e322d416-a3c3-4829-be67-6445860ed612", "type": "codeBlock", "props": { "language": "javascript", "code": "5" }, "children": [] }, { "id": "d78f016a-4c0c-411b-920e-ad26a02caeb2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "a3f61ca2-aa86-455c-9b13-46beb3ae1d34", "type": "codeBlock", "props": { "language": "javascript", "code": "Buy on day 2 (price = 1)\nSell on day 5 (price = 6)\n\nProfit = 6 - 1 = 5" }, "children": [] } ] }, { "id": "7eb130fc-5e39-4d74-8d64-1659adfbde23", "type": "column", "props": { "width": "1" }, "children": [ { "id": "2bd9169e-10f1-4f94-b4a8-3b71555309e8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "5a92e457-473f-4efe-9002-1b08bf8c4ba1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "0d590233-6a07-4f99-a083-404ddb70bdfd", "type": "codeBlock", "props": { "language": "javascript", "code": "prices = [7,6,4,3,1]" }, "children": [] }, { "id": "b30b9cb8-da3f-4587-aedf-47215747147b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "6f0439ab-2d4e-4e21-99f8-8d9752cd5796", "type": "codeBlock", "props": { "language": "javascript", "code": "0" }, "children": [] }, { "id": "0c104e86-0e2f-4cb7-bca8-2353eed468ee", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "88a05580-d681-4455-8d06-29d3ac907f0c", "type": "codeBlock", "props": { "language": "javascript", "code": "The stock price keeps decreasing.\nNo profitable transaction is possible." }, "children": [] } ] } ] }, { "id": "fe574049-34a1-41d4-a3f8-867ec18ec850", "type": "divider", "props": {}, "children": [] }, { "id": "f34efdb8-2dd8-44f9-9736-abbd3779eb4f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "076b9590-1d26-4499-aa70-a63d2fe33b3b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The key idea is to keep track of the ", "styles": {} }, { "type": "text", "text": "lowest price seen so far", "styles": { "bold": true } }, { "type": "text", "text": " while traversing the array.", "styles": {} } ], "children": [] }, { "id": "98de4eb8-23d1-4cf1-adb1-68ebc0a4535b", "type": "divider", "props": {}, "children": [] }, { "id": "41e0c79d-c019-4087-9338-ac5a99113843", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "788b35b9-752c-4249-aedd-4ec73c034abb", "type": "addDoc", "props": { "fileId": "2e36f777-4a25-4fac-9ad5-46f45aac9ccd", "title": "Greedy Stock Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "09c44f46-cac2-49c8-8137-7bc15d7bbf07", "type": "divider", "props": {}, "children": [] }, { "id": "630a67fe-c6e8-4b92-923e-aedfb73acf15", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "1a01c298-467e-4fb8-b26a-97f3032d04ea", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "1abb77b1-2d0f-4380-ab50-f6e212dd7643", "type": "divider", "props": {}, "children": [] }, { "id": "4cd8baa0-ac56-46fc-bba9-e5641e67da56", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "6a14bc9e-41a0-4973-9964-df64aa75a5a8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem demonstrates how to optimize a brute-force solution.", "styles": {} } ], "children": [] }, { "id": "cf85a75a-95df-497f-a1b9-0e411be796a0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of checking every possible buy and sell combination (", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": "), maintain the ", "styles": {} }, { "type": "text", "text": "minimum price seen so far", "styles": { "bold": true } }, { "type": "text", "text": " and calculate the profit at each step. This reduces the solution to ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time while using ", "styles": {} }, { "type": "text", "text": "constant extra space", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "abbf7adb-17e8-4c9f-b997-68e9d388ab99", "type": "divider", "props": {}, "children": [] }, { "id": "87252ad9-2fff-4736-b785-53b049b908c0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "d574dfda-b088-48c7-83a9-71e5be7a36df", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Buy at the lowest price seen so far.", "styles": {} } ], "children": [] }, { "id": "9dd826d4-cbf1-45d4-91f6-272a0d71e5bd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Calculate the profit for every future selling price.", "styles": {} } ], "children": [] }, { "id": "c4657f7c-a866-4c42-9218-e48bcc7d96c2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the maximum profit during a single traversal.", "styles": {} } ], "children": [] }, { "id": "2d83a65c-f018-44d4-87bd-e5f03cf8460c", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If no profitable transaction exists, return ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "112951e8-ef5d-456d-9fa1-a16e42618958", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This is a classic ", "styles": {} }, { "type": "text", "text": "array optimization", "styles": { "bold": true } }, { "type": "text", "text": " problem frequently asked in coding interviews.", "styles": {} } ], "children": [] } ] } } ] } ] }, { "_id": "e5a53057-f329-49a2-ae93-1ba0a84228ee", "name": "3: Contains Duplicate", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "167eb17f-ed56-424c-8a4d-88509c811354", "title": "Contains Duplicate Code", "folderId": "e5a53057-f329-49a2-ae93-1ba0a84228ee", "time": 1785346424872, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Solution {\n public boolean solve(int[] A) {\n HashSet Set = new HashSet<>();\n\n for(int i = 0; i< A.length; i++) {\n if(Set.contains(A[i])) {\n return true;\n }\n Set.add(A[i]);\n }\n return false;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"prices\"); \n boolean output = Input.getBool(\"output\");\n System.out.println(\"Expected: \"+ output);\n\n \n Solution sol = new Solution();\n\n\n boolean isDuplicate = sol.solve(nums);\n\n System.out.println(\"isDuplicate: \" + isDuplicate);\n }\n}", "language": "java", "output": "Expected: true\nisDuplicate: false\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785045229183_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785045229183_0_0", "key": "nums", "value": "[1,2,3,1]" }, { "id": "in_1785045229183_0_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785045229183_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785045229183_1_0", "key": "nums", "value": "[1,2,3,4]" }, { "id": "in_1785045229183_1_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785045229183_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785045229183_2_0", "key": "nums", "value": "[5,5,5,5]" }, { "id": "in_1785045229183_2_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785045229183_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785045229183_3_0", "key": "nums", "value": "[10,20,30,40,50]" }, { "id": "in_1785045229183_3_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785045229183_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785045229183_4_0", "key": "nums", "value": "[-1,-2,-3,-1]" }, { "id": "in_1785045229183_4_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785045229183_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785045229183_5_0", "key": "nums", "value": "[0,1,2,3,0]" }, { "id": "in_1785045229183_5_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785045229183_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785045229183_6_0", "key": "nums", "value": "[1000000,2000000,3000000]" }, { "id": "in_1785045229183_6_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785045229183_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785045229183_7_0", "key": "nums", "value": "[9,8,7,6,5,4,3,2,1,9]" }, { "id": "in_1785045229183_7_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785045229183_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785045229183_8_0", "key": "nums", "value": "[]" }, { "id": "in_1785045229183_8_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785045229183_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785045229183_9_0", "key": "nums", "value": "[42]" }, { "id": "in_1785045229183_9_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785045229183_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785045229183_10_0", "key": "nums", "value": "[7,7]" }, { "id": "in_1785045229183_10_1", "key": "output", "value": "true" } ] } ], "selectedTestCaseId": "tc_1785045229183_0" } } ] }, { "_id": "1435f3de-59e2-4f8b-9ffc-c2f440f0a966", "title": "Contains Duplicate", "folderId": "e5a53057-f329-49a2-ae93-1ba0a84228ee", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "d325a9e0-0b04-4fec-8674-e6939cf9cd12", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "3. Contains Duplicate", "styles": {} } ], "children": [] }, { "id": "87eb30a5-4f78-412a-9375-9df6643a8274", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "947a32b3-f26a-4c72-9222-5ad27013b85c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an integer array ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " if any value appears ", "styles": {} }, { "type": "text", "text": "at least twice", "styles": { "bold": true } }, { "type": "text", "text": " in the array, and return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": " if every element is distinct.", "styles": {} } ], "children": [] }, { "id": "2f9bd9af-e43d-4b36-b685-b58cebc3f9d2", "type": "divider", "props": {}, "children": [] }, { "id": "366b94e4-7cda-4e68-a675-a82d26aeeeb2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "d5fd4cd6-423b-4429-881b-72a217107c07", "type": "column_list", "props": {}, "children": [ { "id": "136432b7-9548-4b0f-a8d9-2bd5955c1d26", "type": "column", "props": { "width": "1" }, "children": [ { "id": "bee6dc8c-edda-4ec3-a76b-56c28c1e3a58", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "2199884d-b1ad-4797-9176-9a7aec4321a5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "5f055fc7-10ca-41a4-8e08-a151feb696fe", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1,2,3,1]" }, "children": [] }, { "id": "1a5bc3fc-15c0-4efe-8455-cf6ff1b397b5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "c21a049d-1d11-4661-9de8-c0e6fd31d342", "type": "codeBlock", "props": { "language": "javascript", "code": "true" }, "children": [] }, { "id": "83a6948f-5a04-4f17-881a-96e42d794e83", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "355e9982-6076-4899-9dec-e8615fff933a", "type": "codeBlock", "props": { "language": "javascript", "code": "The number 1 appears twice." }, "children": [] } ] }, { "id": "0bab0dad-1130-42e6-98f3-4ce4312c23db", "type": "column", "props": { "width": "1" }, "children": [ { "id": "f513206b-f52d-449d-9da8-0bbfc024af40", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "dba23264-db59-42f3-a836-572f3393d47e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "63eb79d7-b306-468e-aa62-53308d7c0646", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1,2,3,4]" }, "children": [] }, { "id": "cfbf926c-da66-40b1-9ec6-9bd18c921d12", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "33996c81-e244-4660-9f21-e6f230737c66", "type": "codeBlock", "props": { "language": "javascript", "code": "false" }, "children": [] }, { "id": "28c14f5b-dd9f-448b-8570-6f81382e7ac0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "4196c5f0-563d-449f-9cb7-5494a3bd01ad", "type": "codeBlock", "props": { "language": "javascript", "code": "All elements are unique." }, "children": [] } ] }, { "id": "c90018fc-468a-422e-a75d-ce1cd96b7952", "type": "column", "props": { "width": "1" }, "children": [ { "id": "7b6ca341-65d6-45c0-961d-8bfd4fdcfd62", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "f5b1faee-86b6-4424-8e18-24713fd6aa12", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "66255b46-c514-40d2-8060-32567c6371a8", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1,1,1,3,3,4,3,2,4,2]" }, "children": [] }, { "id": "6f0c7a3d-b58c-4595-8560-3eb36f5d5e34", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "dc2edb92-4a86-4c2a-b304-89bce81bf13c", "type": "codeBlock", "props": { "language": "javascript", "code": "true" }, "children": [] }, { "id": "bc5d1138-b4a1-4620-b877-490bc53f9d95", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "e329250a-ff0f-415f-8205-1bf11a12ea9b", "type": "codeBlock", "props": { "language": "javascript", "code": "Multiple numbers appear more than once." }, "children": [] } ] } ] }, { "id": "2ca6da47-da7f-42e8-bf7e-a85639ce6931", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "ede68120-6315-4ffc-bce7-9c647012e060", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A brute-force approach compares every pair of elements, resulting in ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " time complexity.", "styles": {} } ], "children": [] }, { "id": "648d2e1a-0193-4836-935f-a1fb96e9bd58", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A more efficient solution is to use a ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "7b6c09da-7b57-448e-a34b-0af9c52e011f", "type": "divider", "props": {}, "children": [] }, { "id": "abd6f8b0-cbbb-49ab-9ddf-5c4f77d7fb01", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "fc3ce549-674a-424e-986d-6473d05021ac", "type": "addDoc", "props": { "fileId": "167eb17f-ed56-424c-8a4d-88509c811354", "title": "Contains Duplicate Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "49f0d501-9fbb-4b74-b937-4831b93a960d", "type": "divider", "props": {}, "children": [] }, { "id": "15f0af95-8ff8-4750-b9d5-38f05ea98678", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "3eb95d0a-0c12-4cfa-8539-738d9c829ede", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "09cb31a2-4bc6-4f98-bf35-cd2d69302c8f", "type": "divider", "props": {}, "children": [] }, { "id": "9a6f71d3-5263-4ca8-9a89-777c12a7876e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "4f2f414e-83d9-473f-ae40-5d494110974c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Whenever you're asked to determine whether an element has already been seen, think of using a ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "bold": true } }, { "type": "text", "text": ". It provides constant-time insertion and lookup on average, making it ideal for duplicate detection problems.", "styles": {} } ], "children": [] }, { "id": "cf72b33d-3829-4ff9-b071-2c8f62b44db9", "type": "divider", "props": {}, "children": [] }, { "id": "b2a2341e-a14d-482a-9e80-6d2da6830af8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "935dfe03-c8d6-4447-9e04-3162568d4310", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "code": true } }, { "type": "text", "text": " to keep track of visited elements.", "styles": {} } ], "children": [] }, { "id": "a669fef2-ff47-4a84-91b2-31bcc48afc21", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether an element already exists before adding it.", "styles": {} } ], "children": [] }, { "id": "0a20099f-40d8-49c9-82d7-ebaef373b00f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return immediately when a duplicate is found.", "styles": {} } ], "children": [] }, { "id": "10413504-b529-4b25-80fd-28f908b04f88", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the traversal completes without duplicates, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "a4af0fbe-3ff7-49cf-ad82-55516543fafa", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This is one of the most common applications of the ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "bold": true } }, { "type": "text", "text": " data structure in coding interviews.", "styles": {} } ], "children": [] } ] } } ] } ] }, { "_id": "ffc0bf39-5e33-4afe-bd23-1e69643541e4", "name": "4: Product of Array", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "d73e9fd6-c473-4485-9b28-a4a4e638802c", "title": "Except Self Code", "folderId": "ffc0bf39-5e33-4afe-bd23-1e69643541e4", "time": 1785346431007, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Solution {\n public void solve(int[] A) {\n int[] Ans = new int[A.length];\n Arrays.fill(Ans, 1);\n\n // Preifx Skip\n int prefix = 1;\n for(int i = 0; i< A.length; i++) {\n Ans[i] *= prefix;\n prefix *= A[i];\n }\n\n // Suffix Skip\n int suffix = 1;\n for( int i= A.length - 1; i >= 0; i--) {\n Ans[i] *= suffix;\n suffix *= A[i];\n }\n\n // Printing Array\n System.out.print(Arrays.toString(Ans));\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"nums\"); \n int[] output = Input.getIntArray(\"output\");\n \n System.out.println(\"Expected: \"+ Arrays.toString(output));\n \n Solution sol = new Solution();\n\n sol.solve(nums);\n }\n}", "language": "java", "output": "Expected: [24, 12, 8, 6]\n[24, 12, 8, 6]", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785045465768_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785045465768_0_0", "key": "nums", "value": "[1,2,3,4]" }, { "id": "in_1785045465768_0_1", "key": "output", "value": "[24,12,8,6]" } ] }, { "id": "tc_1785045465768_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785045465768_1_0", "key": "nums", "value": "[-1,1,0,-3,3]" }, { "id": "in_1785045465768_1_1", "key": "output", "value": "[0,0,9,0,0]" } ] }, { "id": "tc_1785045465768_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785045465768_2_0", "key": "nums", "value": "[2,3,4,5]" }, { "id": "in_1785045465768_2_1", "key": "output", "value": "[60,40,30,24]" } ] }, { "id": "tc_1785045465768_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785045465768_3_0", "key": "nums", "value": "[5,1]" }, { "id": "in_1785045465768_3_1", "key": "output", "value": "[1,5]" } ] }, { "id": "tc_1785045465769_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785045465768_4_0", "key": "nums", "value": "[1,1,1,1]" }, { "id": "in_1785045465769_4_1", "key": "output", "value": "[1,1,1,1]" } ] }, { "id": "tc_1785045465769_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785045465769_5_0", "key": "nums", "value": "[0,2,3,4]" }, { "id": "in_1785045465769_5_1", "key": "output", "value": "[24,0,0,0]" } ] }, { "id": "tc_1785045465769_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785045465769_6_0", "key": "nums", "value": "[2,0,4,0]" }, { "id": "in_1785045465769_6_1", "key": "output", "value": "[0,0,0,0]" } ] }, { "id": "tc_1785045465769_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785045465769_7_0", "key": "nums", "value": "[-2,-3,-4]" }, { "id": "in_1785045465769_7_1", "key": "output", "value": "[12,8,6]" } ] }, { "id": "tc_1785045465769_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785045465769_8_0", "key": "nums", "value": "[10]" }, { "id": "in_1785045465769_8_1", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785045465769_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785045465769_9_0", "key": "nums", "value": "[0]" }, { "id": "in_1785045465769_9_1", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785045465769_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785045465769_10_0", "key": "nums", "value": "[0,0,5]" }, { "id": "in_1785045465769_10_1", "key": "output", "value": "[0,0,0]" } ] } ], "selectedTestCaseId": "tc_1785045465768_0" } } ] }, { "_id": "67756693-31a6-4829-9a37-3d620d4f241d", "title": "Except Self Doc", "folderId": "ffc0bf39-5e33-4afe-bd23-1e69643541e4", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "a12477f7-93dc-40fc-b4cb-78f29c31a305", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "4. Product of Array Except Self", "styles": {} } ], "children": [] }, { "id": "ed8ec343-8d17-47ef-8a76-0e2aa9437419", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "0283837e-32cf-4358-a782-ae5f4f2d53b3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an integer array ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": ", return an array ", "styles": {} }, { "type": "text", "text": "answer", "styles": { "code": true } }, { "type": "text", "text": " such that:", "styles": {} } ], "children": [] }, { "id": "d0e85f5d-63dc-4b10-ac3b-12d3a967af55", "type": "codeBlock", "props": { "language": "javascript", "code": "answer[i] = product of all the elements of nums except nums[i]" }, "children": [] }, { "id": "19269ef6-03f9-4748-89fe-707a247103d1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You must solve the problem ", "styles": {} }, { "type": "text", "text": "without using the division operator", "styles": { "bold": true } }, { "type": "text", "text": " and in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] }, { "id": "7cf43616-d4d8-4623-9cc9-914acf7feb57", "type": "divider", "props": {}, "children": [] }, { "id": "b1cef938-0e88-41b6-ae75-695f576d9de2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "f1112044-59ca-4352-9e1e-4cc06971d8c0", "type": "column_list", "props": {}, "children": [ { "id": "11e82fbf-6e69-4895-aac6-9202da59c1c4", "type": "column", "props": { "width": "1" }, "children": [ { "id": "4765ac00-7406-483c-8409-e1c1b3f3d8d0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "958e20e4-a16d-41f2-8a07-a4f786e8e597", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "99b3a419-ae47-49be-8b7e-45a7e35eff99", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1,2,3,4]\nPrefix = [1, 2, 6, 24]\nSuffix = [24, 24, 12, 4]" }, "children": [] }, { "id": "a5f250f0-d5ee-48c5-ab7a-7bbbaa51cc01", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "df088a5f-fa1d-4c1c-b02a-834a386878d5", "type": "codeBlock", "props": { "language": "javascript", "code": "[24,12,8,6]" }, "children": [] }, { "id": "2b6a1774-e7d6-4f04-860d-db6a8c9760b8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "4dd9c7ac-feda-46c7-9370-6911c030e1fc", "type": "codeBlock", "props": { "language": "javascript", "code": "answer[0] = 2 × 3 × 4 = 24\nanswer[1] = 1 × 3 × 4 = 12\nanswer[2] = 1 × 2 × 4 = 8\nanswer[3] = 1 × 2 × 3 = 6" }, "children": [] } ] }, { "id": "9bb6def5-11d7-4809-8dc8-7b448450fdb0", "type": "column", "props": { "width": "1" }, "children": [ { "id": "96a57576-2371-4cb9-9ff7-9454363030df", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "ce8c4810-6321-4ed3-9156-98cc7135acd2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "027f16fa-dc4a-4b1d-8f80-846585df4461", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [-1,1,0,-3,3]" }, "children": [] }, { "id": "e5efad65-f24a-44c8-bffd-d01828fd3089", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "40b38a0b-4c3a-426c-a970-5042bd0cc69f", "type": "codeBlock", "props": { "language": "javascript", "code": "[0,0,9,0,0]" }, "children": [] }, { "id": "a224192d-729f-4b4f-972e-b48b7712aa3a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "d655b7a8-eebe-469b-84d8-d6675b0f4180", "type": "codeBlock", "props": { "language": "javascript", "code": "Since one element is zero, only the position corresponding to zero gets the product of all non-zero elements.\nAll other positions become 0." }, "children": [] } ] } ] }, { "id": "95b685e1-9959-4cd6-873a-fb5b3ca96bda", "type": "divider", "props": {}, "children": [] }, { "id": "ac75c2a3-08a2-4afb-8729-574809fd9a48", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "c2fd7a0a-a3a5-447c-b152-af777963e732", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of using division, compute the answer using ", "styles": {} }, { "type": "text", "text": "Prefix Pass", "styles": { "bold": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "suffix Pass", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "6094ecd4-3daa-4e5b-ac03-6b3a33a9c079", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "5a20b310-5894-44ff-a9b2-215b6a2c65dd", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create an answer array initialized with ", "styles": {} }, { "type": "text", "text": "1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "33a3f297-48ec-47e6-93a7-204e52cd5f5e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse from left to right and store the product of all elements before the current index.", "styles": {} } ], "children": [] }, { "id": "dad76c06-cb0e-49cd-b457-5db3dd22ec3a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse from right to left while maintaining a suffix product.", "styles": {} } ], "children": [] }, { "id": "18e9516f-9155-4a7d-abd6-ecd22d545b38", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Multiply the current answer with the suffix product.", "styles": {} } ], "children": [] }, { "id": "1c49b4c5-d49d-48b9-9be8-6487fd80fd27", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the answer array.", "styles": {} } ], "children": [] }, { "id": "410bab6e-ab1a-470a-bc50-a619ffefed3d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This approach requires only two traversals of the array.", "styles": {} } ], "children": [] }, { "id": "e6cbc31d-333b-4170-bb4e-29a6f87d07f6", "type": "divider", "props": {}, "children": [] }, { "id": "1b1495b7-02d3-4265-91e8-7cf251653633", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "5572f5bd-724f-4817-89c5-de7881d576d4", "type": "addDoc", "props": { "fileId": "d73e9fd6-c473-4485-9b28-a4a4e638802c", "title": "Except Self Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "90457f2a-ddbe-4786-8e31-1cee49bf88bd", "type": "divider", "props": {}, "children": [] }, { "id": "e3287ad4-a253-4b1b-912f-7b893641a4ea", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "2443f606-aa02-47ab-9101-23d56680ddde", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " ", "styles": {} }, { "type": "text", "text": "(excluding the output array)", "styles": { "italic": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "f0dd8137-1ff4-4e5c-bda2-72da2b91b9c9", "type": "divider", "props": {}, "children": [] }, { "id": "15979682-118a-45a2-8e06-d0fb4956e38e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "643bf51c-a7c4-4a45-8271-a58f1e2e9b0f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem is a classic example of using ", "styles": {} }, { "type": "text", "text": "prefix and suffix products", "styles": { "bold": true } }, { "type": "text", "text": ". The interviewer expects you to avoid division and achieve ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time complexity. Try to optimize the solution further by avoiding extra prefix and suffix arrays and using only the output array plus one suffix variable.", "styles": {} } ], "children": [] }, { "id": "d12750e4-d7f5-4a48-8831-a23f48957b06", "type": "divider", "props": {}, "children": [] }, { "id": "9170fc49-0df7-41f6-a098-5cb9a63d7e31", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "ea2b1472-00c7-4571-a354-a9124a79401c", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Avoid using the division operator.", "styles": {} } ], "children": [] }, { "id": "1f4809b0-3964-43cd-96a2-59d0d60c6a60", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compute prefix products in one pass.", "styles": {} } ], "children": [] }, { "id": "3e4a99c8-ad2c-484b-b555-37c18d0fda75", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compute suffix products in a second pass.", "styles": {} } ], "children": [] }, { "id": "1977c947-e7e6-451a-b29a-5177d98127e9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Combine prefix and suffix products to build the final answer.", "styles": {} } ], "children": [] }, { "id": "cabbe271-8c2f-44a5-a9cc-383b339fa08f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time with ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space (excluding the output array).", "styles": {} } ], "children": [] } ] } } ] } ] }, { "_id": "08f0049c-6416-4831-8ffc-96f2332499ca", "name": "5: Maximum Subarray", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "1d579020-a549-44a8-920e-9b15ac76afa7", "title": "Maximum Subarray Code", "folderId": "08f0049c-6416-4831-8ffc-96f2332499ca", "time": 1785346436283, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n public int solve(int[] A) {\n int CurrentSum = A[0];\n int MaxSum = A[0];\n\n if(A.length == 0) return 0;\n if(A.length == 1) return CurrentSum;\n \n for(int i = 1; i< A.length; i++) {\n if(CurrentSum > 0) {\n CurrentSum += A[i];\n } else {\n CurrentSum = A[i];\n }\n \n MaxSum = Math.max(MaxSum, CurrentSum);\n }\n\n return MaxSum;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"nums\"); \n int output = Input.getInt(\"output\");\n \n System.out.println(\"Expected: \"+ output);\n \n Solution sol = new Solution();\n\n int Profit = sol.solve(nums);\n\n System.out.println(\"Max Sum: \" + Profit);\n }\n}", "language": "java", "output": "Expected: 6\nMax Sum: 6\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785045868289_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785045868289_0_0", "key": "nums", "value": "[-2,1,-3,4,-1,2,1,-5,4]" }, { "id": "in_1785045868289_0_1", "key": "output", "value": "6" } ] }, { "id": "tc_1785045868289_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785045868289_1_0", "key": "nums", "value": "[1]" }, { "id": "in_1785045868289_1_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785045868289_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785045868289_2_0", "key": "nums", "value": "[5,4,-1,7,8]" }, { "id": "in_1785045868289_2_1", "key": "output", "value": "23" } ] }, { "id": "tc_1785045868289_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785045868289_3_0", "key": "nums", "value": "[-1,-2,-3,-4]" }, { "id": "in_1785045868289_3_1", "key": "output", "value": "-1" } ] }, { "id": "tc_1785045868289_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785045868289_4_0", "key": "nums", "value": "[2,-1,2,3,4,-5]" }, { "id": "in_1785045868289_4_1", "key": "output", "value": "10" } ] }, { "id": "tc_1785045868289_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785045868289_5_0", "key": "nums", "value": "[-2,-1]" }, { "id": "in_1785045868289_5_1", "key": "output", "value": "-1" } ] }, { "id": "tc_1785045868289_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785045868289_6_0", "key": "nums", "value": "[0,0,0]" }, { "id": "in_1785045868289_6_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785045868289_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785045868289_7_0", "key": "nums", "value": "[8,-19,5,-4,20]" }, { "id": "in_1785045868289_7_1", "key": "output", "value": "21" } ] }, { "id": "tc_1785045868289_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785045868289_8_0", "key": "nums", "value": "[3,-2,5,-1]" }, { "id": "in_1785045868289_8_1", "key": "output", "value": "6" } ] }, { "id": "tc_1785045868289_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785045868289_9_0", "key": "nums", "value": "[-5,4,-1,7,-8,6]" }, { "id": "in_1785045868289_9_1", "key": "output", "value": "10" } ] }, { "id": "tc_1785045868289_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785045868289_10_0", "key": "nums", "value": "[10]" }, { "id": "in_1785045868289_10_1", "key": "output", "value": "10" } ] }, { "id": "tc_1785045868289_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785045868289_11_0", "key": "nums", "value": "[-10]" }, { "id": "in_1785045868289_11_1", "key": "output", "value": "-10" } ] }, { "id": "tc_1785045868289_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785045868289_12_0", "key": "nums", "value": "[0]" }, { "id": "in_1785045868289_12_1", "key": "output", "value": "0" } ] } ], "selectedTestCaseId": "tc_1785045868289_0" } } ] }, { "_id": "2b835e02-8bfe-473b-b765-74992935e63d", "title": "Maximum Subarray Doc", "folderId": "08f0049c-6416-4831-8ffc-96f2332499ca", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "f2a4b006-c92c-42f5-9f0f-96c70f7ae680", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "5. Maximum Subarray (Kadane's Algorithm)", "styles": {} } ], "children": [] }, { "id": "f050fca6-4cfd-4a4d-9802-c8f918a7e4e4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "ea476899-f4d7-4c67-9c5b-396641fd4c8d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an integer array ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": ", find the contiguous subarray (containing at least one number) that has the largest sum and return its sum.", "styles": {} } ], "children": [] }, { "id": "58c80b7f-f37d-4965-ab10-e49c3bc26236", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A ", "styles": {} }, { "type": "text", "text": "subarray", "styles": { "bold": true } }, { "type": "text", "text": " is a contiguous part of an array.", "styles": {} } ], "children": [] }, { "id": "6e5cefec-88e0-4817-a9ed-b9e2b586f58e", "type": "divider", "props": {}, "children": [] }, { "id": "6ba5f4fb-218d-45b9-8c2d-fa7d5da49f7a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "ac223b4b-f331-4f78-92c4-5f9632fc7a54", "type": "column_list", "props": {}, "children": [ { "id": "1725cd7b-99bc-4a0f-b2ef-7645ee2a9257", "type": "column", "props": { "width": "1" }, "children": [ { "id": "71b33a80-f969-4605-933a-70057f42dfbe", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "45515bd4-b493-4310-9bd3-f44c60a254ca", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "7e7df997-8bf6-4f62-b33f-da5661cecfe7", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [-2,1,-3,4,-1,2,1,-5,4]" }, "children": [] }, { "id": "de55d584-6198-48d0-abcc-b561ae6d591f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "61b52c4c-07cd-4d5d-a72b-890f6a2814c7", "type": "codeBlock", "props": { "language": "javascript", "code": "6" }, "children": [] }, { "id": "57dca1a1-75a2-4376-aac6-06a1d1e60905", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "735867c6-7b23-4953-a3be-265f2655b3d3", "type": "codeBlock", "props": { "language": "javascript", "code": "The subarray [4,-1,2,1] has the largest sum.\n\n4 + (-1) + 2 + 1 = 6" }, "children": [] } ] }, { "id": "a992bfc3-cfe7-4be2-aa79-4dea9111ebf4", "type": "column", "props": { "width": "1" }, "children": [ { "id": "77a0aa72-0f44-496c-b22d-774ba11f239c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "dc187cfb-dab3-4259-98d6-4541c2545385", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "572b04af-c87c-4f7b-b0d9-87b986e8c351", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1]" }, "children": [] }, { "id": "170dd713-7104-4f96-b9b9-ec94d0f32698", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "41c5a767-c068-43cf-ae3c-983884f625b1", "type": "codeBlock", "props": { "language": "javascript", "code": "1" }, "children": [] } ] }, { "id": "d69ab754-01bb-47d8-ae27-644271bccb46", "type": "column", "props": { "width": "1" }, "children": [ { "id": "dc57cd28-4d4f-4fb3-ae58-e60608f2c4ef", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "4d9f9c2e-4770-4cb5-b52c-999f9ac523b0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "cd59e3e5-8bf9-482f-814a-e3999c2cd670", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [5,4,-1,7,8]" }, "children": [] }, { "id": "62ee2fc7-e715-40dd-847e-a7297d83712b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "94b96d65-4c6c-47a0-b55c-b3f76fa230c8", "type": "codeBlock", "props": { "language": "javascript", "code": "23" }, "children": [] } ] } ] }, { "id": "e719c3fa-0e8b-48a8-aca7-4269250742e5", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "89f494c9-39c7-4ff7-b753-a621ee68b6df", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A brute-force solution checks every possible subarray, resulting in ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " time complexity.", "styles": {} } ], "children": [] }, { "id": "a6fb99f0-737c-4fe7-a490-87ed9a405127", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Kadane's Algorithm solves the problem in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time by maintaining:", "styles": {} } ], "children": [] }, { "id": "3679927d-4143-4ca5-8f92-c270bd08ab92", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "currentSum", "styles": { "bold": true } }, { "type": "text", "text": " → Maximum subarray sum ending at the current index.", "styles": {} } ], "children": [] }, { "id": "85270080-0c04-4746-b97f-9e651644342b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "maxSum", "styles": { "bold": true } }, { "type": "text", "text": " → Maximum subarray sum found so far.", "styles": {} } ], "children": [] }, { "id": "330fb79a-cbcf-45cb-b4dd-f07aed2d1fed", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "52b93803-f6e5-49e4-9422-35bc59a81243", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Initialize both ", "styles": {} }, { "type": "text", "text": "currentSum", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "maxSum", "styles": { "code": true } }, { "type": "text", "text": " with the first element.", "styles": {} } ], "children": [] }, { "id": "04ecded0-ccd9-4ed9-b520-2398b01c854f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse the array from the second element.", "styles": {} } ], "children": [] }, { "id": "5bb360c5-ffba-4cb2-9855-609342a25a7a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Decide whether to:", "styles": {} } ], "children": [ { "id": "960c8b01-3fca-4d97-9196-2817168002da", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Start a new subarray from the current element.", "styles": {} } ], "children": [] }, { "id": "4b0ef3dd-bbdf-4c71-87ed-065b9e38940d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Extend the existing subarray.", "styles": {} } ], "children": [] } ] }, { "id": "efcd5213-2938-43f8-bca8-c1c8a601dc1e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the maximum sum found so far.", "styles": {} } ], "children": [] }, { "id": "4d577f59-b941-4cd9-a580-85a3b032e366", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "maxSum", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "711e262c-018e-4d54-a11a-65655d69d84e", "type": "divider", "props": {}, "children": [] }, { "id": "377d2d29-c976-42c4-8525-0b7f4c3ae819", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "7ad95b4d-f973-4fbb-9df0-69453d92c0e0", "type": "addDoc", "props": { "fileId": "1d579020-a549-44a8-920e-9b15ac76afa7", "title": "Maximum Subarray Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "ce8d35af-f393-4f2d-bce1-0c88c93656c4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "5082e515-861a-4a0e-96a5-2a63d7089d6e", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "c2ea27bb-6de3-425b-9d99-a002fddd57e2", "type": "divider", "props": {}, "children": [] }, { "id": "eaebe5a8-363b-4572-87af-d7b38bf0e79b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "e1838a5e-b92b-4256-8d92-e21e6af037b3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Kadane's Algorithm is one of the most frequently asked dynamic programming problems. The key insight is that if the running sum becomes smaller than the current element, it is better to start a new subarray rather than continue the existing one.", "styles": {} } ], "children": [] }, { "id": "02db2acd-2d90-4970-974e-6857ede03bc2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Remember this decision:", "styles": {} } ], "children": [] }, { "id": "c1d4baef-1a0f-4fb7-bc63-59ea8cc62d24", "type": "codeBlock", "props": { "language": "javascript", "code": "currentSum = max(currentElement, currentSum + currentElement)" }, "children": [] }, { "id": "4ba2a333-c288-4b7a-b0d5-b02e935bc22a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This single expression forms the core of Kadane's Algorithm.", "styles": {} } ], "children": [] }, { "id": "623f9d06-7fbf-4cbf-b3fd-0eec119b4399", "type": "divider", "props": {}, "children": [] }, { "id": "3c61fb7d-8d5c-4fa3-afa5-0767c0dcbb71", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "39346c6e-976e-41af-bbc5-6bcef816fb4f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A subarray must consist of contiguous elements.", "styles": {} } ], "children": [] }, { "id": "983d8c30-ad95-4340-8c1e-e61301135ee6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Brute-force requires checking all possible subarrays and runs in ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] }, { "id": "dd3ea647-73cd-4e9e-8e64-2ff4d5b365c2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Kadane's Algorithm solves the problem in a single traversal.", "styles": {} } ], "children": [] }, { "id": "e2e98a1c-8085-4721-b721-d19ee38dd13c", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "At each step, decide whether to extend the current subarray or start a new one.", "styles": {} } ], "children": [] }, { "id": "aad3d53d-00a4-4606-94c5-ea1802623f52", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain both the current subarray sum and the maximum sum found so far.", "styles": {} } ], "children": [] }, { "id": "d7d6f56d-f70a-4f08-8123-d4a54132e6ca", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] }, { "id": "03e329e1-08c5-43c2-b8ee-95e08d1560de", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } } ] } ] }, { "_id": "04e9fe15-1d91-4ec2-8582-2c5b38f422e9", "name": "6: Merge Sorted Array", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "387058c7-0591-440d-9354-64e2cde35186", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "132b00fc-7532-4641-80dd-234355e8068d", "title": "Merge Sorted Array Code", "folderId": "04e9fe15-1d91-4ec2-8582-2c5b38f422e9", "time": 1785346441802, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Solution {\n public void solve(int[] nums1, int m, int[] nums2, int n) {\n int length = m + n;\n\n int P1 = m - 1, P2 = n - 1, P3 = m + n -1;\n \n while(P1 >= 0 && P2 >= 0) {\n if(nums1[P1] >= nums2[P2]) {\n nums1[P3] = nums1[P1];\n P1--;\n } else {\n nums1[P3] = nums2[P2];\n P2--;\n }\n P3--;\n }\n\n while(P1 >= 0) {\n nums1[P3] = nums1[P1];\n P1--;\n P3--;\n }\n\n while(P2 >= 0) {\n nums1[P3] = nums2[P2];\n P2--;\n P3--;\n }\n\n // Printing Array\n System.out.println(Arrays.toString(nums1));\n\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums1 = Input.getIntArray(\"nums1\");\n int m = Input.getInt(\"m\");\n int[] nums2 = Input.getIntArray(\"nums2\");\n int n = Input.getInt(\"n\");\n int[] output = Input.getIntArray(\"output\");\n \n System.out.println(\"Expected: \"+ Arrays.toString(output));\n \n Solution sol = new Solution();\n\n sol.solve(nums1, m, nums2, n);\n\n // System.out.println(\"Max Sum: \" + Profit);\n }\n}", "language": "java", "output": "Expected: [1, 2, 2, 3, 5, 6]\n[1, 2, 2, 3, 5, 6]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785045987823_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785045987823_0_0", "key": "nums1", "value": "[1,2,3,0,0,0]" }, { "id": "in_1785045987823_0_1", "key": "m", "value": "3" }, { "id": "in_1785045987823_0_2", "key": "nums2", "value": "[2,5,6]" }, { "id": "in_1785045987823_0_3", "key": "n", "value": "3" }, { "id": "in_1785045987823_0_4", "key": "output", "value": "[1,2,2,3,5,6]" } ] }, { "id": "tc_1785045987823_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785045987823_1_0", "key": "nums1", "value": "[1]" }, { "id": "in_1785045987823_1_1", "key": "m", "value": "1" }, { "id": "in_1785045987823_1_2", "key": "nums2", "value": "[]" }, { "id": "in_1785045987823_1_3", "key": "n", "value": "0" }, { "id": "in_1785045987823_1_4", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785045987823_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785045987823_2_0", "key": "nums1", "value": "[0]" }, { "id": "in_1785045987823_2_1", "key": "m", "value": "0" }, { "id": "in_1785045987823_2_2", "key": "nums2", "value": "[1]" }, { "id": "in_1785045987823_2_3", "key": "n", "value": "1" }, { "id": "in_1785045987823_2_4", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785045987823_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785045987823_3_0", "key": "nums1", "value": "[2,0]" }, { "id": "in_1785045987823_3_1", "key": "m", "value": "1" }, { "id": "in_1785045987823_3_2", "key": "nums2", "value": "[1]" }, { "id": "in_1785045987823_3_3", "key": "n", "value": "1" }, { "id": "in_1785045987823_3_4", "key": "output", "value": "[1,2]" } ] }, { "id": "tc_1785045987823_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785045987823_4_0", "key": "nums1", "value": "[4,5,6,0,0,0]" }, { "id": "in_1785045987823_4_1", "key": "m", "value": "3" }, { "id": "in_1785045987823_4_2", "key": "nums2", "value": "[1,2,3]" }, { "id": "in_1785045987823_4_3", "key": "n", "value": "3" }, { "id": "in_1785045987823_4_4", "key": "output", "value": "[1,2,3,4,5,6]" } ] }, { "id": "tc_1785045987823_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785045987823_5_0", "key": "nums1", "value": "[1,2,4,5,6,0]" }, { "id": "in_1785045987823_5_1", "key": "m", "value": "5" }, { "id": "in_1785045987823_5_2", "key": "nums2", "value": "[3]" }, { "id": "in_1785045987823_5_3", "key": "n", "value": "1" }, { "id": "in_1785045987823_5_4", "key": "output", "value": "[1,2,3,4,5,6]" } ] }, { "id": "tc_1785045987823_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785045987823_6_0", "key": "nums1", "value": "[2,2,3,0,0,0]" }, { "id": "in_1785045987823_6_1", "key": "m", "value": "3" }, { "id": "in_1785045987823_6_2", "key": "nums2", "value": "[1,2,2]" }, { "id": "in_1785045987823_6_3", "key": "n", "value": "3" }, { "id": "in_1785045987823_6_4", "key": "output", "value": "[1,2,2,2,2,3]" } ] }, { "id": "tc_1785045987823_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785045987823_7_0", "key": "nums1", "value": "[5,6,7,0,0,0]" }, { "id": "in_1785045987823_7_1", "key": "m", "value": "3" }, { "id": "in_1785045987823_7_2", "key": "nums2", "value": "[1,2,3]" }, { "id": "in_1785045987823_7_3", "key": "n", "value": "3" }, { "id": "in_1785045987823_7_4", "key": "output", "value": "[1,2,3,5,6,7]" } ] }, { "id": "tc_1785045987823_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785045987823_8_0", "key": "nums1", "value": "[0]" }, { "id": "in_1785045987823_8_1", "key": "m", "value": "0" }, { "id": "in_1785045987823_8_2", "key": "nums2", "value": "[]" }, { "id": "in_1785045987823_8_3", "key": "n", "value": "0" }, { "id": "in_1785045987823_8_4", "key": "output", "value": "[0]" } ] }, { "id": "tc_1785045987824_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785045987824_9_0", "key": "nums1", "value": "[1,2,3]" }, { "id": "in_1785045987824_9_1", "key": "m", "value": "3" }, { "id": "in_1785045987824_9_2", "key": "nums2", "value": "[]" }, { "id": "in_1785045987824_9_3", "key": "n", "value": "0" }, { "id": "in_1785045987824_9_4", "key": "output", "value": "[1,2,3]" } ] }, { "id": "tc_1785045987824_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785045987824_10_0", "key": "nums1", "value": "[0,0,0]" }, { "id": "in_1785045987824_10_1", "key": "m", "value": "0" }, { "id": "in_1785045987824_10_2", "key": "nums2", "value": "[2,5,6]" }, { "id": "in_1785045987824_10_3", "key": "n", "value": "3" }, { "id": "in_1785045987824_10_4", "key": "output", "value": "[2,5,6]" } ] } ], "selectedTestCaseId": "tc_1785045987823_0" } } ] }, { "_id": "2a2d08cb-1c8b-4688-97d5-21bee0910603", "title": "Merge Sorted Array Doc", "folderId": "04e9fe15-1d91-4ec2-8582-2c5b38f422e9", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "3924b7ea-1cd4-4059-b1f5-aa652fcf76a4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "6. Merge Sorted Array", "styles": {} } ], "children": [] }, { "id": "eeb94337-1621-4494-9430-914ae5115fa9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "cfff120c-151d-45db-9964-de1e4eef2b4d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are given two integer arrays ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": ", sorted in ", "styles": {} }, { "type": "text", "text": "non-decreasing order", "styles": { "bold": true } }, { "type": "text", "text": ", and two integers ", "styles": {} }, { "type": "text", "text": "m", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": ", representing the number of elements in ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": ", respectively.", "styles": {} } ], "children": [] }, { "id": "17e9c7e1-ca12-4392-8008-2bcc9e317ca3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Merge ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": " into ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": " as one sorted array.", "styles": {} } ], "children": [] }, { "id": "3f1b01b3-b9c9-43b9-b575-bb7f47654d26", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The final sorted array should be stored inside ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "ed0c5fd3-7365-4455-9e61-8b935a23c5e5", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "Note:", "styles": { "bold": true } }, { "type": "text", "text": " ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": " has a size of ", "styles": {} }, { "type": "text", "text": "m + n", "styles": { "code": true } }, { "type": "text", "text": ", where the last ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": " elements are set to ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": " and should be ignored. These zeros are placeholders for merging.", "styles": {} } ], "children": [] }, { "id": "0a72dd51-ce9d-46f2-9303-f7f5b9d853ac", "type": "divider", "props": {}, "children": [] }, { "id": "016522a9-60f8-4741-9de8-67e4b092f5b9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "030985eb-8ce0-463d-b2e1-c6b6465b2275", "type": "column_list", "props": {}, "children": [ { "id": "7b638b56-7a26-4481-bc23-fc551066b6d4", "type": "column", "props": { "width": "1" }, "children": [ { "id": "7636266d-45ec-4056-baa3-ee451cf91c44", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "c189c1ea-ac0c-4c56-9a51-4637a69c25d4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "9984a6ec-f021-472e-a010-ffc8a55c7ce9", "type": "codeBlock", "props": { "language": "javascript", "code": "nums1 = [1,2,3,0,0,0]\nm = 3\n\nnums2 = [2,5,6]\nn = 3" }, "children": [] }, { "id": "1ca6f9b0-3ed8-4ee4-8f57-8800e9fa9f6c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "4d0593e8-d4d8-4078-8db5-4fdc8b66256c", "type": "codeBlock", "props": { "language": "javascript", "code": "[1,2,2,3,5,6]" }, "children": [] } ] }, { "id": "2ddac010-03ce-49d1-b587-bf0c3adc2d10", "type": "column", "props": { "width": "1" }, "children": [ { "id": "2a2f9508-c406-40bb-8028-2b10b4aac62f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "69334479-5a8b-4591-af0f-c1cb7390cde7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "72a261d6-0139-456e-805a-39206f323b9e", "type": "codeBlock", "props": { "language": "javascript", "code": "nums1 = [1]\nm = 1\n\nnums2 = []\nn = 0" }, "children": [] }, { "id": "c4d91be0-82c0-4bcf-874a-db2e8b8c5a77", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "c1420950-13d3-4863-8665-9386e2ca413e", "type": "codeBlock", "props": { "language": "javascript", "code": "[1]" }, "children": [] } ] }, { "id": "9b9b8b8b-30b5-4fb6-8474-f0411ab23c09", "type": "column", "props": { "width": "1" }, "children": [ { "id": "b41be39d-86dd-47e5-b721-c57496dce979", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "cbd1aa1c-3f82-421b-8047-4c31a4a50b23", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "b615fe99-d4ce-4f3e-a0ab-89e4bf0aaab2", "type": "codeBlock", "props": { "language": "javascript", "code": "nums1 = [0]\nm = 0\n\nnums2 = [1]\nn = 1" }, "children": [] }, { "id": "c12b47f7-4edc-400e-80e0-16dfc6f77f56", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "fad7fa62-403f-43c2-9605-8d03847099b4", "type": "codeBlock", "props": { "language": "javascript", "code": "[1]" }, "children": [] } ] } ] }, { "id": "75efa772-0989-411b-a72c-9e0b5bd08281", "type": "divider", "props": {}, "children": [] }, { "id": "30901472-2410-46f7-8b07-7f331cb94e84", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "7e16af57-214d-4040-a455-c91a8fef4265", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Since both arrays are already sorted, the most efficient approach is to merge them ", "styles": {} }, { "type": "text", "text": "from the end", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "12c0e4b3-024b-49f8-b2b6-38b9b7742e5a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If we merge from the beginning, elements in ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": " would need to be shifted repeatedly.", "styles": {} } ], "children": [] }, { "id": "a033977f-35a7-4ba1-8045-7714f7d3b138", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead:", "styles": {} } ], "children": [] }, { "id": "71bb4f70-6fc1-46b4-9b71-520f01e37625", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Place one pointer at the last valid element of ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "48c34a12-9edf-40de-bd3b-06ea9b55d409", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Place another pointer at the last element of ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "98522b23-3548-4cf2-87bb-4d9f25b36ae7", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare both elements.", "styles": {} } ], "children": [] }, { "id": "bc19bdba-f148-4aa5-a05f-a555ad72a23b", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Insert the larger element at the last available position.", "styles": {} } ], "children": [] }, { "id": "62231e1b-6ded-40e8-ae9c-a3d571e26323", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until all elements of ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": " are merged.", "styles": {} } ], "children": [] }, { "id": "9c72c337-f6eb-4859-8391-2d1afe2c2035", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This avoids unnecessary shifting and completes the merge in one traversal.", "styles": {} } ], "children": [] }, { "id": "668d1f36-9664-4ec2-8eae-80ff66b9d770", "type": "divider", "props": {}, "children": [] }, { "id": "a86ebfdf-349b-4910-8f73-6327c052034a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "583e1f8a-a5ad-4759-a2bd-6304a89e5d48", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] }, { "id": "00144e70-9326-498a-a0d4-b0f74b286ae5", "type": "divider", "props": {}, "children": [] }, { "id": "0e49e67c-01af-4b55-b856-2ede54cf35fa", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "d6728ad1-a298-4cd6-bed9-ac6610498a75", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(m + n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "32c7fa7c-3aac-42c9-91c0-e073f4319015", "type": "divider", "props": {}, "children": [] }, { "id": "cd4ac459-3e06-4cdc-ac7c-aaadf586ae81", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "3ea43de4-b18a-44d1-ab26-85a38a624355", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The most common mistake is trying to merge from the beginning of ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": ", which requires shifting elements repeatedly.", "styles": {} } ], "children": [] }, { "id": "7331b0ed-18f4-4366-9267-ad9612e9a4dc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A much better approach is to start from the ", "styles": {} }, { "type": "text", "text": "end of both arrays", "styles": { "bold": true } }, { "type": "text", "text": ", where the empty positions already exist. This allows the merge to be completed in-place without using extra memory.", "styles": {} } ], "children": [] }, { "id": "e8297448-9cdb-4ed3-8b4e-92f4097d7c69", "type": "divider", "props": {}, "children": [] }, { "id": "f91718c8-8221-45b2-9089-4333a3c87780", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "a2d6f87d-9704-4fc5-a8e0-8551bfebb0e7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Both arrays are already sorted.", "styles": {} } ], "children": [] }, { "id": "83cb7745-6bbe-4ba6-be0a-afeccae0e270", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Merge from the end to avoid shifting elements.", "styles": {} } ], "children": [] }, { "id": "91e82f6d-48d8-4944-9f37-4850e354931e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use three pointers:", "styles": {} } ], "children": [ { "id": "cf317eb7-dc1e-4b82-bca0-d8fd884e069d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "i", "styles": { "code": true } }, { "type": "text", "text": " → Last valid element in ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } } ], "children": [] }, { "id": "ef5faf30-28d5-4a6e-b769-ea8e2d2f19b4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "j", "styles": { "code": true } }, { "type": "text", "text": " → Last element in ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } } ], "children": [] }, { "id": "92a7e853-8505-48cd-a9be-85c095c71e76", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": " → Last position in ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } } ], "children": [] } ] }, { "id": "99b180fa-89d1-4540-97ca-986b6ace1b6d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If elements remain in ", "styles": {} }, { "type": "text", "text": "nums2", "styles": { "code": true } }, { "type": "text", "text": ", copy them into ", "styles": {} }, { "type": "text", "text": "nums1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "722d3f1a-bf51-42d5-92ea-e1ed67df5818", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(m + n)", "styles": { "bold": true } }, { "type": "text", "text": " time with ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] } ] } ], "files": [] }, { "_id": "cd1c463e-8fe4-4ff1-a1fe-f44467d88e3b", "name": "2. Strings & Sliding Window", "createdAt": 1785210624223, "updatedAt": 1785296709277, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "bfee76b2-4c1b-4338-bc6f-e175e1a0e7b9", "name": "7: Valid Anagram", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "cd1c463e-8fe4-4ff1-a1fe-f44467d88e3b", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "4a148397-db87-4fed-9851-b26ab7e8e091", "title": "Valid Anagram Code", "folderId": "bfee76b2-4c1b-4338-bc6f-e175e1a0e7b9", "time": 1785346456862, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n public boolean isAnagram(String s, String t) {\n\n if (s.length() != t.length()) {\n return false;\n }\n\n int[] frequency = new int[26];\n\n for (int i = 0; i < s.length(); i++) {\n frequency[s.charAt(i) - 'a']++;\n frequency[t.charAt(i) - 'a']--;\n }\n\n for (int count : frequency) {\n if (count != 0) {\n return false;\n }\n }\n\n return true;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n String s = Input.getString(\"s\");\n String t = Input.getString(\"t\");\n boolean output = Input.getBool(\"output\");\n \n System.out.println(\"Expected: \" + output);\n \n Solution sol = new Solution();\n\n System.out.println(\"Is Anagram Sum: \" + sol.isAnagram(s, t));\n }\n}", "language": "java", "output": "Expected: true\nIs Anagram Sum: true\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785046327649_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785046327649_0_0", "key": "s", "value": "anagram" }, { "id": "in_1785046327649_0_1", "key": "t", "value": "nagaram" }, { "id": "in_1785046327649_0_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785046327649_1_0", "key": "s", "value": "rat" }, { "id": "in_1785046327649_1_1", "key": "t", "value": "car" }, { "id": "in_1785046327649_1_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785046327649_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785046327649_2_0", "key": "s", "value": "listen" }, { "id": "in_1785046327649_2_1", "key": "t", "value": "silent" }, { "id": "in_1785046327649_2_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785046327649_3_0", "key": "s", "value": "hello" }, { "id": "in_1785046327649_3_1", "key": "t", "value": "ollhe" }, { "id": "in_1785046327649_3_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785046327649_4_0", "key": "s", "value": "apple" }, { "id": "in_1785046327649_4_1", "key": "t", "value": "papel" }, { "id": "in_1785046327649_4_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785046327649_5_0", "key": "s", "value": "apple" }, { "id": "in_1785046327649_5_1", "key": "t", "value": "appls" }, { "id": "in_1785046327649_5_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785046327649_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785046327649_6_0", "key": "s", "value": "a" }, { "id": "in_1785046327649_6_1", "key": "t", "value": "a" }, { "id": "in_1785046327649_6_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785046327649_7_0", "key": "s", "value": "ab" }, { "id": "in_1785046327649_7_1", "key": "t", "value": "aa" }, { "id": "in_1785046327649_7_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785046327649_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785046327649_8_0", "key": "s", "value": "abc" }, { "id": "in_1785046327649_8_1", "key": "t", "value": "cba" }, { "id": "in_1785046327649_8_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785046327649_9_0", "key": "s", "value": "abcd" }, { "id": "in_1785046327649_9_1", "key": "t", "value": "abcde" }, { "id": "in_1785046327649_9_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785046327649_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785046327649_10_0", "key": "s", "value": "" }, { "id": "in_1785046327649_10_1", "key": "t", "value": "" }, { "id": "in_1785046327649_10_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785046327649_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785046327649_11_0", "key": "s", "value": "a" }, { "id": "in_1785046327649_11_1", "key": "t", "value": "" }, { "id": "in_1785046327649_11_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785046327649_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785046327649_12_0", "key": "s", "value": "aaaaaaaa" }, { "id": "in_1785046327649_12_1", "key": "t", "value": "aaaaaaaa" }, { "id": "in_1785046327649_12_2", "key": "output", "value": "true" } ] } ], "selectedTestCaseId": "tc_1785046327649_0" } } ] }, { "_id": "09069ead-e44e-4e37-b948-d7ec5b11a18e", "title": "Valid Anagram Doc", "folderId": "bfee76b2-4c1b-4338-bc6f-e175e1a0e7b9", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "b6876e0d-1e50-410a-ae97-d3346b4f3f9c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "7. Valid Anagram", "styles": {} } ], "children": [] }, { "id": "80b8e544-7a61-4709-ac64-6970b399df72", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "3a1a895a-ec7a-4675-a005-350df2b5fe03", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given two strings ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "t", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " if ", "styles": {} }, { "type": "text", "text": "t", "styles": { "code": true } }, { "type": "text", "text": " is an ", "styles": {} }, { "type": "text", "text": "anagram", "styles": { "bold": true } }, { "type": "text", "text": " of ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": ", and ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": " otherwise.", "styles": {} } ], "children": [] }, { "id": "527ba696-8940-4d7f-b422-4e57b9bc141a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "An ", "styles": {} }, { "type": "text", "text": "anagram", "styles": { "bold": true } }, { "type": "text", "text": " is a word or phrase formed by rearranging the letters of another word, using all the original letters exactly once.", "styles": {} } ], "children": [] }, { "id": "268d652a-072d-4fa9-a6d8-20ae99e778c6", "type": "divider", "props": {}, "children": [] }, { "id": "27ff4dca-b189-4498-aadd-bd13535798a7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "7918af38-9ca0-46f4-8beb-82a52eca7a55", "type": "column_list", "props": {}, "children": [ { "id": "ea472d5b-1f30-4555-992c-68591e039423", "type": "column", "props": { "width": "1" }, "children": [ { "id": "e0434303-a6e6-47ff-aded-470354ff8489", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "8e937a7d-da6b-4e0a-8b17-7946a85791f7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "84650b0d-6bba-478f-8480-0e1e51c9d08e", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"anagram\"\nt = \"nagaram\"" }, "children": [] }, { "id": "602eafa1-7a87-4171-8264-03f56634e514", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "11d3af91-004e-44e4-96f2-00684dde78e0", "type": "codeBlock", "props": { "language": "javascript", "code": "true" }, "children": [] }, { "id": "3fea4c0a-261b-4d90-bb21-be8cac12d398", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "b15e508c-e4e8-4dc5-9f09-01ef892054ed", "type": "codeBlock", "props": { "language": "javascript", "code": "Both strings contain the same characters with the same frequencies." }, "children": [] } ] }, { "id": "7972c9c4-9f0b-41af-8fb6-3c030a087a09", "type": "column", "props": { "width": "1" }, "children": [ { "id": "1ddaa9c7-b668-40ae-a9df-90f3d9ff7f55", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "6e30d61f-aff6-425d-8e79-59cbddc44180", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "310b2a28-ce5e-4248-8cdd-5815b214a6c6", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"rat\"\nt = \"car\"" }, "children": [] }, { "id": "a119a9f2-0ddc-49e4-b08e-c0d1d7b24836", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "dbec9c57-1d08-4c9d-a8cd-27d81c23c31f", "type": "codeBlock", "props": { "language": "javascript", "code": "false" }, "children": [] }, { "id": "53a45a6c-9aa7-4b07-85a8-e368cac563b9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "81618242-ff0f-4afb-91cf-d8047a7d2cbe", "type": "codeBlock", "props": { "language": "javascript", "code": "The characters are different, so they cannot be anagrams." }, "children": [] } ] }, { "id": "73cf8dbc-81f2-40d3-9fff-a933df21f897", "type": "column", "props": { "width": "1" }, "children": [ { "id": "bd626698-b9bd-4119-a998-63f55663e54f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "f2062cf4-852a-4102-abf9-c8c1ceec3051", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "a8868850-0a15-4599-add4-e8874b3030a9", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"listen\"\nt = \"silent\"" }, "children": [] }, { "id": "034a7786-0d27-4c92-a8eb-89e0fa9282c4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "acd46219-e26c-46eb-849a-11cda2d11c60", "type": "codeBlock", "props": { "language": "javascript", "code": "true" }, "children": [] } ] } ] }, { "id": "e9ed135e-2b24-46f2-8f48-78c5cd83b1c7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "e5af6558-ad13-4052-879b-0f37920c2810", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If two strings are anagrams:", "styles": {} } ], "children": [] }, { "id": "9389e157-40d1-49bd-95b4-8e4e14ca60bc", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "They must have the ", "styles": {} }, { "type": "text", "text": "same length", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "4d7580ae-1db0-492d-957d-28ad2bf5070c", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Every character must appear the ", "styles": {} }, { "type": "text", "text": "same number of times", "styles": { "bold": true } }, { "type": "text", "text": " in both strings.", "styles": {} } ], "children": [] }, { "id": "002f43b0-e3dc-44f0-a267-3f463c835401", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "aff0329d-c28e-4ba5-a654-82b93f2ca1d2", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the lengths of the strings are different, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "27905608-6c49-4794-97c4-895a81d5f122", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create an integer array of size ", "styles": {} }, { "type": "text", "text": "26", "styles": { "code": true } }, { "type": "text", "text": " to store character frequencies.", "styles": {} } ], "children": [] }, { "id": "028cb3bb-1869-400f-8a3c-e4aa640e7c1c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse both strings simultaneously.", "styles": {} } ], "children": [] }, { "id": "6ea43cba-8b7e-4ebf-8fcc-28bbd3d53f3f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Increment the frequency for characters in ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "7c372a19-88da-4a6e-82a4-b839dd690f76", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Decrement the frequency for characters in ", "styles": {} }, { "type": "text", "text": "t", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "94f95438-d21b-427f-9eae-e11358b10463", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If every frequency becomes ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ", the strings are anagrams.", "styles": {} } ], "children": [] }, { "id": "6e039989-3b5d-4551-8527-eff3312cf1d6", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Since only lowercase English letters are used, a frequency array is the most efficient solution.", "styles": {} } ], "children": [] }, { "id": "7f50b5aa-2e9c-4ea7-8056-dc81bc9255e7", "type": "divider", "props": {}, "children": [] }, { "id": "08b9a6c3-bb42-4496-8bbb-540294b4f3ac", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "e9733e1b-5660-454b-9c44-f544871f29a1", "type": "addDoc", "props": { "fileId": "4a148397-db87-4fed-9851-b26ab7e8e091", "title": "Valid Anagram Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "387fab36-3646-48b7-bb0a-f140eb574557", "type": "divider", "props": {}, "children": [] }, { "id": "0dbb6274-b868-4aac-bb7c-1b03d4f05ca4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "1034e81e-cb4d-4956-9fc3-5035215e08d2", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "ceb0e1e5-2b25-405b-a2e4-939497b99495", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "The space complexity is considered ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " because the frequency array always has a fixed size of 26.", "styles": {} } ], "children": [] }, { "id": "82f7e7e4-df75-483a-93f2-e64a4d3effef", "type": "divider", "props": {}, "children": [] }, { "id": "398d84c7-fd05-466a-b36e-3b01498c1c1e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "fabb50d5-7a18-4faf-a961-b8881e3a2a6c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "There are two common approaches to solve this problem:", "styles": {} } ], "children": [] }, { "id": "84d32e45-ae3b-400c-98c2-7d25d77d5a66", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Sorting", "styles": { "bold": true } }, { "type": "text", "text": " both strings and comparing them.", "styles": {} } ], "children": [ { "id": "7597f88a-ce02-4289-98ad-e6a8656e3c76", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Time Complexity: ", "styles": {} }, { "type": "text", "text": "O(n log n)", "styles": { "bold": true } } ], "children": [] }, { "id": "4666f25d-37dd-4624-b0fc-25d2cb848565", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Space Complexity: ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " or ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " depending on the sorting algorithm.", "styles": {} } ], "children": [] } ] }, { "id": "8c27052b-7a03-4dfb-9eed-60b719761569", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Character Frequency Array", "styles": { "bold": true } }, { "type": "text", "text": " (Optimal)", "styles": {} } ], "children": [ { "id": "ad8ceed0-2b75-4e9a-96cc-dd7e250e0ce4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Time Complexity: ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "children": [] }, { "id": "026ebeda-1c86-4be4-ab1f-8c549013148d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Space Complexity: ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "children": [] } ] }, { "id": "580bef5f-8fcf-4e81-a394-d13c1007e735", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "In coding interviews, if the strings contain only lowercase English letters, the frequency array solution is generally preferred because it is more efficient.", "styles": {} } ], "children": [] }, { "id": "bfd1a535-ce20-4b86-bfcd-2683240fbf83", "type": "divider", "props": {}, "children": [] }, { "id": "cdd5cf86-3301-4428-b0e7-89551223184c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "d6ad63b8-9683-49c5-8de3-24e0b431ba81", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Anagrams contain the same characters with the same frequencies.", "styles": {} } ], "children": [] }, { "id": "4fa32695-145f-40a0-9f00-18dc8797edb3", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the string lengths differ, they cannot be anagrams.", "styles": {} } ], "children": [] }, { "id": "9520d657-4f7b-44fd-9aad-d6e0f34a37c6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A frequency array of size ", "styles": {} }, { "type": "text", "text": "26", "styles": { "code": true } }, { "type": "text", "text": " provides an optimal solution for lowercase English letters.", "styles": {} } ], "children": [] }, { "id": "e658e6a7-0adb-443d-aa2f-2740eee4ff94", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Increment counts for the first string and decrement for the second string.", "styles": {} } ], "children": [] }, { "id": "d5f17445-e680-43dc-b21c-2efa8271066e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If all frequencies are zero after processing, the strings are anagrams.", "styles": {} } ], "children": [] }, { "id": "7928c5af-395c-4936-9e6f-fbfa1fa8b9b4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] } ] }, { "_id": "eac5f070-cac0-4ea7-900d-b38f38d3bdcb", "name": "8: Longest Substring", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "cd1c463e-8fe4-4ff1-a1fe-f44467d88e3b", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "11d77ac0-d552-4c7a-9346-83fb661ba7f6", "title": "Document", "folderId": "eac5f070-cac0-4ea7-900d-b38f38d3bdcb", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "4e97cb89-e99a-446e-9190-dafb6d7724ff", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "8. Longest Substring Without Repeating Characters", "styles": {} } ], "children": [] }, { "id": "8a1bf36e-0fba-4f96-a807-2379a23465c2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "5045af27-fd6b-4171-be4d-b7533026105a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given a string ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": ", find the length of the ", "styles": {} }, { "type": "text", "text": "longest substring", "styles": { "bold": true } }, { "type": "text", "text": " without repeating characters.", "styles": {} } ], "children": [] }, { "id": "717d1af8-6423-4bb9-b432-1f2c7b95e350", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A ", "styles": {} }, { "type": "text", "text": "substring", "styles": { "bold": true } }, { "type": "text", "text": " is a contiguous sequence of characters within a string.", "styles": {} } ], "children": [] }, { "id": "415c7629-8921-4b39-a425-3e584ac3d832", "type": "divider", "props": {}, "children": [] }, { "id": "6b4fad50-88f2-47f8-ad1b-2bc6200bac0e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "28e28195-7339-437b-b693-a676f2bfae45", "type": "column_list", "props": {}, "children": [ { "id": "9adf4728-f5a5-45a6-b1da-7267770afa17", "type": "column", "props": { "width": "1" }, "children": [ { "id": "5cfa9298-6f34-48ab-8591-04271f223d61", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "1f1b00da-b001-42a8-8ce7-11a99902726b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "97379f26-93ed-4c8e-b9b4-6b36937ccad0", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"abcabcbb\"" }, "children": [] }, { "id": "a7865c75-5fc1-4a22-8abf-389b4f6ba05f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "fb57ead8-ee58-43a2-81dd-e4a3c95e997b", "type": "codeBlock", "props": { "language": "javascript", "code": "3" }, "children": [] }, { "id": "2a16b359-0125-4675-90a4-d93d7fef6855", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "e390e828-b461-42f5-a5b8-315d94d9fc0b", "type": "codeBlock", "props": { "language": "javascript", "code": "The longest substring without repeating characters is \"abc\".\n\nLength = 3" }, "children": [] } ] }, { "id": "d4a9f879-f5a0-4f6f-a97b-00d967cc0633", "type": "column", "props": { "width": "1" }, "children": [ { "id": "3d976f77-5707-4322-b41e-334f8a249922", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "f4932132-b605-4ba6-aa93-7cabb148cbd7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "dd2d5783-8d9c-4248-aa7d-4c7747a718ec", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"bbbbb\"" }, "children": [] }, { "id": "a7b5ffe8-82e5-4441-b5eb-225dcf3d9ba5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "fa533dcc-ed6f-45de-b9b2-e240af441641", "type": "codeBlock", "props": { "language": "javascript", "code": "1" }, "children": [] }, { "id": "2261f273-81eb-4414-a36f-3e31f7f2d630", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "943fdbf6-d680-4ca0-a9ac-b6366b514f2e", "type": "codeBlock", "props": { "language": "javascript", "code": "The longest substring is \"b\".\n\nLength = 1" }, "children": [] } ] }, { "id": "0d8230c5-29e2-4ab4-9e2c-e501130317b8", "type": "column", "props": { "width": "1" }, "children": [ { "id": "e2ce0370-4abe-48e2-a232-bedaf4f413d3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "c2e163b0-c4dd-4c0c-883e-899eff8cbe32", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "a0cf1d6f-98b0-4f0c-b99f-275f91c61963", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"pwwkew\"" }, "children": [] }, { "id": "6131b0ef-232c-49ee-b28e-85402c1562ff", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "bc74ec69-8f97-49be-87a8-2292cf393cfe", "type": "codeBlock", "props": { "language": "javascript", "code": "3" }, "children": [] }, { "id": "f1237704-75ca-40e5-a2e8-ab10c9227cd2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "88fa9bf2-8cc4-423f-8dfa-1c09a7b14742", "type": "codeBlock", "props": { "language": "javascript", "code": "The longest substring is \"wke\".\n\nLength = 3" }, "children": [] } ] } ] }, { "id": "72d5a99d-0da7-4779-b97e-077dd04e55d1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "c34a0dc3-f4d2-4fd3-a282-66d700442340", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem can be solved efficiently using the ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " technique.", "styles": {} } ], "children": [] }, { "id": "52024b8f-4ef4-4b35-892d-c2a2c91c817c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain a window that always contains unique characters.", "styles": {} } ], "children": [] }, { "id": "0899e893-ae63-459e-999d-cb10e065488f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "6949c3eb-d92e-48a4-962e-120234ade7d9", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create a ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "code": true } }, { "type": "text", "text": " to store the current characters in the window.", "styles": {} } ], "children": [] }, { "id": "2c2b6522-8e18-4fc6-9cd7-e1a51fb11f09", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Initialize two pointers:", "styles": {} } ], "children": [ { "id": "f750ae7a-7146-45ea-b704-5040b69891d9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "left", "styles": { "code": true } }, { "type": "text", "text": " → Start of the window.", "styles": {} } ], "children": [] }, { "id": "7bdd6798-a255-4131-a2c5-d617708b891f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "right", "styles": { "code": true } }, { "type": "text", "text": " → End of the window.", "styles": {} } ], "children": [] } ] }, { "id": "3f141a1a-1d79-4ce9-8930-37a0725c471d", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Expand the window by moving ", "styles": {} }, { "type": "text", "text": "right", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d8d65da7-c276-4493-bc22-f8450ad7648c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the current character already exists in the set:", "styles": {} } ], "children": [ { "id": "fc4309e6-4976-4697-b1e1-fd1003e373cf", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Remove characters from the left until the duplicate is removed.", "styles": {} } ], "children": [] } ] }, { "id": "2c2ebf13-de8a-41bd-92ad-1a242237c689", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Add the current character to the set.", "styles": {} } ], "children": [] }, { "id": "838c0148-85a4-4a51-ac8d-ab2cb1411b5a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the maximum window size.", "styles": {} } ], "children": [] }, { "id": "efd40eac-7995-46be-b750-15a254230189", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until the end of the string.", "styles": {} } ], "children": [] }, { "id": "9842c2d8-482c-46f3-8815-dae525e55773", "type": "divider", "props": {}, "children": [] }, { "id": "f95255c6-de82-47f4-94c5-ddbb55339170", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "ca86467a-6e26-4ff9-9d7d-8e76c9a066b7", "type": "addDoc", "props": { "fileId": "9bc3e804-c542-4f4a-832c-0a0ec3a7445c", "title": "Java Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "576c52be-382f-4ea6-a218-98bc7218629c", "type": "divider", "props": {}, "children": [] }, { "id": "2facb0c2-ab5f-4c0b-8733-00b11cf7b1ca", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "0d13bd19-fbf0-4ebf-b662-7055c153a34a", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(min(n, 128))", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "6a84ef13-b410-4e05-9be3-ad1aa80c344a", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "Since the input contains ASCII characters, the HashSet stores at most 128 unique characters. In general, the space complexity is ", "styles": {} }, { "type": "text", "text": "O(min(n, charset))", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "fc85dc32-f66b-404c-8022-5bd143919f8a", "type": "divider", "props": {}, "children": [] }, { "id": "80007332-2349-4ac3-9be4-f5df9fcafff3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "e414f4eb-d3ab-41b7-a487-053b9faab01b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This is one of the most common ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " interview questions.", "styles": {} } ], "children": [] }, { "id": "b0cfa888-da1c-4c9c-af86-56c74ef8230d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A brute-force solution checks every possible substring, resulting in ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " time complexity.", "styles": {} } ], "children": [] }, { "id": "dcd31117-3b81-4ca1-86c4-11e5fd3de0eb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The Sliding Window technique ensures that each character is added and removed from the window at most once, reducing the time complexity to ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "9ed4678f-1d85-42cb-a54d-c6f0814abda1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Remember this pattern:", "styles": {} } ], "children": [] }, { "id": "c92a6a15-ab3f-466f-807f-1c6fcfe9cc05", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Expand the window by moving the ", "styles": {} }, { "type": "text", "text": "right", "styles": { "code": true } }, { "type": "text", "text": " pointer.", "styles": {} } ], "children": [] }, { "id": "3163b484-0d61-42f8-97d8-3a3baf990e9d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If a duplicate is found, shrink the window by moving the ", "styles": {} }, { "type": "text", "text": "left", "styles": { "code": true } }, { "type": "text", "text": " pointer.", "styles": {} } ], "children": [] }, { "id": "fc60def6-9aff-492c-aa31-6e26b75c1143", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Keep track of the maximum window size.", "styles": {} } ], "children": [] }, { "id": "076bbce5-3677-4720-8cc7-d310d8c78d9f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This pattern is widely used in substring and subarray problems.", "styles": {} } ], "children": [] }, { "id": "1a09cf03-840b-418d-8559-abd78cdd348c", "type": "divider", "props": {}, "children": [] }, { "id": "5e97d3e0-fd06-4235-a88c-15a3dc0dbfc9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "301174bb-06a8-4022-9edb-386a987ce79e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A substring consists of contiguous characters.", "styles": {} } ], "children": [] }, { "id": "251bed8c-91a7-4919-8317-df2837ac975f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use the ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " technique with two pointers.", "styles": {} } ], "children": [] }, { "id": "c8f7bd0d-8802-441a-b328-bea092d7fd12", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain a ", "styles": {} }, { "type": "text", "text": "HashSet", "styles": { "code": true } }, { "type": "text", "text": " to ensure all characters in the current window are unique.", "styles": {} } ], "children": [] }, { "id": "06bc0002-0730-411b-86ec-b30991a45263", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "When a duplicate is encountered, shrink the window until it becomes valid again.", "styles": {} } ], "children": [] }, { "id": "a4cfb720-2daf-46f9-839f-a2bba1c88601", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the maximum window length after each expansion.", "styles": {} } ], "children": [] }, { "id": "ca3fe398-4d15-4802-924e-3632ae7ea5e5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time with ", "styles": {} }, { "type": "text", "text": "O(min(n, charset))", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "9bc3e804-c542-4f4a-832c-0a0ec3a7445c", "title": "Java Code", "folderId": "eac5f070-cac0-4ea7-900d-b38f38d3bdcb", "time": 1785346449387, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.HashSet;\n\nclass Solution {\n public int lengthOfLongestSubstring(String s) {\n\n HashSet set = new HashSet<>();\n\n int left = 0;\n int maxLength = 0;\n\n for (int right = 0; right < s.length(); right++) {\n\n while (set.contains(s.charAt(right))) {\n set.remove(s.charAt(left));\n left++;\n }\n\n set.add(s.charAt(right));\n\n maxLength = Math.max(maxLength, right - left + 1);\n }\n\n return maxLength;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n String s = Input.getString(\"s\");\n int output = Input.getInt(\"output\");\n \n System.out.println(\"Expected: \" + output);\n \n Solution sol = new Solution();\n\n System.out.println(\"Longest Substring Length: \" + sol.lengthOfLongestSubstring(s));\n }\n}", "language": "java", "output": "Expected: 3\nLongest Substring Length: 3\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785046846464_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785046846464_0_0", "key": "s", "value": "abcabcbb" }, { "id": "in_1785046846464_0_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785046846464_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785046846464_1_0", "key": "s", "value": "bbbbb" }, { "id": "in_1785046846464_1_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785046846464_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785046846464_2_0", "key": "s", "value": "pwwkew" }, { "id": "in_1785046846464_2_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785046846464_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785046846464_3_0", "key": "s", "value": "" }, { "id": "in_1785046846464_3_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785046846464_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785046846464_4_0", "key": "s", "value": "abcdef" }, { "id": "in_1785046846464_4_1", "key": "output", "value": "6" } ] }, { "id": "tc_1785046846464_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785046846464_5_0", "key": "s", "value": "abba" }, { "id": "in_1785046846464_5_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785046846464_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785046846464_6_0", "key": "s", "value": "dvdf" }, { "id": "in_1785046846464_6_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785046846464_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785046846464_7_0", "key": "s", "value": "aab" }, { "id": "in_1785046846464_7_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785046846464_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785046846464_8_0", "key": "s", "value": "tmmzuxt" }, { "id": "in_1785046846464_8_1", "key": "output", "value": "5" } ] }, { "id": "tc_1785046846464_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785046846464_9_0", "key": "s", "value": "au" }, { "id": "in_1785046846464_9_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785046846464_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785046846464_10_0", "key": "s", "value": "" }, { "id": "in_1785046846464_10_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785046846464_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785046846464_11_0", "key": "s", "value": "a" }, { "id": "in_1785046846464_11_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785046846464_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785046846464_12_0", "key": "s", "value": "aaaaaa" }, { "id": "in_1785046846464_12_1", "key": "output", "value": "1" } ] } ], "selectedTestCaseId": "tc_1785046846464_0" } } ] } ] }, { "_id": "24f7f3eb-36f3-430f-b07e-c4d3ffc59267", "name": "9: Longest Repeating Character", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "cd1c463e-8fe4-4ff1-a1fe-f44467d88e3b", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "dac1a4aa-0a1d-4f85-a83d-91e4f05380ad", "title": "Document", "folderId": "24f7f3eb-36f3-430f-b07e-c4d3ffc59267", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "0c5f979a-6617-4724-a7da-df3a8d668097", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "9. Longest Repeating Character Replacement", "styles": {} } ], "children": [] }, { "id": "595af14d-3cd4-47ed-90b1-c1d3354e2b4e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "d1ebca31-e306-4150-a9cd-5f5b320c0638", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are given a string ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": " consisting of uppercase English letters and an integer ", "styles": {} }, { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "c907e1e7-052b-49f2-a6e2-15149deae2dd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You can choose any character in the string and change it to any other uppercase English character. You can perform this operation at most ", "styles": {} }, { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": " times.", "styles": {} } ], "children": [] }, { "id": "cb59f30a-c7b2-421f-8563-660aa581aea7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the length of the ", "styles": {} }, { "type": "text", "text": "longest substring", "styles": { "bold": true } }, { "type": "text", "text": " containing the same letter after performing at most ", "styles": {} }, { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": " replacements.", "styles": {} } ], "children": [] }, { "id": "9cc0e1a8-60d8-417f-9fcd-0aced402332b", "type": "divider", "props": {}, "children": [] }, { "id": "ac3bf0f5-fda4-428f-bac2-ab40589d3437", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "27981fc7-5980-4fec-aae4-0b5d0aa49bf6", "type": "column_list", "props": {}, "children": [ { "id": "12fca23a-975f-4739-8677-e32ea94572b3", "type": "column", "props": { "width": "1" }, "children": [ { "id": "afabc181-5b3d-45aa-8a07-25f9cac045d2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "28e9b1a3-80b7-45ea-a5f6-a883718c5ff0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "75bb0653-2676-4635-b39d-85007b2ea242", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"ABAB\"\nk = 2" }, "children": [] }, { "id": "179ded9e-ea5a-4d36-a6e5-90d8b49b06e2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "5c95b61b-9b3d-498f-9969-cbdb7e4ade6a", "type": "codeBlock", "props": { "language": "javascript", "code": "4" }, "children": [] }, { "id": "202b3e59-d571-4681-ba6f-a066b66a68e7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "b7096f20-1874-44d3-8407-b0521dfb3144", "type": "codeBlock", "props": { "language": "javascript", "code": "Replace the two 'A' characters with 'B', or the two 'B' characters with 'A'.\n\nThe entire string becomes the same character.\n\nLength = 4" }, "children": [] } ] }, { "id": "4cf22194-7ec5-45c1-b5dd-c39974d38023", "type": "column", "props": { "width": "1" }, "children": [ { "id": "3825b958-3112-4050-83f7-5da920aa59e0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "4d9679e5-bc76-4503-ad17-d7df1ae1bb41", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "c2d41739-419f-4d3d-a988-4bde2df8cb27", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"AABABBA\"\nk = 1" }, "children": [] }, { "id": "6c535b58-f4d0-4317-965d-3ef79e781287", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "b4be5631-5fcb-4d54-aacb-7d139e2e36b4", "type": "codeBlock", "props": { "language": "javascript", "code": "4" }, "children": [] }, { "id": "8430b7ca-51fa-427d-9076-e333a8ac455e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "237d6232-fa75-4109-a542-d2bca9956f49", "type": "codeBlock", "props": { "language": "javascript", "code": "Replace one 'A' with 'B'.\n\nThe longest repeating substring becomes \"BBBB\".\n\nLength = 4" }, "children": [] } ] } ] }, { "id": "b376bad4-5e96-4fa5-a8e3-31c49c45b0fc", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "0cd7d64d-5ba6-40dd-a6f4-35377e8e77ad", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem can be solved efficiently using the ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " technique.", "styles": {} } ], "children": [] }, { "id": "7fe53290-bdc4-4322-a476-3a37cbe4bbb3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain a window and keep track of the frequency of each character.", "styles": {} } ], "children": [] }, { "id": "82c0346e-29f2-4bce-b7e4-d9398fe03b24", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Observation", "styles": {} } ], "children": [] }, { "id": "c113801e-52ff-4302-90fc-db206683ccb6", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A window is valid if:", "styles": {} } ], "children": [] }, { "id": "8c286e04-f5a5-4cdb-a105-a9c0fcefbccc", "type": "codeBlock", "props": { "language": "javascript", "code": "Window Size - Maximum Frequency <= k" }, "children": [] }, { "id": "e13ff4d6-3aad-4583-88a1-5b9a4996c8da", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This means the number of characters that need to be replaced is at most ", "styles": {} }, { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "ebeab0f9-03df-40a8-959a-356b36304a11", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "6d25ce20-c512-44ca-811b-4dac5500e8dd", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create a frequency array of size ", "styles": {} }, { "type": "text", "text": "26", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "1e651086-af90-49cd-85fa-31839b8ab8b9", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain two pointers:", "styles": {} } ], "children": [ { "id": "8fb610b4-8b7b-4a80-8cd2-2f25c952f137", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "left", "styles": { "code": true } } ], "children": [] }, { "id": "23887af7-4141-48be-95f2-abfce23dcdd7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "right", "styles": { "code": true } } ], "children": [] } ] }, { "id": "4c793ef1-cecd-4f25-a8df-18dc14491a04", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Expand the window by moving ", "styles": {} }, { "type": "text", "text": "right", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "fcde3174-9f87-4e73-b171-0948a5165224", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the frequency of the current character.", "styles": {} } ], "children": [] }, { "id": "2549a92d-671e-4783-9a50-167c15dcf8cd", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Track the maximum frequency inside the current window.", "styles": {} } ], "children": [] }, { "id": "8d1c9c3b-4eac-4a3b-9fac-1d612d8a350e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If:", "styles": {} } ], "children": [] }, { "id": "ecd37eeb-6d95-43d3-b5d2-35211d90e7d5", "type": "codeBlock", "props": { "language": "javascript", "code": "Window Size - maxFrequency > k" }, "children": [] }, { "id": "4239fa75-a52a-47ac-921c-65533379e0ce", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "shrink the window from the left.", "styles": {} } ], "children": [] }, { "id": "d9dc3cc0-e7ec-442f-bc2c-e1a15c03ad8f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "start": 7 }, "content": [ { "type": "text", "text": "Update the maximum window size.", "styles": {} } ], "children": [] }, { "id": "43225976-cb02-4c1a-ba06-52a4b00d51f6", "type": "divider", "props": {}, "children": [] }, { "id": "1b0bc821-2fb4-49f2-80a1-7e94e0778c48", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "27e274fe-8373-4439-a5d2-91da8f30db5a", "type": "addDoc", "props": { "fileId": "546c4b5f-0c16-4659-a778-7d2e81ba104c", "title": "Java Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "1136fbd1-0d08-4eff-bcf5-16ef1f28ce58", "type": "divider", "props": {}, "children": [] }, { "id": "6655ecd8-7afc-415b-968a-3c8fb6248f90", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "ff29593a-221a-4cf0-8348-6e46160c8e23", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "09333dbf-8384-4f62-b6ac-9eede6e26942", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "Since the frequency array always contains only 26 uppercase English letters, the space complexity is constant.", "styles": {} } ], "children": [] }, { "id": "f899f019-4f49-40e6-a246-96f611239557", "type": "divider", "props": {}, "children": [] }, { "id": "76d075d6-b3cb-4a55-867d-414440d91714", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "a0804d34-742f-4aa9-b50e-0138fdbfbd8b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem is a variation of the ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " pattern.", "styles": {} } ], "children": [] }, { "id": "ad5a0b5d-6beb-4230-bb72-9f2a49c468d3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The important observation is that a window is valid as long as the number of characters that need to be replaced does not exceed ", "styles": {} }, { "type": "text", "text": "k", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "4ee3be64-1c8f-4b4e-b0bb-cc047758efce", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Remember the condition:", "styles": {} } ], "children": [] }, { "id": "375347cd-2052-4b6b-a89e-4e7d91c4c08c", "type": "codeBlock", "props": { "language": "javascript", "code": "(window size) - (maximum frequency) <= k" }, "children": [] }, { "id": "1703b301-50b7-43c6-a15b-d8776711d17e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of recalculating the maximum frequency every time the window changes, we only keep track of the highest frequency seen so far. This optimization allows the solution to run in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] }, { "id": "6a2b9e7b-ce18-40a1-a014-77f3ee1574c0", "type": "divider", "props": {}, "children": [] }, { "id": "99764bdf-5643-4a22-8dc1-b3e24da7c808", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "80911e3c-4c7d-4c2c-8f19-ba4bfd1cd7d6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use the ", "styles": {} }, { "type": "text", "text": "Sliding Window", "styles": { "bold": true } }, { "type": "text", "text": " technique.", "styles": {} } ], "children": [] }, { "id": "09e288e9-5b9a-4cd8-9790-ef289350aefd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain the frequency of characters inside the current window.", "styles": {} } ], "children": [] }, { "id": "1d061795-34f2-47e4-914b-1d01b03e52ea", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Track the maximum frequency in the window.", "styles": {} } ], "children": [] }, { "id": "b6952a96-7b4f-44e0-bdd5-48a0ae68617d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A window is valid if:", "styles": {} } ], "children": [ { "id": "348b8ad4-02b1-4c26-bdca-bfe075cdf51b", "type": "codeBlock", "props": { "language": "javascript", "code": "Window Size - Maximum Frequency <= k" }, "children": [] } ] }, { "id": "b659a384-bc63-4c70-bb05-6b647e9139cf", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Shrink the window whenever the condition is violated.", "styles": {} } ], "children": [] }, { "id": "03bc01dc-434b-49b3-b527-5437404cb1d4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the maximum valid window length during each iteration.", "styles": {} } ], "children": [] }, { "id": "b70b061b-1d12-4ebc-b002-478d2e01e7cd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "546c4b5f-0c16-4659-a778-7d2e81ba104c", "title": "Java Code", "folderId": "24f7f3eb-36f3-430f-b07e-c4d3ffc59267", "time": 1785346462729, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int characterReplacement(String s, int k) {\n\n int[] frequency = new int[26];\n\n int left = 0;\n int maxFrequency = 0;\n int maxLength = 0;\n\n for (int right = 0; right < s.length(); right++) {\n\n frequency[s.charAt(right) - 'A']++;\n\n maxFrequency = Math.max(\n maxFrequency,\n frequency[s.charAt(right) - 'A']\n );\n\n while ((right - left + 1) - maxFrequency > k) {\n frequency[s.charAt(left) - 'A']--;\n left++;\n }\n\n maxLength = Math.max(maxLength, right - left + 1);\n }\n\n return maxLength;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n String s = Input.getString(\"s\");\n int k = Input.getInt(\"k\");\n int output = Input.getInt(\"output\");\n \n System.out.println(\"Expected: \" + output);\n \n Solution sol = new Solution();\n\n System.out.println(\"Longest Substring Length: \" + sol.characterReplacement(s, k));\n }\n}", "language": "java", "output": "Expected: 4\nLongest Substring Length: 4\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785047023202_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785047023202_0_0", "key": "s", "value": "ABAB" }, { "id": "in_1785047023202_0_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_0_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047023202_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785047023202_1_0", "key": "s", "value": "AABABBA" }, { "id": "in_1785047023202_1_1", "key": "k", "value": "1" }, { "id": "in_1785047023202_1_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047023202_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785047023202_2_0", "key": "s", "value": "AAAA" }, { "id": "in_1785047023202_2_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_2_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047023202_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785047023202_3_0", "key": "s", "value": "ABCDE" }, { "id": "in_1785047023202_3_1", "key": "k", "value": "1" }, { "id": "in_1785047023202_3_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047023202_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785047023202_4_0", "key": "s", "value": "BAAAB" }, { "id": "in_1785047023202_4_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_4_2", "key": "output", "value": "5" } ] }, { "id": "tc_1785047023202_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785047023202_5_0", "key": "s", "value": "ABBB" }, { "id": "in_1785047023202_5_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_5_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047023202_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785047023202_6_0", "key": "s", "value": "ABCDE" }, { "id": "in_1785047023202_6_1", "key": "k", "value": "0" }, { "id": "in_1785047023202_6_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047023202_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785047023202_7_0", "key": "s", "value": "AAABBC" }, { "id": "in_1785047023202_7_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_7_2", "key": "output", "value": "5" } ] }, { "id": "tc_1785047023202_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785047023202_8_0", "key": "s", "value": "AABA" }, { "id": "in_1785047023202_8_1", "key": "k", "value": "0" }, { "id": "in_1785047023202_8_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047023202_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785047023202_9_0", "key": "s", "value": "ABAA" }, { "id": "in_1785047023202_9_1", "key": "k", "value": "0" }, { "id": "in_1785047023202_9_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047023202_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785047023202_10_0", "key": "s", "value": "" }, { "id": "in_1785047023202_10_1", "key": "k", "value": "2" }, { "id": "in_1785047023202_10_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047023202_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785047023202_11_0", "key": "s", "value": "A" }, { "id": "in_1785047023202_11_1", "key": "k", "value": "0" }, { "id": "in_1785047023202_11_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047023202_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785047023202_12_0", "key": "s", "value": "BBBBBB" }, { "id": "in_1785047023202_12_1", "key": "k", "value": "3" }, { "id": "in_1785047023202_12_2", "key": "output", "value": "6" } ] } ], "selectedTestCaseId": "tc_1785047023202_0" } } ] } ] } ], "files": [] }, { "_id": "12c7e7d4-ca6f-4b56-a7ce-cd68bac41374", "name": "3. Binary Search", "createdAt": 1785210624223, "updatedAt": 1785296701436, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "fdd512c7-13c4-4a0e-bd83-cdf583bd56c4", "name": "10: Binary Search", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "12c7e7d4-ca6f-4b56-a7ce-cd68bac41374", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "40acc757-9da2-41ae-b54c-a3a34216299a", "title": "Document", "folderId": "fdd512c7-13c4-4a0e-bd83-cdf583bd56c4", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "c08e62aa-0bc2-4712-9677-545f7c39e95c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "10. Binary Search", "styles": {} } ], "children": [] }, { "id": "30a0613c-5812-4391-969c-60173159edd1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "b56a170d-562b-4d57-87d7-4b666e2df346", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an array of integers ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": " sorted in ", "styles": {} }, { "type": "text", "text": "ascending order", "styles": { "bold": true } }, { "type": "text", "text": ", and an integer ", "styles": {} }, { "type": "text", "text": "target", "styles": { "code": true } }, { "type": "text", "text": ", return the ", "styles": {} }, { "type": "text", "text": "index", "styles": { "bold": true } }, { "type": "text", "text": " of ", "styles": {} }, { "type": "text", "text": "target", "styles": { "code": true } }, { "type": "text", "text": " if it exists in the array. Otherwise, return ", "styles": {} }, { "type": "text", "text": "-1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "fe644390-f99b-4483-ba52-320cf01020b5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You must write an algorithm with ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " runtime complexity.", "styles": {} } ], "children": [] }, { "id": "909b3f9d-3d33-4641-8cce-936941060ff6", "type": "divider", "props": {}, "children": [] }, { "id": "a54965fc-228a-4940-be0b-a70b8444b271", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "d6bc0a67-6904-4eff-b8ac-2beb49037a64", "type": "column_list", "props": {}, "children": [ { "id": "3d7c396d-b99f-49f0-a45f-0a4318f1347e", "type": "column", "props": { "width": "1" }, "children": [ { "id": "6e6e5c66-a360-4721-8953-9b82e56ebda2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "87b42d62-4dd3-4415-868b-2bb3bc2b4182", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "1b460720-d616-47a2-8c85-a4d7a61118c6", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [-1,0,3,5,9,12]\ntarget = 9" }, "children": [] }, { "id": "d5f1a9e1-8dc0-46eb-9a05-593513d11b74", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "6f396a6b-5ef3-4237-a420-7afa8a7e1268", "type": "codeBlock", "props": { "language": "javascript", "code": "4" }, "children": [] }, { "id": "580c8cc3-d06f-4a21-83c1-a364e178fbf3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "4b6f857e-2e62-4e33-8777-19af462ef821", "type": "codeBlock", "props": { "language": "javascript", "code": "The target value 9 is found at index 4." }, "children": [] } ] }, { "id": "baa4d6c5-8e44-4aad-a027-a3c3463a8f35", "type": "column", "props": { "width": "1" }, "children": [ { "id": "cc5d90bb-e81d-4d39-8b67-d5ede967370a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "bd41e1aa-0cf1-41cf-baae-521fe6cc7a31", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "f7c3f67e-825f-4829-b749-b2d2499d546c", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [-1,0,3,5,9,12]\ntarget = 2" }, "children": [] }, { "id": "4c02a252-a4b9-4610-a3aa-86074f1a9595", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "7a1656ea-77d9-4bd1-ad87-fb584f8c1083", "type": "codeBlock", "props": { "language": "javascript", "code": "-1" }, "children": [] }, { "id": "dadd3a10-4591-467f-8c97-5a0cd21389ad", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "942b506f-e7b5-41c9-a7cf-05ab5d4bc6f2", "type": "codeBlock", "props": { "language": "javascript", "code": "The target value does not exist in the array." }, "children": [] } ] } ] }, { "id": "58768215-8644-479c-b533-2e9f38c1687d", "type": "divider", "props": {}, "children": [] }, { "id": "faa53711-c8c5-4768-a4e1-966cfad8ea40", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "8fe2ebf5-7cf3-48d9-a3ea-f37ff5749e83", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Binary Search works only on ", "styles": {} }, { "type": "text", "text": "sorted arrays", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "634550d9-2ab4-4522-917e-59fc3e7259d1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of checking every element, Binary Search repeatedly divides the search space into two halves.", "styles": {} } ], "children": [] }, { "id": "b0bb5b95-2a1c-4ccb-8dbe-6e923648825e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "64ebd19c-8c46-437e-bcc4-e931a97a15e5", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Initialize two pointers:", "styles": {} } ], "children": [ { "id": "c7a45522-5b00-498b-8e43-f6592c8afbca", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "left = 0", "styles": { "code": true } } ], "children": [] }, { "id": "cbbcfb88-5262-4197-907c-9500a5995008", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "right = nums.length - 1", "styles": { "code": true } } ], "children": [] } ] }, { "id": "92fcf706-1636-429f-a632-52d1ac0e8261", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Find the middle index:", "styles": {} } ], "children": [] }, { "id": "29cc1983-b567-4fb7-8fa4-166613b9a624", "type": "codeBlock", "props": { "language": "javascript", "code": "mid = left + (right - left) / 2" }, "children": [] }, { "id": "ff453eda-9d7d-4807-ae6b-2c6729667e67", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "start": 3 }, "content": [ { "type": "text", "text": "Compare ", "styles": {} }, { "type": "text", "text": "nums[mid]", "styles": { "code": true } }, { "type": "text", "text": " with the target.", "styles": {} } ], "children": [] }, { "id": "fe347ca9-bc08-483a-aa21-1d7e865a9403", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If equal, return ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "152e0eaa-f7f2-4cdc-a03d-8986df5319f7", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the target is greater, search the right half.", "styles": {} } ], "children": [] }, { "id": "9aa9e9c0-d937-4bbc-a879-b6ea5089efcc", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise, search the left half.", "styles": {} } ], "children": [] }, { "id": "9fdbaef1-9472-4e30-9f43-f65f3895fe6a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until ", "styles": {} }, { "type": "text", "text": "left > right", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2a9082eb-4905-4ee3-826d-41648273e294", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the target is not found, return ", "styles": {} }, { "type": "text", "text": "-1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "01a8a5cc-3656-489f-b6aa-576861f0937a", "type": "divider", "props": {}, "children": [] }, { "id": "a68bd3c4-03d0-426c-a75e-08d43bfe0f23", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "85290434-cff0-4261-9df0-fac605c6d8e0", "type": "addDoc", "props": { "fileId": "36f7dbfd-46bf-4acb-bfc3-bd98b5e28b46", "title": "Java Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "5b3dcabf-5259-4087-84b7-484d891136c7", "type": "divider", "props": {}, "children": [] }, { "id": "41c4fda0-c183-4e13-8f3c-1ab4b13e6d9a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "f9c86125-c5f8-465b-9e09-ce774ce0e285", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(log n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "99fbc969-73a0-41a6-8c52-612171231ed8", "type": "divider", "props": {}, "children": [] }, { "id": "080af0aa-3c60-46d0-80e0-14a7ae5ac7ea", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "01a95228-938a-456f-b658-d74ff3e88a67", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Binary Search is one of the most important search algorithms in computer science.", "styles": {} } ], "children": [] }, { "id": "c86c8c10-ed11-49a8-bec6-652b105f1555", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "To avoid integer overflow when calculating the middle index, use:", "styles": {} } ], "children": [] }, { "id": "8df95d3c-4852-465f-a795-6fd0ddd015e8", "type": "codeBlock", "props": { "language": "javascript", "code": "int mid = left + (right - left) / 2;" }, "children": [] }, { "id": "48b67939-a110-420d-b592-d808fed43a9f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "instead of:", "styles": {} } ], "children": [] }, { "id": "4b16ecc6-0e59-4abc-afae-e119752fa164", "type": "codeBlock", "props": { "language": "javascript", "code": "int mid = (left + right) / 2;" }, "children": [] }, { "id": "ee64aef6-e4be-4668-86d5-15974e599994", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem introduces the classic Binary Search pattern, which is widely used in problems involving sorted arrays, search spaces, and optimization.", "styles": {} } ], "children": [] }, { "id": "b79aaa17-bafc-4514-9992-106dfe045b8d", "type": "divider", "props": {}, "children": [] }, { "id": "fbeb3295-00db-46df-a225-817912b15c65", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "a150c25e-8c41-4fc4-9a9c-8cadfcb21ce5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Binary Search works only on ", "styles": {} }, { "type": "text", "text": "sorted arrays", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2753b022-14d4-4ab9-b5f6-c0e5ca842935", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Repeatedly divide the search space into two halves.", "styles": {} } ], "children": [] }, { "id": "7a54177e-a73f-4bd6-9988-310367732459", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare the middle element with the target.", "styles": {} } ], "children": [] }, { "id": "91234bb4-150b-433c-8ac0-1ac19dd5ff49", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Eliminate one half of the search space in every iteration.", "styles": {} } ], "children": [] }, { "id": "bf97d34b-8abf-48f0-9880-43e8834427d3", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always calculate the middle index using:", "styles": {} } ], "children": [ { "id": "70e5456c-acc9-4cb0-86da-cd48c8ca7836", "type": "codeBlock", "props": { "language": "javascript", "code": "left + (right - left) / 2" }, "children": [] }, { "id": "70bbceef-e473-4f82-bed7-99930d05d8ce", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "to avoid integer overflow.", "styles": {} } ], "children": [] } ] }, { "id": "b1ba0793-d42f-436f-ab2e-b116cc6a8d8e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "36f7dbfd-46bf-4acb-bfc3-bd98b5e28b46", "title": "Java Code", "folderId": "fdd512c7-13c4-4a0e-bd83-cdf583bd56c4", "time": 1785346469235, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int search(int[] nums, int target) {\n\n int left = 0;\n int right = nums.length - 1;\n\n while (left <= right) {\n\n int mid = left + (right - left) / 2;\n\n if (nums[mid] == target) {\n return mid;\n }\n\n if (nums[mid] < target) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n }\n\n return -1;\n }\n \n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"nums\");\n int target = Input.getInt(\"target\");\n int output = Input.getInt(\"output\");\n \n System.out.println(\"Expected: \" + output);\n \n Solution sol = new Solution();\n\n System.out.println(\"Index of target \"+ target + \" is : \" + sol.search(nums, target));\n }\n}", "language": "java", "output": "Expected: 4\nIndex of target 9 is : 4\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785047143237_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785047143237_0_0", "key": "nums", "value": "[-1,0,3,5,9,12]" }, { "id": "in_1785047143237_0_1", "key": "target", "value": "9" }, { "id": "in_1785047143237_0_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047143237_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785047143237_1_0", "key": "nums", "value": "[-1,0,3,5,9,12]" }, { "id": "in_1785047143237_1_1", "key": "target", "value": "2" }, { "id": "in_1785047143237_1_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047143237_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785047143237_2_0", "key": "nums", "value": "[1]" }, { "id": "in_1785047143237_2_1", "key": "target", "value": "1" }, { "id": "in_1785047143237_2_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047143237_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785047143237_3_0", "key": "nums", "value": "[1]" }, { "id": "in_1785047143237_3_1", "key": "target", "value": "0" }, { "id": "in_1785047143237_3_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047143237_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785047143237_4_0", "key": "nums", "value": "[1,3,5,7,9]" }, { "id": "in_1785047143237_4_1", "key": "target", "value": "7" }, { "id": "in_1785047143237_4_2", "key": "output", "value": "3" } ] }, { "id": "tc_1785047143237_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785047143237_5_0", "key": "nums", "value": "[2,4,6,8,10]" }, { "id": "in_1785047143237_5_1", "key": "target", "value": "2" }, { "id": "in_1785047143237_5_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047143237_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785047143237_6_0", "key": "nums", "value": "[2,4,6,8,10]" }, { "id": "in_1785047143237_6_1", "key": "target", "value": "10" }, { "id": "in_1785047143237_6_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047143237_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785047143237_7_0", "key": "nums", "value": "[5,10,15,20,25]" }, { "id": "in_1785047143237_7_1", "key": "target", "value": "15" }, { "id": "in_1785047143237_7_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047143237_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785047143237_8_0", "key": "nums", "value": "[-10,-5,0,5,10]" }, { "id": "in_1785047143237_8_1", "key": "target", "value": "-5" }, { "id": "in_1785047143237_8_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047143237_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785047143237_9_0", "key": "nums", "value": "[1,2,3,4,5,6]" }, { "id": "in_1785047143237_9_1", "key": "target", "value": "6" }, { "id": "in_1785047143237_9_2", "key": "output", "value": "5" } ] }, { "id": "tc_1785047143237_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785047143237_10_0", "key": "nums", "value": "[]" }, { "id": "in_1785047143237_10_1", "key": "target", "value": "5" }, { "id": "in_1785047143237_10_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047143237_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785047143237_11_0", "key": "nums", "value": "[100]" }, { "id": "in_1785047143237_11_1", "key": "target", "value": "100" }, { "id": "in_1785047143237_11_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047143237_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785047143237_12_0", "key": "nums", "value": "[100]" }, { "id": "in_1785047143237_12_1", "key": "target", "value": "50" }, { "id": "in_1785047143237_12_2", "key": "output", "value": "-1" } ] } ], "selectedTestCaseId": "tc_1785047143237_0" } } ] } ] }, { "_id": "ad80c506-080c-45ee-b467-819da4271e21", "name": "11: Rotated Sorted Array", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "12c7e7d4-ca6f-4b56-a7ce-cd68bac41374", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "02e7766f-0ee8-435e-9548-2266a6388710", "title": "Document", "folderId": "ad80c506-080c-45ee-b467-819da4271e21", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "0f6ecbf4-9763-44cf-a962-add011ecc2a4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "11. Search in Rotated Sorted Array", "styles": {} } ], "children": [] }, { "id": "916c4fea-dfa1-4e15-bbd8-7d076ffe6b99", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "af511176-6a98-4f96-a25c-37ae7dec900a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "There is an integer array ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": " sorted in ascending order (with distinct values).", "styles": {} } ], "children": [] }, { "id": "8635a732-5f6a-43a8-9a5f-b6b340372a20", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Before being passed to your function, the array is possibly rotated at an unknown pivot index.", "styles": {} } ], "children": [] }, { "id": "fd7e72ef-1fb3-472e-aefb-2999f45a59fe", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "For example:", "styles": {} } ], "children": [] }, { "id": "b589e446-4eda-4da8-a6d6-01e40f17f414", "type": "codeBlock", "props": { "language": "javascript", "code": "[0,1,2,4,5,6,7]" }, "children": [] }, { "id": "7aaac00a-5233-4a77-96ad-8ef938e30e27", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "might become", "styles": {} } ], "children": [] }, { "id": "1339b89a-535e-4382-825f-b8fd7839b8bb", "type": "codeBlock", "props": { "language": "javascript", "code": "[4,5,6,7,0,1,2]" }, "children": [] }, { "id": "2207bd1a-abf2-45fa-a547-56cafe44bd59", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the rotated array ", "styles": {} }, { "type": "text", "text": "nums", "styles": { "code": true } }, { "type": "text", "text": " and an integer ", "styles": {} }, { "type": "text", "text": "target", "styles": { "code": true } }, { "type": "text", "text": ", return the ", "styles": {} }, { "type": "text", "text": "index", "styles": { "bold": true } }, { "type": "text", "text": " of ", "styles": {} }, { "type": "text", "text": "target", "styles": { "code": true } }, { "type": "text", "text": " if it exists in the array. Otherwise, return ", "styles": {} }, { "type": "text", "text": "-1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2dbdff9a-9048-40ad-9ac7-4350153c983b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You must write an algorithm with ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " runtime complexity.", "styles": {} } ], "children": [] }, { "id": "c75606c1-8952-4d8d-984c-3e169150c6c5", "type": "divider", "props": {}, "children": [] }, { "id": "5ae31f0d-0564-4317-aeb4-48771ff986c0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "6423dab6-c093-47a7-b0c2-d0946c48f9d0", "type": "column_list", "props": {}, "children": [ { "id": "3141dcae-096b-4532-8b8f-a6d696b5b834", "type": "column", "props": { "width": "1" }, "children": [ { "id": "a866aec0-92cf-4237-a546-5479a5cabbd0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "2ea21a0b-76f6-48ad-9011-e6deff1edb00", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "65abb7c0-fed0-45b7-978e-33b26917e372", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [4,5,6,7,0,1,2]\ntarget = 0" }, "children": [] }, { "id": "13450ad8-53cf-4841-bd3e-9ae7b2449c5f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "7521c19a-307a-400f-9021-4045a918b586", "type": "codeBlock", "props": { "language": "javascript", "code": "4" }, "children": [] } ] }, { "id": "17b9af93-83ae-4ffe-bd83-94efbb6e9962", "type": "column", "props": { "width": "1" }, "children": [ { "id": "d6df25bb-455a-448e-8f17-f6e544b20f6c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "bbcb113a-f548-45ac-9264-e1f3099429d0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "8e1f3e92-56ec-4a26-94eb-e6c1b2ae5ede", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [4,5,6,7,0,1,2]\ntarget = 3" }, "children": [] }, { "id": "b24e6a48-e176-4a8b-9b09-dd8d61d618e8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "0445b3bb-693f-4621-88cb-7ae3fb570579", "type": "codeBlock", "props": { "language": "javascript", "code": "-1" }, "children": [] } ] }, { "id": "8ba6335f-2f48-405f-8656-c69e34e04250", "type": "column", "props": { "width": "1" }, "children": [ { "id": "5eb568a6-7094-4674-a231-ea2ab475ddc0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "95c10adc-95ea-405c-b071-ba4420f33d51", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "1575990e-0c0c-4209-aa9e-2ee290ff9804", "type": "codeBlock", "props": { "language": "javascript", "code": "nums = [1]\ntarget = 0" }, "children": [] }, { "id": "9d87a4b4-0656-4d7e-bd9e-fc4c6ae7511a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "2c0e0653-9bad-4d5c-8853-1406417a14d4", "type": "codeBlock", "props": { "language": "javascript", "code": "-1" }, "children": [] } ] } ] }, { "id": "2c77d4aa-af65-4da9-bcf7-6b6761379376", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "0bc1667e-9f3b-4279-84af-4b666749128c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A rotated sorted array always has ", "styles": {} }, { "type": "text", "text": "one half that is sorted", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "f1decea9-7373-4fde-9959-6aedb6892b44", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "During each iteration:", "styles": {} } ], "children": [] }, { "id": "9848b74c-1efc-4926-bc25-b5ea39e5ccdc", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Find the middle element.", "styles": {} } ], "children": [] }, { "id": "716aca70-0542-4884-9c03-2fa04e00701e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether the left half is sorted.", "styles": {} } ], "children": [] }, { "id": "aa5ff54a-28ef-4779-a6ad-5ad8d20db2ab", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the left half is sorted:", "styles": {} } ], "children": [ { "id": "8c5d76ea-74f1-490c-a868-bcb667431e0f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether the target lies within the left half.", "styles": {} } ], "children": [] }, { "id": "d14438a7-5886-4f77-ad8d-e0f38d323599", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise, search the right half.", "styles": {} } ], "children": [] } ] }, { "id": "3894d67e-6214-41c4-9705-ccd6e5a448ef", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the right half is sorted:", "styles": {} } ], "children": [ { "id": "5a87dc75-9626-447b-8b82-5e9809914f36", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether the target lies within the right half.", "styles": {} } ], "children": [] }, { "id": "8359b80f-87d5-4e18-8851-36cd02d61d08", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise, search the left half.", "styles": {} } ], "children": [] } ] }, { "id": "e7f91c03-06da-4baf-886c-636a03900434", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until the target is found or the search space becomes empty.", "styles": {} } ], "children": [] }, { "id": "177a3416-9215-4857-9c5d-255c6c120898", "type": "divider", "props": {}, "children": [] }, { "id": "ef140579-c13c-43a0-a409-af5ad38d4fef", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "dcd74376-2feb-48c2-a093-bae48131152c", "type": "addDoc", "props": { "fileId": "fe5ef9e0-fd13-4b7d-8bdf-99d1dd3a3ee0", "title": "Java Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "f36d17cf-56b3-4355-aae4-b67529bb65d5", "type": "divider", "props": {}, "children": [] }, { "id": "9a92218f-6658-477f-9e1b-c7a69c5f19db", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "c699b946-c222-4daf-a667-1f265ea8434d", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(log n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "6a5e1ee0-3eeb-416a-a746-32bf341b2794", "type": "divider", "props": {}, "children": [] }, { "id": "d72ecbf3-5aa1-4175-a0f3-da9f5374f2f9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "0fc5c4b4-84ff-4e15-9e61-44b0104e2e97", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The trick is ", "styles": {} }, { "type": "text", "text": "not", "styles": { "bold": true } }, { "type": "text", "text": " to find the rotation point first.", "styles": {} } ], "children": [] }, { "id": "b6d33c06-9b57-4fb9-8534-b978cd49696b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead, during every iteration:", "styles": {} } ], "children": [] }, { "id": "0dba2a10-f499-4faf-9920-b80e8c082217", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Determine which half is sorted.", "styles": {} } ], "children": [] }, { "id": "eaa86c26-818f-48fc-987d-4ac09c14e054", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether the target lies inside that sorted half.", "styles": {} } ], "children": [] }, { "id": "5004fbf7-3c44-4387-9b88-2c46fa83099e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Discard the other half.", "styles": {} } ], "children": [] }, { "id": "641a4e80-9348-4587-bc4e-bccf9b0cbd6a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This preserves the ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " time complexity and is the approach most interviewers expect.", "styles": {} } ], "children": [] }, { "id": "ab1e7bcc-f7a9-4509-a18d-4ae707649323", "type": "divider", "props": {}, "children": [] }, { "id": "cce84477-5963-4c7d-8f16-45c25966fd65", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "2246d91d-f259-4058-a8f9-2d3695b653a1", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A rotated sorted array always has one sorted half.", "styles": {} } ], "children": [] }, { "id": "fee7b53e-ffce-4bb7-84c3-0acdd48635cd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use Binary Search to identify the sorted half in each iteration.", "styles": {} } ], "children": [] }, { "id": "4781c934-3a26-403c-802b-bcc30260f04d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Decide whether to search the left or right half based on the target.", "styles": {} } ], "children": [] }, { "id": "34e5ac5f-8108-4b41-be2f-3bf2ee671298", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Do not search both halves, as that would increase the time complexity.", "styles": {} } ], "children": [] }, { "id": "df80b561-2a65-4bc4-afbc-b44995bd3fbf", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "fe5ef9e0-fd13-4b7d-8bdf-99d1dd3a3ee0", "title": "Java Code", "folderId": "ad80c506-080c-45ee-b467-819da4271e21", "time": 1785346474380, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int search(int[] nums, int target) {\n\n int left = 0;\n int right = nums.length - 1;\n\n while (left <= right) {\n\n int mid = left + (right - left) / 2;\n\n if (nums[mid] == target) {\n return mid;\n }\n\n // Left half is sorted\n if (nums[left] <= nums[mid]) {\n\n if (target >= nums[left] && target < nums[mid]) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n\n }\n // Right half is sorted\n else {\n\n if (target > nums[mid] && target <= nums[right]) {\n left = mid + 1;\n } else {\n right = mid - 1;\n }\n\n }\n }\n\n return -1;\n }\n\n // Test the code\n public static void main(String[] args) {\n // This is custom code to read Test case in Devscribe\n int[] nums = Input.getIntArray(\"nums\");\n int target = Input.getInt(\"target\");\n int output = Input.getInt(\"output\");\n \n System.out.println(\"Expected: \" + output);\n\n Solution sol = new Solution();\n\n System.out.println(\"Index of target \" + target + \" is : \" + sol.search(nums, target));\n }\n}", "language": "java", "output": "Expected: 4\nIndex of target 0 is : 4\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785047249105_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785047249105_0_0", "key": "nums", "value": "[4,5,6,7,0,1,2]" }, { "id": "in_1785047249105_0_1", "key": "target", "value": "0" }, { "id": "in_1785047249105_0_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047249105_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785047249105_1_0", "key": "nums", "value": "[4,5,6,7,0,1,2]" }, { "id": "in_1785047249105_1_1", "key": "target", "value": "3" }, { "id": "in_1785047249105_1_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047249105_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785047249105_2_0", "key": "nums", "value": "[1]" }, { "id": "in_1785047249105_2_1", "key": "target", "value": "0" }, { "id": "in_1785047249105_2_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047249105_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785047249105_3_0", "key": "nums", "value": "[1]" }, { "id": "in_1785047249105_3_1", "key": "target", "value": "1" }, { "id": "in_1785047249105_3_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047249105_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785047249105_4_0", "key": "nums", "value": "[5,1,3]" }, { "id": "in_1785047249105_4_1", "key": "target", "value": "5" }, { "id": "in_1785047249105_4_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047249105_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785047249105_5_0", "key": "nums", "value": "[5,1,3]" }, { "id": "in_1785047249105_5_1", "key": "target", "value": "3" }, { "id": "in_1785047249105_5_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047249105_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785047249105_6_0", "key": "nums", "value": "[6,7,8,1,2,3,4,5]" }, { "id": "in_1785047249105_6_1", "key": "target", "value": "2" }, { "id": "in_1785047249105_6_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047249105_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785047249105_7_0", "key": "nums", "value": "[6,7,8,1,2,3,4,5]" }, { "id": "in_1785047249105_7_1", "key": "target", "value": "9" }, { "id": "in_1785047249105_7_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047249105_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785047249105_8_0", "key": "nums", "value": "[3,1]" }, { "id": "in_1785047249105_8_1", "key": "target", "value": "1" }, { "id": "in_1785047249105_8_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047249105_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785047249105_9_0", "key": "nums", "value": "[3,1]" }, { "id": "in_1785047249105_9_1", "key": "target", "value": "3" }, { "id": "in_1785047249105_9_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047249105_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785047249105_10_0", "key": "nums", "value": "[]" }, { "id": "in_1785047249105_10_1", "key": "target", "value": "5" }, { "id": "in_1785047249105_10_2", "key": "output", "value": "-1" } ] }, { "id": "tc_1785047249105_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785047249105_11_0", "key": "nums", "value": "[1]" }, { "id": "in_1785047249105_11_1", "key": "target", "value": "1" }, { "id": "in_1785047249105_11_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785047249105_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785047249105_12_0", "key": "nums", "value": "[2,1]" }, { "id": "in_1785047249105_12_1", "key": "target", "value": "2" }, { "id": "in_1785047249105_12_2", "key": "output", "value": "0" } ] } ], "selectedTestCaseId": "tc_1785047249105_0" } } ] } ] }, { "_id": "260b3618-999e-4caa-b7dc-c0e76e4a1015", "name": "12: First Bad Version", "createdAt": 1785210624223, "updatedAt": 1785210624223, "parentId": "12c7e7d4-ca6f-4b56-a7ce-cd68bac41374", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "5d3b8905-4fe3-4cf4-8aa2-34d00cf9da80", "title": "Document", "folderId": "260b3618-999e-4caa-b7dc-c0e76e4a1015", "time": 1785261106875, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "0349379a-1d36-4697-8ca8-434f5cb22196", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "12. First Bad Version", "styles": {} } ], "children": [] }, { "id": "dc8fd104-3215-45ab-8293-8ac8cf584a94", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "e5bc01a3-20f2-45b3-8c47-1f4f0bf90bdc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are a product manager leading a team that develops a new product.", "styles": {} } ], "children": [] }, { "id": "c3c25678-090a-425a-b812-8ad0875228b0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Unfortunately, the latest version of the product is bad. Since each version is developed based on the previous version, all versions after a bad version are also bad.", "styles": {} } ], "children": [] }, { "id": "a26f304c-9f76-4a29-b457-a1762c97602f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are given an integer ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": ", representing versions ", "styles": {} }, { "type": "text", "text": "1", "styles": { "code": true } }, { "type": "text", "text": " to ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": ", and an API:", "styles": {} } ], "children": [] }, { "id": "a521bf1a-cd73-474f-9265-1d5367ab9fb5", "type": "codeBlock", "props": { "language": "javascript", "code": "boolean isBadVersion(version)" }, "children": [] }, { "id": "7ae2d8d3-8ccb-4f54-a567-5cdb75d5bc20", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "which returns whether a version is bad.", "styles": {} } ], "children": [] }, { "id": "791effd5-39e7-4827-b819-dce286cc9fc8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Find the ", "styles": {} }, { "type": "text", "text": "first bad version", "styles": { "bold": true } }, { "type": "text", "text": " while minimizing the number of API calls.", "styles": {} } ], "children": [] }, { "id": "65731299-0c34-4147-9660-2bf89800683c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You must write an algorithm with ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " runtime complexity.", "styles": {} } ], "children": [] }, { "id": "21084d4b-d4bd-458d-8c81-28ddab8029e4", "type": "divider", "props": {}, "children": [] }, { "id": "3da50fb2-77ca-43ca-81f5-ecc6a619dd4e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "f6b28b12-cf2a-4b38-8ab8-4beb419862fa", "type": "column_list", "props": {}, "children": [ { "id": "b8a41aa3-50d4-4d48-9cee-d61c769011d4", "type": "column", "props": { "width": "1" }, "children": [ { "id": "dca6497d-f93a-43ac-9276-537e87ff1b93", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "6c060b0a-b2b4-4703-8863-bd4026b3be9e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "d1703e6d-ae2c-4b65-985b-b1f3e3cb57ff", "type": "codeBlock", "props": { "language": "javascript", "code": "n = 5\nbad = 4" }, "children": [] }, { "id": "6cc79501-435f-4b72-ba9c-24a2d52f8d77", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "e9618599-150f-4ff3-82d5-47751e79ced0", "type": "codeBlock", "props": { "language": "javascript", "code": "4" }, "children": [] }, { "id": "3b200111-7316-4887-9f73-ab95aec56ceb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "2f825be5-7065-4826-a704-3a9684fd43d1", "type": "codeBlock", "props": { "language": "javascript", "code": "Version 1 -> Good\nVersion 2 -> Good\nVersion 3 -> Good\nVersion 4 -> Bad\nVersion 5 -> Bad\n\nThe first bad version is 4." }, "children": [] } ] }, { "id": "4f67e589-833e-4191-b772-e0292bbe1228", "type": "column", "props": { "width": "1" }, "children": [ { "id": "fa4272e4-f70b-479c-b8dd-4210011ef648", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "e9b5032e-4a34-47c6-9185-f3a7dfb20dac", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "7ee2eecb-604d-4cba-93cf-4482f8983e06", "type": "codeBlock", "props": { "language": "javascript", "code": "n = 1\nbad = 1" }, "children": [] }, { "id": "674a88bf-6c22-42ad-8fd0-65cbf0d17381", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "1a2aae20-2276-40fc-97ed-d6655620119a", "type": "codeBlock", "props": { "language": "javascript", "code": "1" }, "children": [] } ] } ] }, { "id": "822081f0-e2e0-4b5f-9725-bd1ce8791493", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "030135dd-091a-4cb7-8817-231bd3a37cbc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem is solved using ", "styles": {} }, { "type": "text", "text": "Binary Search", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "8b9a370a-f194-4b93-9430-562c6c9c9bd7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Observation", "styles": {} } ], "children": [] }, { "id": "93e22ff7-bd1b-4f05-9afb-0657e72f1563", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If a version is bad, then every version after it is also bad.", "styles": {} } ], "children": [] }, { "id": "96380c62-f234-4a22-8a93-35d53826077e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If a version is good, then every version before it is also good.", "styles": {} } ], "children": [] }, { "id": "2b7a4242-e4b2-4ac6-9e43-d411f86824d4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "3b3cf73c-b71b-4905-b7fa-62b2c716f0b1", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Initialize:", "styles": {} } ], "children": [ { "id": "0f22b75f-4a8a-44f6-bba2-204b740c3a26", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "left = 1", "styles": { "code": true } } ], "children": [] }, { "id": "a98d4fd6-246e-4614-ad6b-6d76a5e7fbbe", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "right = n", "styles": { "code": true } } ], "children": [] } ] }, { "id": "0fe1de3a-c765-43e2-983c-f43a4849b98e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Find the middle version.", "styles": {} } ], "children": [] }, { "id": "19065697-46e6-41dc-89f9-d7a06c30d5b0", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " is bad:", "styles": {} } ], "children": [ { "id": "b3988c4a-5311-403a-abab-62999b695f47", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The first bad version is either ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " or before it.", "styles": {} } ], "children": [] }, { "id": "948662f4-1ded-4e07-99e5-eb5ce072f12b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Move ", "styles": {} }, { "type": "text", "text": "right = mid", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] } ] }, { "id": "5767b7d9-73a9-47f8-92cd-aa057ed39e34", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise:", "styles": {} } ], "children": [ { "id": "c753bb55-fcc5-4329-8664-b9354549a7b8", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Search the right half.", "styles": {} } ], "children": [] }, { "id": "8faefe26-e73f-4cec-a78b-cca619247a6d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Move ", "styles": {} }, { "type": "text", "text": "left = mid + 1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] } ] }, { "id": "f75971b4-f8f9-43ea-b8ea-b54bd7fbdbb2", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until ", "styles": {} }, { "type": "text", "text": "left == right", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "83b3c772-b607-4804-80e3-de919da535de", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "left", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "5f154dc1-a5a7-4fa4-97eb-79c7e882cec0", "type": "divider", "props": {}, "children": [] }, { "id": "36f5444a-613b-436d-80cc-dc80014b163c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "9cd55c2e-54b2-48a6-aa8b-0bff800a1790", "type": "addDoc", "props": { "fileId": "c189ecaf-7d1d-4e23-86c4-f1565f6707e1", "title": "Java Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "68ca8499-eff1-4b3c-aa8e-4327f2c6f110", "type": "divider", "props": {}, "children": [] }, { "id": "63810e8a-eaaa-4894-8f98-0ca02f087f3b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Output", "styles": {} } ], "children": [] }, { "id": "2c3d21ee-3e6c-44a2-ab40-b19d2858d660", "type": "codeBlock", "props": { "language": "javascript", "code": "First Bad Version : 4" }, "children": [] }, { "id": "d500885d-6e52-47cf-aa4b-c379e5ed5390", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "Note:", "styles": { "bold": true } }, { "type": "text", "text": " On LeetCode, the ", "styles": {} }, { "type": "text", "text": "isBadVersion()", "styles": { "code": true } }, { "type": "text", "text": " API is already provided. You only need to implement the ", "styles": {} }, { "type": "text", "text": "firstBadVersion()", "styles": { "code": true } }, { "type": "text", "text": " method.", "styles": {} } ], "children": [] }, { "id": "81f4a0ae-0843-431f-91b9-eb067800771f", "type": "divider", "props": {}, "children": [] }, { "id": "f9b2615b-bb6e-4e16-a86e-236d6bcc3e72", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "e8e0320c-9065-4feb-aa27-29eeddcf97f4", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(log n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "15206b4a-7cd5-48da-8e31-87f08004311d", "type": "divider", "props": {}, "children": [] }, { "id": "128ba031-cbad-42d6-a198-ac53be65957d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "2ae1553f-a6e3-43c9-bbd2-6780f1814646", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This problem is a classic variation of ", "styles": {} }, { "type": "text", "text": "Binary Search", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "58ae16ec-22b7-49aa-87a3-a1b8cb4e91ff", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of searching for an exact value, you're searching for the ", "styles": {} }, { "type": "text", "text": "boundary", "styles": { "bold": true } }, { "type": "text", "text": " where versions change from ", "styles": {} }, { "type": "text", "text": "good", "styles": { "bold": true } }, { "type": "text", "text": " to ", "styles": {} }, { "type": "text", "text": "bad", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "513763e3-abe8-4278-9271-476ab3ee4ebb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "To avoid missing the first bad version:", "styles": {} } ], "children": [] }, { "id": "9950a4ac-465f-4775-820d-23c4479013d2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " is bad, keep searching the left half by setting ", "styles": {} }, { "type": "text", "text": "right = mid", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "5dd1692a-edda-4013-a58f-8079ee1c7b40", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " is good, search the right half by setting ", "styles": {} }, { "type": "text", "text": "left = mid + 1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2d3e020b-0ba4-4af1-a4b3-ef67c6f2eb98", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "When the loop ends, ", "styles": {} }, { "type": "text", "text": "left", "styles": { "code": true } }, { "type": "text", "text": " points to the first bad version.", "styles": {} } ], "children": [] }, { "id": "e0cd6f94-c40f-426b-8ef2-67872c585b43", "type": "divider", "props": {}, "children": [] }, { "id": "fe24e271-f0e6-403a-96c1-8bff633dee6d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "0d60a9a2-1360-4466-81cf-6ed29fde2009", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The versions form a monotonic sequence: ", "styles": {} }, { "type": "text", "text": "Good → Bad", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "46db545e-63e1-44c6-9d70-71ef681b46fb", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use Binary Search to find the first occurrence of a bad version.", "styles": {} } ], "children": [] }, { "id": "67c485d1-8d33-4e2b-b5d9-457996239ca5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " is bad, continue searching the left half.", "styles": {} } ], "children": [] }, { "id": "06286235-6f00-4bfa-bdcd-63edeea7a31b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "mid", "styles": { "code": true } }, { "type": "text", "text": " is good, continue searching the right half.", "styles": {} } ], "children": [] }, { "id": "2033fbe6-e54a-4645-a5a8-41c4183f3f60", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The loop terminates when ", "styles": {} }, { "type": "text", "text": "left == right", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "703c51bc-3884-466f-8fcb-c34b48a982f2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution runs in ", "styles": {} }, { "type": "text", "text": "O(log n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "c189ecaf-7d1d-4e23-86c4-f1565f6707e1", "title": "Java Code", "folderId": "260b3618-999e-4caa-b7dc-c0e76e4a1015", "time": 1785346479680, "createdAt": 1785210624223, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "public class Solution {\n\n int firstBad;\n\n boolean isBadVersion(int version) {\n return version >= firstBad;\n }\n\n public int firstBadVersion(int n) {\n\n int left = 1;\n int right = n;\n\n while (left < right) {\n\n int mid = left + (right - left) / 2;\n\n if (isBadVersion(mid)) {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n return left;\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n int n = Input.getInt(\"n\");\n int bad = Input.getInt(\"bad\");\n int output = Input.getInt(\"output\");\n\n System.out.println(\"Expected: \" + output);\n\n Solution sol = new Solution();\n sol.firstBad = bad; // Set the bad version from input\n\n System.out.println(\"First Bad Version : \" + sol.firstBadVersion(n));\n }\n}", "language": "java", "output": "Expected: 4\nFirst Bad Version : 4\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785047497174_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785047497174_0_0", "key": "n", "value": "5" }, { "id": "in_1785047497174_0_1", "key": "bad", "value": "4" }, { "id": "in_1785047497174_0_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785047497174_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785047497174_1_0", "key": "n", "value": "1" }, { "id": "in_1785047497174_1_1", "key": "bad", "value": "1" }, { "id": "in_1785047497174_1_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047497174_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785047497174_2_0", "key": "n", "value": "10" }, { "id": "in_1785047497174_2_1", "key": "bad", "value": "7" }, { "id": "in_1785047497174_2_2", "key": "output", "value": "7" } ] }, { "id": "tc_1785047497175_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785047497175_3_0", "key": "n", "value": "8" }, { "id": "in_1785047497175_3_1", "key": "bad", "value": "1" }, { "id": "in_1785047497175_3_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047497175_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785047497175_4_0", "key": "n", "value": "100" }, { "id": "in_1785047497175_4_1", "key": "bad", "value": "100" }, { "id": "in_1785047497175_4_2", "key": "output", "value": "100" } ] }, { "id": "tc_1785047497175_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785047497175_5_0", "key": "n", "value": "50" }, { "id": "in_1785047497175_5_1", "key": "bad", "value": "25" }, { "id": "in_1785047497175_5_2", "key": "output", "value": "25" } ] }, { "id": "tc_1785047497175_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785047497175_6_0", "key": "n", "value": "2" }, { "id": "in_1785047497175_6_1", "key": "bad", "value": "2" }, { "id": "in_1785047497175_6_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047497175_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785047497175_7_0", "key": "n", "value": "2" }, { "id": "in_1785047497175_7_1", "key": "bad", "value": "1" }, { "id": "in_1785047497175_7_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047497175_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785047497175_8_0", "key": "n", "value": "20" }, { "id": "in_1785047497175_8_1", "key": "bad", "value": "13" }, { "id": "in_1785047497175_8_2", "key": "output", "value": "13" } ] }, { "id": "tc_1785047497175_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785047497175_9_0", "key": "n", "value": "1000" }, { "id": "in_1785047497175_9_1", "key": "bad", "value": "567" }, { "id": "in_1785047497175_9_2", "key": "output", "value": "567" } ] }, { "id": "tc_1785047497175_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785047497175_10_0", "key": "n", "value": "1" }, { "id": "in_1785047497175_10_1", "key": "bad", "value": "1" }, { "id": "in_1785047497175_10_2", "key": "output", "value": "1" } ] }, { "id": "tc_1785047497175_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785047497175_11_0", "key": "n", "value": "2" }, { "id": "in_1785047497175_11_1", "key": "bad", "value": "2" }, { "id": "in_1785047497175_11_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785047497175_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785047497175_12_0", "key": "n", "value": "100" }, { "id": "in_1785047497175_12_1", "key": "bad", "value": "100" }, { "id": "in_1785047497175_12_2", "key": "output", "value": "100" } ] } ], "selectedTestCaseId": "tc_1785047497174_0" } } ] } ] } ], "files": [] }, { "_id": "80222204-0cd3-444b-a343-2e8fcd2d8d3a", "name": "4. Linked List", "createdAt": 1785210624223, "updatedAt": 1785263343667, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": null, "github_path": null, "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "8f57017d-db88-4c7b-a983-e946e1e25d88", "name": "13. Reverse Linked List", "createdAt": 1785262126356, "updatedAt": 1785263331682, "parentId": "80222204-0cd3-444b-a343-2e8fcd2d8d3a", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "890530b8-6582-411b-84c0-b2721da8cb52", "title": "Reverse LinkedList Doc", "folderId": "8f57017d-db88-4c7b-a983-e946e1e25d88", "time": 1785263033473, "createdAt": 1785262150937, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "674ded33-3638-4451-8a85-1bd8c9fd0531", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "13. Reverse Linked List", "styles": {} } ], "children": [] }, { "id": "611ad4ef-842f-49a8-8775-23ed5a25ff7f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "6e2c37ee-7862-4ff6-a9c4-aa053aa7dde1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the ", "styles": {} }, { "type": "text", "text": "head", "styles": { "code": true } }, { "type": "text", "text": " of a singly linked list, reverse the list and return the reversed list.", "styles": {} } ], "children": [] }, { "id": "e0330a44-9207-4d10-98e0-a63a64b805c5", "type": "divider", "props": {}, "children": [] }, { "id": "3d9ab107-5cc3-4be3-8628-47ee735533bb", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "effdd8e9-8d54-45f1-8002-fdc7b07f6df5", "type": "column_list", "props": {}, "children": [ { "id": "f8007fcc-2bd7-45b3-96d9-e53bbb7926bb", "type": "column", "props": { "width": "1" }, "children": [ { "id": "06adf964-785b-4518-bf97-e0376d8e6806", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "7568db66-ca9c-4f4a-9b03-54e698fe5e93", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "a7e6825e-bd6d-4586-8615-c2bf61906438", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [1,2,3,4,5]", "wordWrap": "true" }, "children": [] }, { "id": "08c473c3-8f83-4318-af20-64acb8ebda9d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "08efac55-b116-46b8-8295-6ea1e7e4b98d", "type": "codeBlock", "props": { "language": "javascript", "code": "[5,4,3,2,1]", "wordWrap": "true" }, "children": [] }, { "id": "d15ef6e0-c8f0-4e34-b419-fb6b5e5b677d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "7f2e532d-f91a-4b85-8e9a-99c9d88e6c7f", "type": "column", "props": { "width": "1" }, "children": [ { "id": "7dc59fc0-4b21-49e7-a142-152c3ca13253", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "2e1ec0f5-f9b6-41f6-95f5-396bfa5ebf41", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "73f28f4b-ffe4-4616-8134-e8cabf0b1747", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [1,2]", "wordWrap": "true" }, "children": [] }, { "id": "228dbdeb-0953-4c08-baaf-652eb24e22b9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "78a393db-c48e-473a-b641-0677f47b3484", "type": "codeBlock", "props": { "language": "javascript", "code": "[2,1]", "wordWrap": "true" }, "children": [] }, { "id": "38dbfed3-5bb1-4c9b-ae24-d4e5fff7c918", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "39e15684-783c-4096-8d47-1e5f1b8efef7", "type": "column", "props": { "width": "1" }, "children": [ { "id": "b5d070be-ee9d-4a4f-b6b1-08c60e290956", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "40f986f2-d7a6-4d63-89c1-182587def515", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "379fa00d-660b-406d-9dce-1c71d8a34bbf", "type": "codeBlock", "props": { "language": "javascript", "code": "head = []", "wordWrap": "true" }, "children": [] }, { "id": "65fb156f-57b2-4644-8b16-2adf7c840ae5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "912b9fd8-0c4a-4d51-846c-ec17788e58c0", "type": "codeBlock", "props": { "language": "javascript", "code": "[]", "wordWrap": "true" }, "children": [] }, { "id": "721ef7fd-31d5-4d82-957f-976527b472c9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "20848174-f08e-4d53-8d3a-b990ed7501a7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "e5ea6f13-20de-4937-963a-48df59e8d5f1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "We can reverse a linked list using three pointers.", "styles": {} } ], "children": [] }, { "id": "7b6cecd6-ae5e-49a9-bce4-3efe3e13908b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "previous", "styles": { "code": true } }, { "type": "text", "text": " stores the previous node.", "styles": {} } ], "children": [] }, { "id": "b4e715ba-9b1e-494a-a5cf-4c0dc7ca0771", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "current", "styles": { "code": true } }, { "type": "text", "text": " points to the current node.", "styles": {} } ], "children": [] }, { "id": "5bbeedab-b5fd-4301-8f4c-158aaf8c2658", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "next", "styles": { "code": true } }, { "type": "text", "text": " stores the next node before changing the link.", "styles": {} } ], "children": [] }, { "id": "4d5eba4f-585e-4919-b2aa-a67765eaa06a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "941b5037-fd63-4c51-90d7-3816e4c1e6e9", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Initialize:", "styles": {} } ], "children": [ { "id": "ddfda64f-71fa-4f58-88ec-4300ccd8f7e2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "previous = null", "styles": { "code": true } } ], "children": [] }, { "id": "99faaad2-8158-4148-8ea1-9046f7886478", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "current = head", "styles": { "code": true } } ], "children": [] } ] }, { "id": "6042fade-14a3-4c37-bc01-ebb2138f110f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store the next node.", "styles": {} } ], "children": [] }, { "id": "6b15beae-4a5a-4b06-a3b1-5bf7718f0203", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Reverse the current node's pointer.", "styles": {} } ], "children": [] }, { "id": "0723519f-a874-4841-9850-b0aad527af32", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Move both pointers one step ahead.", "styles": {} } ], "children": [] }, { "id": "aad311a8-e1fd-4d6a-803d-080ceab6bfd4", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Repeat until ", "styles": {} }, { "type": "text", "text": "current", "styles": { "code": true } }, { "type": "text", "text": " becomes ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "85d0b1d8-96cd-44f0-a29a-5f4dce865943", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "previous", "styles": { "code": true } }, { "type": "text", "text": ", which becomes the new head.", "styles": {} } ], "children": [] }, { "id": "e1c7b7bf-08af-4833-ba81-26bec4a04482", "type": "divider", "props": {}, "children": [] }, { "id": "57bedfdb-9d47-4fcb-aac9-1653e6765225", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "9a1955dd-7034-4005-9f04-c955e83d9862", "type": "codeBlock", "props": { "language": "javascript", "code": "Original List\n\n1 → 2 → 3 → null\n\nprevious = null\ncurrent = 1\n\nStep 1\n\nnext = 2\n1 → null\n\nprevious = 1\ncurrent = 2\n\n-------------------\n\nStep 2\n\nnext = 3\n2 → 1 → null\n\nprevious = 2\ncurrent = 3\n\n-------------------\n\nStep 3\n\n3 → 2 → 1 → null\n\nprevious = 3\ncurrent = null\n\nReturn previous", "wordWrap": "true" }, "children": [] }, { "id": "37c39258-1e2a-465c-a949-6e5c617a8dcd", "type": "divider", "props": {}, "children": [] }, { "id": "cbe13e9d-8fa8-49a3-81d6-5a1db91d36dd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "c9749da7-23bc-4f6d-8161-5bf83d93e888", "type": "addDoc", "props": { "fileId": "cda5bb0f-8e19-4bd2-ade9-3680a2779867", "title": "Reverse LinkedList Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "b912ee8e-fd46-49a9-8334-4b012ab7ff26", "type": "divider", "props": {}, "children": [] }, { "id": "24bf5959-4026-4a12-bcbe-f366e9861fce", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "7933a349-bb76-4b2d-9a92-6db6d7ed5efe", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "eb0f616d-596e-4058-9997-f82c9c6c2348", "type": "divider", "props": {}, "children": [] }, { "id": "412229f4-6e8b-47ee-8b09-d92a6c77775a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "30c44f6f-b108-46b5-b2d5-8147e13101b8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The important part is not to lose the remaining list while reversing the current node.", "styles": {} } ], "children": [] }, { "id": "bcd7d468-c911-433f-b877-a13ab1339e01", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always save the next node before changing the pointer.", "styles": {} } ], "children": [] }, { "id": "b02f0c7c-7c9e-4350-a99e-b731778c6b71", "type": "codeBlock", "props": { "language": "javascript", "code": "ListNode next = current.next;\ncurrent.next = previous;", "wordWrap": "true" }, "children": [] }, { "id": "356360dd-d1bf-4c7b-ab4d-531c943deafd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This allows the traversal to continue safely.", "styles": {} } ], "children": [] }, { "id": "51c216d6-9148-40b3-afa8-6cf8479d836d", "type": "divider", "props": {}, "children": [] }, { "id": "283dfdcc-ac9d-45ff-95e7-49f6ec16d85c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "81a42746-f92e-4afc-8308-9c68e9887995", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use three pointers: ", "styles": {} }, { "type": "text", "text": "previous", "styles": { "code": true } }, { "type": "text", "text": ", ", "styles": {} }, { "type": "text", "text": "current", "styles": { "code": true } }, { "type": "text", "text": ", and ", "styles": {} }, { "type": "text", "text": "next", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "c6d23530-80f1-4752-8d4c-42c344c24409", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Reverse one link at a time.", "styles": {} } ], "children": [] }, { "id": "61e3cbf6-c513-4cf5-b95e-b8c097f49cfa", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Save the next node before modifying pointers.", "styles": {} } ], "children": [] }, { "id": "4ead2f73-9d5a-455a-9ebe-b70c3bc65e3b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "previous", "styles": { "code": true } }, { "type": "text", "text": " as the new head.", "styles": {} } ], "children": [] }, { "id": "b2635ba7-44bd-422b-b646-aec4a9e870f5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "cda5bb0f-8e19-4bd2-ade9-3680a2779867", "title": "Reverse LinkedList Code", "folderId": "8f57017d-db88-4c7b-a983-e946e1e25d88", "time": 1785346485801, "createdAt": 1785262460575, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class ListNode {\n\n int val;\n ListNode next;\n\n ListNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public ListNode reverseList(ListNode head) {\n\n ListNode previous = null;\n ListNode current = head;\n\n while (current != null) {\n\n ListNode next = current.next;\n\n current.next = previous;\n\n previous = current;\n\n current = next;\n }\n\n return previous;\n }\n\n // Helper method to create Linked List from array\n private static ListNode buildList(int[] values) {\n\n if (values.length == 0) {\n return null;\n }\n\n ListNode head = new ListNode(values[0]);\n ListNode current = head;\n\n for (int i = 1; i < values.length; i++) {\n current.next = new ListNode(values[i]);\n current = current.next;\n }\n\n return head;\n }\n\n // Helper method to print Linked List\n private static void printList(ListNode head) {\n\n if (head == null) {\n System.out.println(\"[]\");\n return;\n }\n\n System.out.print(\"[\");\n\n while (head != null) {\n\n System.out.print(head.val);\n\n if (head.next != null) {\n System.out.print(\",\");\n }\n\n head = head.next;\n }\n\n System.out.println(\"]\");\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n int[] head = Input.getIntArray(\"head\");\n int[] output = Input.getIntArray(\"output\");\n\n ListNode list = buildList(head);\n\n Solution sol = new Solution();\n\n ListNode result = sol.reverseList(list);\n\n System.out.print(\"Expected: \");\n printList(buildList(output));\n\n System.out.print(\"Actual: \");\n printList(result);\n }\n}", "language": "java", "output": "Expected: [5,4,3,2,1]\nActual: [5,4,3,2,1]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785262517690_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785262517690_0_0", "key": "head", "value": "[1,2,3,4,5]" }, { "id": "in_1785262517690_0_1", "key": "output", "value": "[5,4,3,2,1]" } ] }, { "id": "tc_1785262517690_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785262517690_1_0", "key": "head", "value": "[1,2]" }, { "id": "in_1785262517690_1_1", "key": "output", "value": "[2,1]" } ] }, { "id": "tc_1785262517690_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785262517690_2_0", "key": "head", "value": "[1]" }, { "id": "in_1785262517690_2_1", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785262517690_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785262517690_3_0", "key": "head", "value": "[]" }, { "id": "in_1785262517690_3_1", "key": "output", "value": "[]" } ] }, { "id": "tc_1785262517690_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785262517690_4_0", "key": "head", "value": "[10,20,30]" }, { "id": "in_1785262517690_4_1", "key": "output", "value": "[30,20,10]" } ] }, { "id": "tc_1785262517690_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785262517690_5_0", "key": "head", "value": "[7,8,9,10]" }, { "id": "in_1785262517690_5_1", "key": "output", "value": "[10,9,8,7]" } ] }, { "id": "tc_1785262517690_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785262517690_6_0", "key": "head", "value": "[100]" }, { "id": "in_1785262517690_6_1", "key": "output", "value": "[100]" } ] }, { "id": "tc_1785262517690_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785262517690_7_0", "key": "head", "value": "[5,4,3,2,1]" }, { "id": "in_1785262517690_7_1", "key": "output", "value": "[1,2,3,4,5]" } ] }, { "id": "tc_1785262517690_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785262517690_8_0", "key": "head", "value": "[9,9,9]" }, { "id": "in_1785262517690_8_1", "key": "output", "value": "[9,9,9]" } ] }, { "id": "tc_1785262517690_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785262517690_9_0", "key": "head", "value": "[11,22,33,44]" }, { "id": "in_1785262517690_9_1", "key": "output", "value": "[44,33,22,11]" } ] }, { "id": "tc_1785262517690_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785262517690_10_0", "key": "head", "value": "[]" }, { "id": "in_1785262517690_10_1", "key": "output", "value": "[]" } ] }, { "id": "tc_1785262517690_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785262517690_11_0", "key": "head", "value": "[1]" }, { "id": "in_1785262517690_11_1", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785262517690_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785262517690_12_0", "key": "head", "value": "[1,2]" }, { "id": "in_1785262517690_12_1", "key": "output", "value": "[2,1]" } ] } ], "selectedTestCaseId": "tc_1785262517690_0" } } ] } ] }, { "_id": "f4c87e90-64a3-4359-b0c3-8061a614dfe6", "name": "14. Merge Two Sorted Lists", "createdAt": 1785262608029, "updatedAt": 1785263317464, "parentId": "80222204-0cd3-444b-a343-2e8fcd2d8d3a", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "09300596-ce65-4b2a-a137-c11d197c11bb", "title": "Merge List Doc", "folderId": "f4c87e90-64a3-4359-b0c3-8061a614dfe6", "time": 1785263174686, "createdAt": 1785262613660, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "3841c42e-aba5-4474-acd8-54fe10994b08", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "14. Merge Two Sorted Lists", "styles": {} } ], "children": [] }, { "id": "de8040f1-b646-484a-9fc3-321311d3e53c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "69d248e3-80f8-45e7-9f51-d24980ee04ff", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "You are given the heads of two sorted linked lists ", "styles": {} }, { "type": "text", "text": "list1", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "list2", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "b0aa9811-81ce-4772-b931-13292ace147b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Merge the two lists into one sorted linked list and return the head of the merged list.", "styles": {} } ], "children": [] }, { "id": "d2464485-49e3-44a8-87d0-0ca9eaafdd05", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The merged list should be made by splicing together the nodes of the first two lists.", "styles": {} } ], "children": [] }, { "id": "81daf3b3-c6d5-4261-98b1-88f54cb75846", "type": "divider", "props": {}, "children": [] }, { "id": "96835982-291c-4b31-be51-42b3a2bf070c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "c8f00dda-13e9-4b25-bae0-925b1f115789", "type": "column_list", "props": {}, "children": [ { "id": "58b35ac0-0fab-4ed4-a601-64ad9e094db0", "type": "column", "props": { "width": "1" }, "children": [ { "id": "f9fa66c8-48c6-416a-a2d7-f23075e9e96a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "61974bf4-83d5-4404-8688-77d46c7793cf", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "43d678d7-2083-4dbe-a76a-fc9556a26d68", "type": "codeBlock", "props": { "language": "javascript", "code": "list1 = [1,2,4]\nlist2 = [1,3,4]", "wordWrap": "true" }, "children": [] }, { "id": "a7afe50f-9d2c-46f1-8fad-5b440eabce0d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "c1c07b9d-72c6-4a65-b1db-033d80a9f593", "type": "codeBlock", "props": { "language": "javascript", "code": "[1,1,2,3,4,4]", "wordWrap": "true" }, "children": [] }, { "id": "65a2c7ef-6baf-420b-bec8-e301adec0279", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "93ec4552-a52f-4889-84ff-fdb14d828a5e", "type": "column", "props": { "width": "1" }, "children": [ { "id": "00739d32-3ed4-47dc-8725-1bee6ba5833e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "0a59ec79-5ef3-4084-9b98-132f0a5e321c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "4b99ec3d-cf88-40f7-bd93-16baccc9598e", "type": "codeBlock", "props": { "language": "javascript", "code": "list1 = []\nlist2 = []", "wordWrap": "true" }, "children": [] }, { "id": "33b25ca0-0a52-40cf-96fc-cf6c8ba2b0b7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "dd08affd-8ead-477c-83d9-e1f9a2563c3c", "type": "codeBlock", "props": { "language": "javascript", "code": "[]", "wordWrap": "true" }, "children": [] }, { "id": "310636ee-385b-47bb-9a24-4229fe4d3e62", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "02868dae-6e82-4c0d-8181-de9b3cd5ab7d", "type": "column", "props": { "width": "1" }, "children": [ { "id": "a319064c-00c1-4c22-981b-598c9df20f24", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "a49a3284-5f61-41a7-a563-557e0c9d9bb8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "90c94f3f-c7b2-4c93-978c-d8115e92016a", "type": "codeBlock", "props": { "language": "javascript", "code": "list1 = []\nlist2 = [0]", "wordWrap": "true" }, "children": [] }, { "id": "f40361f4-3201-4d36-b278-366695b9b475", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "8b40316b-b74a-46f4-bf6d-37cd75d4fa44", "type": "codeBlock", "props": { "language": "javascript", "code": "[0]", "wordWrap": "true" }, "children": [] }, { "id": "a15bde95-271b-4c9b-b551-3f60eb16ed7b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "d09f2ed2-b4c5-4de1-93a2-f6cdf718051c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "7f6d49fc-c26d-4a58-be75-136e867dd3b2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Since both linked lists are already sorted, compare the current nodes of both lists and attach the smaller node to the merged list.", "styles": {} } ], "children": [] }, { "id": "9fa509e7-6900-46d3-bba4-8345d3ab2e00", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "dfc0131f-d13c-4484-8459-b293b91556eb", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create a dummy node.", "styles": {} } ], "children": [] }, { "id": "ff1d46a4-0630-4954-9447-b072b9e1714d", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain a pointer ", "styles": {} }, { "type": "text", "text": "current", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2ba4567e-f024-49aa-9fd6-f523f1e011d4", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare the current nodes of both lists.", "styles": {} } ], "children": [] }, { "id": "c38f6f68-b690-4c38-8c4b-f71b2734a23f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Attach the smaller node to the merged list.", "styles": {} } ], "children": [] }, { "id": "d20242b7-15ee-49dd-813d-c9c503917972", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Move the pointer of the selected list.", "styles": {} } ], "children": [] }, { "id": "3a13290f-dae0-4141-8297-f7fc77879693", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Continue until one list becomes empty.", "styles": {} } ], "children": [] }, { "id": "cb1df246-1492-4a63-825d-d780239ea2f7", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Attach the remaining nodes of the other list.", "styles": {} } ], "children": [] }, { "id": "561878fd-a1cb-41b2-8b89-9ef012f82052", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "dummy.next", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "e636f9ee-8d10-4133-98a5-295a893bd269", "type": "divider", "props": {}, "children": [] }, { "id": "abb21b3f-259f-4b1f-a870-9536ffc5c192", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "aaf849a2-28a4-4d7d-b0ec-15559c53deee", "type": "codeBlock", "props": { "language": "javascript", "code": "list1 = 1 → 2 → 4\nlist2 = 1 → 3 → 4\n\ndummy\n\nCompare 1 and 1\n\nTake list1\n\nMerged\n\n1\n\nCompare 2 and 1\n\nTake list2\n\nMerged\n\n1 → 1\n\nCompare 2 and 3\n\nTake list1\n\nMerged\n\n1 → 1 → 2\n\nCompare 4 and 3\n\nTake list2\n\nMerged\n\n1 → 1 → 2 → 3\n\nCompare 4 and 4\n\nTake list1\n\nMerged\n\n1 → 1 → 2 → 3 → 4\n\nAttach remaining node\n\n1 → 1 → 2 → 3 → 4 → 4", "wordWrap": "true" }, "children": [] }, { "id": "c4b95814-93bc-4efb-9449-153ac8b3f664", "type": "divider", "props": {}, "children": [] }, { "id": "c1929180-2933-4ee8-8e9c-00c082468c23", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "975d5be5-0940-4f11-930a-16b25a9eb677", "type": "addDoc", "props": { "fileId": "86adc4be-fe00-4b09-9886-10169b787ff3", "title": "Merge List Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "b265b96d-de4d-447e-9ccf-de535f65ade1", "type": "divider", "props": {}, "children": [] }, { "id": "7d8da0fb-cd28-4749-99df-aaddfc29c1c7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "96a3c6fb-c8e6-4850-94cd-32bd60e332c7", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n + m)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "58e5db14-5dc9-42b5-b9e3-ff6cd4a05b29", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "a644b6e8-98c0-46d7-94f9-d13f2c7a21e8", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes in list1", "styles": {} } ], "children": [] }, { "id": "537d4e33-27db-4eb1-957d-f3b95d97f22a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "m", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes in list2", "styles": {} } ], "children": [] }, { "id": "e9e6e462-eb23-484d-b857-525193ae2132", "type": "divider", "props": {}, "children": [] }, { "id": "20f1aae3-19cb-492b-b0a0-5f3f168d347e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "58a9f63d-ce0a-4191-ab24-9c7bbd8b5837", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a ", "styles": {} }, { "type": "text", "text": "dummy node", "styles": { "bold": true } }, { "type": "text", "text": " to simplify the implementation.", "styles": {} } ], "children": [] }, { "id": "8ce38f2d-877e-4ef8-9a98-1dfa09775f58", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Without a dummy node, you'll need special handling for the first node of the merged list. The dummy node removes this edge case and makes the code cleaner.", "styles": {} } ], "children": [] }, { "id": "4a5e0493-b4fd-490f-add0-86b3fc3c68a1", "type": "divider", "props": {}, "children": [] }, { "id": "e127c800-15de-4fc2-b7c4-87338714f077", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "6a8e2ede-06b2-4df7-a729-97a3fbdef7e9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Both lists are already sorted.", "styles": {} } ], "children": [] }, { "id": "5a6e9e46-d9fe-44b1-a5ac-b10ffde9b78e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare one node at a time.", "styles": {} } ], "children": [] }, { "id": "c2e1e2ea-b2a4-4301-abba-1d8ddfe33b6b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Attach the smaller node to the merged list.", "styles": {} } ], "children": [] }, { "id": "24113563-5478-408e-8a73-774ad4c79f34", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a dummy node to simplify pointer manipulation.", "styles": {} } ], "children": [] }, { "id": "e74c6efa-c376-43f8-b396-355c47933a9a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n + m)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "86adc4be-fe00-4b09-9886-10169b787ff3", "title": "Merge List Code", "folderId": "f4c87e90-64a3-4359-b0c3-8061a614dfe6", "time": 1785346490951, "createdAt": 1785262947077, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class ListNode {\n\n int val;\n ListNode next;\n\n ListNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n\n ListNode dummy = new ListNode(-1);\n ListNode current = dummy;\n\n while (list1 != null && list2 != null) {\n\n if (list1.val <= list2.val) {\n current.next = list1;\n list1 = list1.next;\n } else {\n current.next = list2;\n list2 = list2.next;\n }\n\n current = current.next;\n }\n\n if (list1 != null) {\n current.next = list1;\n }\n\n if (list2 != null) {\n current.next = list2;\n }\n\n return dummy.next;\n }\n\n // Helper method to build linked list\n private static ListNode buildList(int[] values) {\n\n if (values.length == 0) {\n return null;\n }\n\n ListNode head = new ListNode(values[0]);\n ListNode current = head;\n\n for (int i = 1; i < values.length; i++) {\n current.next = new ListNode(values[i]);\n current = current.next;\n }\n\n return head;\n }\n\n // Helper method to print linked list\n private static void printList(ListNode head) {\n\n if (head == null) {\n System.out.println(\"[]\");\n return;\n }\n\n System.out.print(\"[\");\n\n while (head != null) {\n\n System.out.print(head.val);\n\n if (head.next != null) {\n System.out.print(\",\");\n }\n\n head = head.next;\n }\n\n System.out.println(\"]\");\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n int[] list1 = Input.getIntArray(\"list1\");\n int[] list2 = Input.getIntArray(\"list2\");\n int[] output = Input.getIntArray(\"output\");\n\n ListNode head1 = buildList(list1);\n ListNode head2 = buildList(list2);\n\n Solution sol = new Solution();\n\n ListNode result = sol.mergeTwoLists(head1, head2);\n\n System.out.print(\"Expected: \");\n printList(buildList(output));\n\n System.out.print(\"Actual: \");\n printList(result);\n }\n}", "language": "java", "output": "Expected: [1,1,2,3,4,4]\nActual: [1,1,2,3,4,4]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785262999323_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785262999323_0_0", "key": "list1", "value": "[1,2,4]" }, { "id": "in_1785262999323_0_1", "key": "list2", "value": "[1,3,4]" }, { "id": "in_1785262999323_0_2", "key": "output", "value": "[1,1,2,3,4,4]" } ] }, { "id": "tc_1785262999323_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785262999323_1_0", "key": "list1", "value": "[]" }, { "id": "in_1785262999323_1_1", "key": "list2", "value": "[]" }, { "id": "in_1785262999323_1_2", "key": "output", "value": "[]" } ] }, { "id": "tc_1785262999323_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785262999323_2_0", "key": "list1", "value": "[]" }, { "id": "in_1785262999323_2_1", "key": "list2", "value": "[0]" }, { "id": "in_1785262999323_2_2", "key": "output", "value": "[0]" } ] }, { "id": "tc_1785262999324_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785262999324_3_0", "key": "list1", "value": "[1]" }, { "id": "in_1785262999324_3_1", "key": "list2", "value": "[2]" }, { "id": "in_1785262999324_3_2", "key": "output", "value": "[1,2]" } ] }, { "id": "tc_1785262999324_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785262999324_4_0", "key": "list1", "value": "[2]" }, { "id": "in_1785262999324_4_1", "key": "list2", "value": "[1]" }, { "id": "in_1785262999324_4_2", "key": "output", "value": "[1,2]" } ] }, { "id": "tc_1785262999324_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785262999324_5_0", "key": "list1", "value": "[1,3,5]" }, { "id": "in_1785262999324_5_1", "key": "list2", "value": "[2,4,6]" }, { "id": "in_1785262999324_5_2", "key": "output", "value": "[1,2,3,4,5,6]" } ] }, { "id": "tc_1785262999324_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785262999324_6_0", "key": "list1", "value": "[1,1,2]" }, { "id": "in_1785262999324_6_1", "key": "list2", "value": "[1,3]" }, { "id": "in_1785262999324_6_2", "key": "output", "value": "[1,1,1,2,3]" } ] }, { "id": "tc_1785262999324_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785262999324_7_0", "key": "list1", "value": "[5,10]" }, { "id": "in_1785262999324_7_1", "key": "list2", "value": "[1,2,3]" }, { "id": "in_1785262999324_7_2", "key": "output", "value": "[1,2,3,5,10]" } ] }, { "id": "tc_1785262999324_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785262999324_8_0", "key": "list1", "value": "[1,2,3]" }, { "id": "in_1785262999324_8_1", "key": "list2", "value": "[4,5,6]" }, { "id": "in_1785262999324_8_2", "key": "output", "value": "[1,2,3,4,5,6]" } ] }, { "id": "tc_1785262999324_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785262999324_9_0", "key": "list1", "value": "[10,20,30]" }, { "id": "in_1785262999324_9_1", "key": "list2", "value": "[15,25,35]" }, { "id": "in_1785262999324_9_2", "key": "output", "value": "[10,15,20,25,30,35]" } ] }, { "id": "tc_1785262999324_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785262999324_10_0", "key": "list1", "value": "[]" }, { "id": "in_1785262999324_10_1", "key": "list2", "value": "[1]" }, { "id": "in_1785262999324_10_2", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785262999324_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785262999324_11_0", "key": "list1", "value": "[1]" }, { "id": "in_1785262999324_11_1", "key": "list2", "value": "[]" }, { "id": "in_1785262999324_11_2", "key": "output", "value": "[1]" } ] }, { "id": "tc_1785262999324_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785262999324_12_0", "key": "list1", "value": "[]" }, { "id": "in_1785262999324_12_1", "key": "list2", "value": "[]" }, { "id": "in_1785262999324_12_2", "key": "output", "value": "[]" } ] } ], "selectedTestCaseId": "tc_1785262999323_0" } } ] } ] }, { "_id": "1e08c713-8e87-4b95-bd19-66d29f7633ce", "name": "15. Linked List Cycle", "createdAt": 1785263302716, "updatedAt": 1785263302716, "parentId": "80222204-0cd3-444b-a343-2e8fcd2d8d3a", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "150f6714-7a66-49c3-97ef-6ba998e69c6d", "title": "Linked List Cycle Doc", "folderId": "1e08c713-8e87-4b95-bd19-66d29f7633ce", "time": 1785263616348, "createdAt": 1785263355146, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "f13afed4-f2f4-4fc3-96a7-1aa3e5ad03c1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "15. Linked List Cycle", "styles": {} } ], "children": [] }, { "id": "f59ae55f-8149-40e9-bdce-a186e003b2ae", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "e1b5882d-57d0-4bc9-b033-0dbb2c313dc7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the ", "styles": {} }, { "type": "text", "text": "head", "styles": { "code": true } }, { "type": "text", "text": " of a linked list, determine if the linked list has a cycle.", "styles": {} } ], "children": [] }, { "id": "f34adbea-636b-4cee-8b6f-40673de429a3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A cycle exists if some node in the list can be reached again by continuously following the ", "styles": {} }, { "type": "text", "text": "next", "styles": { "code": true } }, { "type": "text", "text": " pointer.", "styles": {} } ], "children": [] }, { "id": "85e9c681-e744-425f-85e9-36a65ffde32c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " if there is a cycle; otherwise, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "84f330f3-c674-4ec6-bae6-ada25ad16a87", "type": "divider", "props": {}, "children": [] }, { "id": "1cc93180-72cd-4ad5-9a31-88e0dca5daac", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "74f72a2c-981b-49b7-9d24-a6dff6e63aa5", "type": "column_list", "props": {}, "children": [ { "id": "9f9dec00-f015-488c-a054-f77ac49015ed", "type": "column", "props": { "width": "1" }, "children": [ { "id": "97389b55-450a-4eb4-8ed0-f1b0ff6ab1d8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "b0d5e4f9-37c4-4e30-8afd-8114eba26028", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "3f29a725-0c64-4c79-ae9a-7042cf1e4be7", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [3,2,0,-4]\npos = 1", "wordWrap": "true" }, "children": [] }, { "id": "b002edeb-38bd-46e2-a89d-33031ae48944", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "8d0645e6-f125-4713-a0f2-7e4e5f0013e7", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "edb1740f-4e17-479d-a859-9a2f58caf136", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "86916dbf-be4e-43db-a80d-5020103f511d", "type": "codeBlock", "props": { "language": "javascript", "code": "The last node points back to the node at index 1.\n\n3 → 2 → 0 → -4\n ↑ |\n |_________|", "wordWrap": "true" }, "children": [] }, { "id": "2d3c8b66-1a48-4dd7-9563-faad0aa4ddd5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "24027bed-db66-4a7d-88cc-adcc1514c2dd", "type": "column", "props": { "width": "1" }, "children": [ { "id": "87bea08d-24bb-4fc1-8b60-3eb5636bc01e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "a28b3553-462c-4c68-b135-7d731865bcc5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "3c43e7c2-337b-44a4-b2a5-feb4329cfe96", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [1,2]\npos = 0", "wordWrap": "true" }, "children": [] }, { "id": "02161bfa-a5a6-4112-b74b-f98854cc23a1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "a99078e7-04e7-405f-935f-e712d05c7ea7", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "5ab65c7a-0df1-4bff-bd25-ae7b0f09bd34", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "c51ff805-8103-4f85-8be0-495ec288000b", "type": "column", "props": { "width": "1" }, "children": [ { "id": "482044e5-286e-404c-b56a-f7ac0c207044", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "629b5cea-3b32-4b10-8705-7b19d22b4e03", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "e2888470-288c-4ab1-b14d-5c907f45f3f3", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [1]\npos = -1", "wordWrap": "true" }, "children": [] }, { "id": "79caff92-53f7-4547-96d7-690186370fb5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "8a2b9859-e205-4465-8f66-ebbc1f1f2586", "type": "codeBlock", "props": { "language": "javascript", "code": "false", "wordWrap": "true" }, "children": [] }, { "id": "2cfb8862-4b4c-4568-b9f4-73f193a4d5ee", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "e50c4c12-dc8b-4359-94a8-1bd960faeab4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "f384e552-d28a-4aac-ade0-17ac77936200", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use Floyd's Cycle Detection Algorithm (also called the Tortoise and Hare Algorithm).", "styles": {} } ], "children": [] }, { "id": "98263a23-9e46-4cf7-b8f4-b31ae76e8d0f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain two pointers:", "styles": {} } ], "children": [] }, { "id": "e6789503-7ef1-4922-af29-a2195a7e9718", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Slow pointer moves one step at a time.", "styles": {} } ], "children": [] }, { "id": "13a27311-3238-4422-9ab4-ae48b5d9c4e3", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Fast pointer moves two steps at a time.", "styles": {} } ], "children": [] }, { "id": "64864214-c08c-43f4-8413-8184f23c00c8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If a cycle exists, the two pointers will eventually meet.", "styles": {} } ], "children": [] }, { "id": "e358f53c-8302-49eb-82e3-0a4f8060a8d5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the fast pointer reaches ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", there is no cycle.", "styles": {} } ], "children": [] }, { "id": "951404b9-8e3e-44ad-aa7d-557e0e73583c", "type": "divider", "props": {}, "children": [] }, { "id": "19da6052-f02b-47f9-bd12-d4738a50dbf1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "fa7f2c03-99b5-4e0c-96f9-b3b743a744f6", "type": "codeBlock", "props": { "language": "javascript", "code": "head = [3,2,0,-4]\npos = 1\n\nSlow = 3\nFast = 3\n\nMove\n\nSlow = 2\nFast = 0\n\nMove\n\nSlow = 0\nFast = 2\n\nMove\n\nSlow = -4\nFast = -4\n\nPointers meet\n\nReturn true", "wordWrap": "true" }, "children": [] }, { "id": "7578c6aa-98f0-4b1c-8b47-d06926c3b9ad", "type": "divider", "props": {}, "children": [] }, { "id": "522a1d09-a754-4df0-bca3-4e2c250f7d80", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "0dc7bbc6-0de5-43f2-b408-7dd836da8072", "type": "addDoc", "props": { "fileId": "ca184c86-cbe1-465d-831c-6b2badab578d", "title": "Linked List Cycle Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "bfd40bb8-a8d8-4683-8423-c496b2fbddf7", "type": "divider", "props": {}, "children": [] }, { "id": "9bd3148a-fac2-467d-b916-421731670d9a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "008c6906-76f9-4e75-acd4-8d64670c4d7a", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "6f1e3841-a619-4609-aa45-28e2a7e2372f", "type": "divider", "props": {}, "children": [] }, { "id": "53e41710-1715-416e-84b0-186bf5a325c4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "cbf0dd0f-b0fd-402e-b91f-58214c48337e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Do ", "styles": {} }, { "type": "text", "text": "not", "styles": { "bold": true } }, { "type": "text", "text": " use a HashSet unless specifically asked.", "styles": {} } ], "children": [] }, { "id": "6cb7f812-1da9-4242-8e80-a6aa1513daeb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The optimal solution is Floyd's Cycle Detection Algorithm using two pointers.", "styles": {} } ], "children": [] }, { "id": "4b1c3ffe-6d80-4181-80fe-4f3d40974377", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the fast pointer catches the slow pointer, a cycle exists.", "styles": {} } ], "children": [] }, { "id": "35b4975e-86cc-4e85-a053-41c7654f4610", "type": "divider", "props": {}, "children": [] }, { "id": "9903a8d7-7448-419d-b474-c7c9a2ee25c0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "865664ef-f5c1-47a4-8fdf-b369dca11b16", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use two pointers.", "styles": {} } ], "children": [] }, { "id": "624c1c36-d7f5-497a-83dc-6876a09cf4e5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Slow moves one node.", "styles": {} } ], "children": [] }, { "id": "75b6b560-2d54-43bc-869a-e1ad9cd07a63", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Fast moves two nodes.", "styles": {} } ], "children": [] }, { "id": "b6b00aa6-79a0-4f5c-8e7d-845bbb0673fa", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If they meet, a cycle exists.", "styles": {} } ], "children": [] }, { "id": "2e33e920-2f33-4218-bda1-28dd5f99c6d8", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If fast reaches ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", there is no cycle.", "styles": {} } ], "children": [] }, { "id": "3ca26a7a-9a48-462b-b547-ad7381a58b01", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " extra space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "ca184c86-cbe1-465d-831c-6b2badab578d", "title": "Linked List Cycle Code", "folderId": "1e08c713-8e87-4b95-bd19-66d29f7633ce", "time": 1785346496335, "createdAt": 1785263546067, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class ListNode {\n\n int val;\n ListNode next;\n\n ListNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public boolean hasCycle(ListNode head) {\n\n ListNode slow = head;\n ListNode fast = head;\n\n while (fast != null && fast.next != null) {\n\n slow = slow.next;\n fast = fast.next.next;\n\n if (slow == fast) {\n return true;\n }\n }\n\n return false;\n }\n\n // Helper method to build linked list\n private static ListNode buildList(int[] values, int pos) {\n\n if (values.length == 0) {\n return null;\n }\n\n ListNode head = new ListNode(values[0]);\n ListNode current = head;\n ListNode cycleNode = null;\n\n if (pos == 0) {\n cycleNode = head;\n }\n\n for (int i = 1; i < values.length; i++) {\n\n current.next = new ListNode(values[i]);\n current = current.next;\n\n if (i == pos) {\n cycleNode = current;\n }\n }\n\n if (pos != -1) {\n current.next = cycleNode;\n }\n\n return head;\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n int[] head = Input.getIntArray(\"head\");\n int pos = Input.getInt(\"pos\");\n boolean output = Input.getBool(\"output\");\n\n ListNode list = buildList(head, pos);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.hasCycle(list));\n }\n}", "language": "java", "output": "Expected: true\nActual: true\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785263578881_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785263578881_0_0", "key": "head", "value": "[3,2,0,-4]" }, { "id": "in_1785263578881_0_1", "key": "pos", "value": "1" }, { "id": "in_1785263578881_0_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578881_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785263578881_1_0", "key": "head", "value": "[1,2]" }, { "id": "in_1785263578881_1_1", "key": "pos", "value": "0" }, { "id": "in_1785263578881_1_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578881_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785263578881_2_0", "key": "head", "value": "[1]" }, { "id": "in_1785263578881_2_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578881_2_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578881_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785263578881_3_0", "key": "head", "value": "[1,2,3,4,5]" }, { "id": "in_1785263578881_3_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578881_3_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578881_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785263578881_4_0", "key": "head", "value": "[1,2,3,4,5]" }, { "id": "in_1785263578881_4_1", "key": "pos", "value": "2" }, { "id": "in_1785263578881_4_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578882_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785263578882_5_0", "key": "head", "value": "[10,20]" }, { "id": "in_1785263578882_5_1", "key": "pos", "value": "1" }, { "id": "in_1785263578882_5_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578882_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785263578882_6_0", "key": "head", "value": "[10,20]" }, { "id": "in_1785263578882_6_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578882_6_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578882_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785263578882_7_0", "key": "head", "value": "[5]" }, { "id": "in_1785263578882_7_1", "key": "pos", "value": "0" }, { "id": "in_1785263578882_7_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578882_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785263578882_8_0", "key": "head", "value": "[5]" }, { "id": "in_1785263578882_8_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578882_8_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578882_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785263578882_9_0", "key": "head", "value": "[1,2,3]" }, { "id": "in_1785263578882_9_1", "key": "pos", "value": "0" }, { "id": "in_1785263578882_9_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785263578882_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785263578882_10_0", "key": "head", "value": "[]" }, { "id": "in_1785263578882_10_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578882_10_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578882_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785263578882_11_0", "key": "head", "value": "[1]" }, { "id": "in_1785263578882_11_1", "key": "pos", "value": "-1" }, { "id": "in_1785263578882_11_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785263578882_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785263578882_12_0", "key": "head", "value": "[1]" }, { "id": "in_1785263578882_12_1", "key": "pos", "value": "0" }, { "id": "in_1785263578882_12_2", "key": "output", "value": "true" } ] } ], "selectedTestCaseId": "tc_1785263578881_0" } } ] } ] } ], "files": [] }, { "_id": "46783974-31cf-404c-b2bb-567dafe40add", "name": "5. Stack & Queue", "createdAt": 1785296689737, "updatedAt": 1785296689737, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "7eba7428-5349-43d6-856d-4226067629ce", "name": "16. Valid Parentheses", "createdAt": 1785296782767, "updatedAt": 1785296798598, "parentId": "46783974-31cf-404c-b2bb-567dafe40add", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "2120714e-a546-4723-b3b7-045562006b40", "title": "Valid Parentheses Doc", "folderId": "7eba7428-5349-43d6-856d-4226067629ce", "time": 1785297301759, "createdAt": 1785296802891, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "61a8b428-a0e2-492c-bfe5-432ece79865b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "16. Valid Parentheses", "styles": {} } ], "children": [] }, { "id": "7e58c468-bfee-427c-9b12-06e68f29199b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "3cd53925-3582-49b6-935b-a997691cb5f4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given a string ", "styles": {} }, { "type": "text", "text": "s", "styles": { "code": true } }, { "type": "text", "text": " containing just the characters:", "styles": {} } ], "children": [] }, { "id": "ef2ae067-12f7-4a53-b00a-7341cd1739a1", "type": "codeBlock", "props": { "language": "javascript", "code": "'(', ')', '{', '}', '[' and ']'", "wordWrap": "true" }, "children": [] }, { "id": "12bd9ce8-e33b-4e77-b811-3b1ce08def26", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "determine if the input string is valid.", "styles": {} } ], "children": [] }, { "id": "31b26e03-0fb3-4cf3-a281-162e85ca6701", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A string is valid if:", "styles": {} } ], "children": [] }, { "id": "2ce8bc88-7aa2-42fc-aee3-23b3c110aec7", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Every opening bracket has a corresponding closing bracket of the same type.", "styles": {} } ], "children": [] }, { "id": "e0a6a74b-77d4-4dcb-aa42-e1499e63fd43", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Opening brackets are closed in the correct order.", "styles": {} } ], "children": [] }, { "id": "00982e86-cd42-412d-9dfe-9066b3b8392e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Every closing bracket has a matching opening bracket.", "styles": {} } ], "children": [] }, { "id": "86dc7440-b841-4989-a42c-e7885a663f9a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " if the string is valid; otherwise, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "e81db8d8-e3e2-4eca-8dc5-c5c9de446b0f", "type": "divider", "props": {}, "children": [] }, { "id": "3cf81201-ceb5-471f-b0ff-c3e1d8e552e7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "a8ede9a1-c8be-4860-8a99-b9a58fc87af6", "type": "column_list", "props": {}, "children": [ { "id": "576b0ac2-2a5e-4e7c-b9c3-a04b147736ef", "type": "column", "props": { "width": "1" }, "children": [ { "id": "93adb80d-2a7f-4684-9fd0-3f4dab1c281d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "9ad8cb55-7561-47d4-b644-d58c20cac773", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "ca38e61b-0347-4326-999a-ffd44eab53cc", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"()\"", "wordWrap": "true" }, "children": [] }, { "id": "8c393c47-58f2-49f8-80ac-f53bb330147b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "48f03431-5a4c-48f8-83c4-6b86778e8381", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "1ae5e743-dea3-4ed5-90ee-b5ba121a135e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "f2461a3e-9725-42a5-aeed-915b22493adf", "type": "column", "props": { "width": "1" }, "children": [ { "id": "fa65b4e5-03c3-4630-9e00-d14837dc977c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "e43eef11-f507-49f3-86d3-a70c0376ba87", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "8fcdd596-6abf-4417-b444-48e997f1ee75", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"()[]{}\"", "wordWrap": "true" }, "children": [] }, { "id": "17746453-4b2b-4b83-a0b9-4f584959f7b5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "11d7ae86-9f8b-44c8-9c04-0a5dfc94ee08", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "16bb51bb-15b6-464f-b449-e00f6edbbbc0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "b0f428de-4866-44d9-8222-dc96944fcc23", "type": "column", "props": { "width": "1" }, "children": [ { "id": "c091686e-c09c-4ced-86dd-e802f9eb66ca", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "025bcf15-913a-485c-858a-4eed10b31181", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "250e93ff-685f-4dab-9dbd-bdbd03d287cc", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"(]\"", "wordWrap": "true" }, "children": [] }, { "id": "1e963e7c-a528-4a30-b5a5-3f95340d36fc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "eb61aabc-c859-4a48-85ec-aff600da40f2", "type": "codeBlock", "props": { "language": "javascript", "code": "false", "wordWrap": "true" }, "children": [] }, { "id": "0d767aea-3f0e-473b-8b56-b00269033015", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "e9ddc215-8407-4add-90e2-0dbb84be77a7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "fd5d3ebb-8e9d-4336-81cb-833dbffaa1d8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a ", "styles": {} }, { "type": "text", "text": "Stack", "styles": { "bold": true } }, { "type": "text", "text": " to keep track of opening brackets.", "styles": {} } ], "children": [] }, { "id": "b79bf7c9-1ddf-4e41-8d76-75ee4a645070", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "d0825279-bc3f-4ca1-a046-08b00c3f49fe", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse the string character by character.", "styles": {} } ], "children": [] }, { "id": "b55009a1-9a59-4d64-81de-347e3ec79704", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the character is an opening bracket, push it onto the stack.", "styles": {} } ], "children": [] }, { "id": "88c013a4-67d7-4761-9c2d-3cb4b3c6cf65", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If it is a closing bracket:", "styles": {} } ], "children": [ { "id": "9b25f490-00fc-4638-97e9-3366986f1a6b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the stack is empty, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d9cc4bfe-daa9-4746-a7bc-3af603cefe23", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Pop the top element.", "styles": {} } ], "children": [] }, { "id": "32984b82-12e2-4227-916f-fd779b6c3152", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Check whether it matches the current closing bracket.", "styles": {} } ], "children": [] } ] }, { "id": "1a1e8ba1-9bfe-47d2-8a33-af038022c95a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "After processing all characters, the stack should be empty.", "styles": {} } ], "children": [] }, { "id": "e569ed8e-7adc-4182-8960-47ab38208b27", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the stack is empty, return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": "; otherwise, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "19936cc6-8e95-437e-8512-6fa014c07e47", "type": "divider", "props": {}, "children": [] }, { "id": "9da7b173-b371-4bbd-8e64-aca4ebbe3f85", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "319436ca-8697-4fb3-b5d3-5639b1d534b4", "type": "codeBlock", "props": { "language": "javascript", "code": "s = \"({[]})\"\n\nRead '('\n\nStack\n\n(\n\n-------------------\n\nRead '{'\n\nStack\n\n(\n{\n\n-------------------\n\nRead '['\n\nStack\n\n(\n{\n[\n\n-------------------\n\nRead ']'\n\nPop '['\n\nStack\n\n(\n{\n\n-------------------\n\nRead '}'\n\nPop '{'\n\nStack\n\n(\n\n-------------------\n\nRead ')'\n\nPop '('\n\nStack Empty\n\nReturn true", "wordWrap": "true" }, "children": [] }, { "id": "8bf7f337-befc-4e32-bc3c-efa1057850ac", "type": "divider", "props": {}, "children": [] }, { "id": "b24345a7-430d-465b-bb52-c51b589b9486", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "353de496-3e16-4d65-a298-f5e7d48cb6ca", "type": "addDoc", "props": { "fileId": "be81ad11-47e7-4820-ac74-72a078633af4", "title": "Valid Parentheses Code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "3a20fbff-43ee-4d25-b904-1f06ad76a030", "type": "divider", "props": {}, "children": [] }, { "id": "11ca23c7-c7dc-43a2-860f-ca42b0b6c45b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "5f6d13ba-b0c7-4ebd-b4d4-df1f163300f7", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "603e1f72-a35a-4257-95dc-cb9d582c57c2", "type": "divider", "props": {}, "children": [] }, { "id": "422dd90a-b7ce-4f75-8fd4-5a6746927a83", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "95415085-88d4-4b3c-a79e-0cc6f34ccf88", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A stack follows the ", "styles": {} }, { "type": "text", "text": "Last In, First Out (LIFO)", "styles": { "bold": true } }, { "type": "text", "text": " principle.", "styles": {} } ], "children": [] }, { "id": "8e442673-0613-493e-a6f0-35ae3983fa8a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The most recently opened bracket must be closed first.", "styles": {} } ], "children": [] }, { "id": "6222bc96-45f6-4123-8c5f-f3eb0144b877", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If at any point a closing bracket doesn't match the top of the stack, the string is invalid.", "styles": {} } ], "children": [] }, { "id": "871c48c7-5c34-495d-bb4c-9983338ad052", "type": "divider", "props": {}, "children": [] }, { "id": "227a2dc5-254b-45a2-9f9d-2b39ca12dc09", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "ff997066-23b2-4e9c-9b26-37cc818b64bb", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a stack to store opening brackets.", "styles": {} } ], "children": [] }, { "id": "d8a4c54a-5965-4e38-b188-8121c2ab9bee", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Push opening brackets onto the stack.", "styles": {} } ], "children": [] }, { "id": "cd73b80a-1e93-45d0-bb7d-7244fa659b6f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Pop and compare when encountering closing brackets.", "styles": {} } ], "children": [] }, { "id": "aadc97c1-05fb-4c5d-8147-fa53f8bd7e42", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The stack should be empty after processing the string.", "styles": {} } ], "children": [] }, { "id": "10baa785-18b2-46ad-863b-6133a92a13e6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "be81ad11-47e7-4820-ac74-72a078633af4", "title": "Valid Parentheses Code", "folderId": "7eba7428-5349-43d6-856d-4226067629ce", "time": 1785346502171, "createdAt": 1785297219286, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.Stack;\n\nclass Solution {\n\n public boolean isValid(String s) {\n\n Stack stack = new Stack<>();\n\n for (char ch : s.toCharArray()) {\n\n if (ch == '(' || ch == '{' || ch == '[') {\n\n stack.push(ch);\n\n } else {\n\n if (stack.isEmpty()) {\n return false;\n }\n\n char top = stack.pop();\n\n if (ch == ')' && top != '(') {\n return false;\n }\n\n if (ch == '}' && top != '{') {\n return false;\n }\n\n if (ch == ']' && top != '[') {\n return false;\n }\n }\n }\n\n return stack.isEmpty();\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n String s = Input.getString(\"s\");\n boolean output = Input.getBool(\"output\");\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.isValid(s));\n }\n}", "language": "java", "output": "Expected: true\nActual: true\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785297247068_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785297247068_0_0", "key": "s", "value": "()" }, { "id": "in_1785297247068_0_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785297247068_1_0", "key": "s", "value": "()[]{}" }, { "id": "in_1785297247068_1_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785297247068_2_0", "key": "s", "value": "(]" }, { "id": "in_1785297247068_2_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785297247068_3_0", "key": "s", "value": "([)]" }, { "id": "in_1785297247068_3_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785297247068_4_0", "key": "s", "value": "{[]}" }, { "id": "in_1785297247068_4_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785297247068_5_0", "key": "s", "value": "((()))" }, { "id": "in_1785297247068_5_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785297247068_6_0", "key": "s", "value": "(((" }, { "id": "in_1785297247068_6_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785297247068_7_0", "key": "s", "value": ")))" }, { "id": "in_1785297247068_7_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785297247068_8_0", "key": "s", "value": "[{()}]" }, { "id": "in_1785297247068_8_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785297247068_9_0", "key": "s", "value": "{[(])}" }, { "id": "in_1785297247068_9_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785297247068_10_0", "key": "s", "value": "" }, { "id": "in_1785297247068_10_1", "key": "output", "value": "true" } ] }, { "id": "tc_1785297247068_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785297247068_11_0", "key": "s", "value": "(" }, { "id": "in_1785297247068_11_1", "key": "output", "value": "false" } ] }, { "id": "tc_1785297247068_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785297247068_12_0", "key": "s", "value": "]" }, { "id": "in_1785297247068_12_1", "key": "output", "value": "false" } ] } ], "selectedTestCaseId": "tc_1785297247068_0" } } ] } ] }, { "_id": "09c72ef7-ed9d-46b2-bae2-6f775e999ab4", "name": "17. Min Stack", "createdAt": 1785297380803, "updatedAt": 1785297380803, "parentId": "46783974-31cf-404c-b2bb-567dafe40add", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "76d8b02c-aadc-49ec-86e9-740539966d14", "title": "Min Stack Doc", "folderId": "09c72ef7-ed9d-46b2-bae2-6f775e999ab4", "time": 1785298355384, "createdAt": 1785297449792, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "7c28e1a9-7970-44fa-b2b7-add4f1906cb3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "17. Min Stack", "styles": {} } ], "children": [] }, { "id": "c3f36887-45ac-4c58-89d2-53e45ac12060", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "c577dd46-47d3-401d-90af-a02dd10efb5c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Design a stack that supports the following operations in ", "styles": {} }, { "type": "text", "text": "constant time", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "8e23e0b1-05e1-4d21-8391-f99864452d47", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "push(val)", "styles": { "code": true } }, { "type": "text", "text": " — Push the element onto the stack.", "styles": {} } ], "children": [] }, { "id": "28803c98-05af-4c61-a5e5-c8d9d5a0152a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "pop()", "styles": { "code": true } }, { "type": "text", "text": " — Remove the element on the top of the stack.", "styles": {} } ], "children": [] }, { "id": "4575cad5-6040-433c-8d43-4f21f5fcf8b2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "top()", "styles": { "code": true } }, { "type": "text", "text": " — Get the top element.", "styles": {} } ], "children": [] }, { "id": "166584cf-b310-4540-bb12-218eba841cb2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "getMin()", "styles": { "code": true } }, { "type": "text", "text": " — Retrieve the minimum element currently in the stack.", "styles": {} } ], "children": [] }, { "id": "c462036b-d4d4-4e1b-89c5-c95be6e9ad01", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "All operations must run in ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] }, { "id": "462204c0-9c59-43ab-a1f1-6b60a7f8560b", "type": "divider", "props": {}, "children": [] }, { "id": "c4df0e47-66f3-484a-afa0-70755a490c89", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "98f2fef0-0d47-40ca-b2c3-cc55faef3cc1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "e1085cd1-1618-45b0-ae1e-074e49fe17bf", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "63524ec4-0c5f-4819-bcda-43b5d28c5277", "type": "codeBlock", "props": { "language": "javascript", "code": "[\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"]\n\n[[],[-2],[0],[-3],[],[],[],[]]", "wordWrap": "true" }, "children": [] }, { "id": "07b9a71a-b16c-410b-b131-98ff924a2e2e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "33ed5893-3a4e-46ae-bf91-3a4ef386f197", "type": "codeBlock", "props": { "language": "javascript", "code": "[null,null,null,null,-3,null,0,-2]", "wordWrap": "true" }, "children": [] }, { "id": "d747965a-e792-4666-b802-6778dabbbba0", "type": "divider", "props": {}, "children": [] }, { "id": "3fb435eb-e922-4a81-ac49-c450a3824497", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "c9d84cac-a4c1-40f5-ac7a-901286a4a999", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "two stacks", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d27c0e9c-c20d-4336-960d-b10a35fcd8cf", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Main Stack", "styles": { "bold": true } }, { "type": "text", "text": " stores all the values.", "styles": {} } ], "children": [] }, { "id": "1d418f6f-8857-4b4b-b06b-61807954e1fc", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Min Stack", "styles": { "bold": true } }, { "type": "text", "text": " stores the minimum value seen so far.", "styles": {} } ], "children": [] }, { "id": "207a00a3-2e90-486e-b3e0-ac3d0aaee683", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "7dd3f0be-99bf-42c0-9402-b0c99f27219b", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Push every value into the main stack.", "styles": {} } ], "children": [] }, { "id": "6cb46438-7ae8-430e-b8ae-024a9154c652", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the min stack is empty or the current value is less than or equal to the current minimum, push it into the min stack.", "styles": {} } ], "children": [] }, { "id": "deafd9ab-aa6e-4f82-a8a6-a2a19702bc6f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "During pop, if the popped value equals the current minimum, also pop from the min stack.", "styles": {} } ], "children": [] }, { "id": "0fa446c1-480f-430b-813b-abc32b149722", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The top of the min stack is always the minimum element.", "styles": {} } ], "children": [] }, { "id": "7c640761-299f-4885-ad5d-f6873176f204", "type": "divider", "props": {}, "children": [] }, { "id": "dad14f7d-6152-41b6-aa51-8ab9fa2dbb73", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "5622b84b-4c02-4cfd-b640-dfcf4b2fd8da", "type": "codeBlock", "props": { "language": "javascript", "code": "Operations\n\npush(-2)\n\nMain Stack\n[-2]\n\nMin Stack\n[-2]\n\n-------------------\n\npush(0)\n\nMain Stack\n[-2,0]\n\nMin Stack\n[-2]\n\n-------------------\n\npush(-3)\n\nMain Stack\n[-2,0,-3]\n\nMin Stack\n[-2,-3]\n\n-------------------\n\ngetMin()\n\nAnswer = -3\n\n-------------------\n\npop()\n\nMain Stack\n[-2,0]\n\nMin Stack\n[-2]\n\n-------------------\n\ntop()\n\nAnswer = 0\n\n-------------------\n\ngetMin()\n\nAnswer = -2", "wordWrap": "true" }, "children": [] }, { "id": "78657a0d-a4a7-437c-ba79-abbf850808e8", "type": "divider", "props": {}, "children": [] }, { "id": "fa10d591-9d3c-45a9-b981-1c850d17f58a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "577de75f-5d81-4b52-a39a-ae3a30074ce9", "type": "addDoc", "props": { "fileId": "db8a73ed-2402-418e-a2e0-58cc9e2b7415", "title": "Min Stack code", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "8d0d0bd4-bbfd-473f-a559-e0336bba1242", "type": "divider", "props": {}, "children": [] }, { "id": "1d9355d1-4b53-4125-9d9c-0dc6ff4425ec", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "60e4c601-ec43-4e53-93b9-709c92819f7f", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " for all operations", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "ae89b17c-478d-4272-91f1-4cab19cb5994", "type": "divider", "props": {}, "children": [] }, { "id": "9146ebcf-0703-4172-ad6d-ac552ba34096", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "18c44a0e-50d9-466a-8aaa-3c4cb6a3d521", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The trick is ", "styles": {} }, { "type": "text", "text": "not", "styles": { "bold": true } }, { "type": "text", "text": " to search for the minimum every time.", "styles": {} } ], "children": [] }, { "id": "aa863afa-b37e-4769-9201-1f81a7f2009f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain another stack that stores the minimum value at every stage, allowing ", "styles": {} }, { "type": "text", "text": "getMin()", "styles": { "code": true } }, { "type": "text", "text": " to return the answer in constant time.", "styles": {} } ], "children": [] }, { "id": "22f621db-81f8-4746-9f9c-ed2a90cfdaa1", "type": "divider", "props": {}, "children": [] }, { "id": "249d289b-da50-4a48-bd6f-c66ad3e7ec9f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "c44cb7ee-a03d-4983-9e43-6f790eb4502e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use two stacks.", "styles": {} } ], "children": [] }, { "id": "ab453e9d-f6b1-4a55-8613-4035d59e2271", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Main stack stores all elements.", "styles": {} } ], "children": [] }, { "id": "44041193-b9a3-4fa5-b355-b8587eeca206", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Min stack stores the current minimums.", "styles": {} } ], "children": [] }, { "id": "d441df31-a517-46f8-b1ff-e4c1559f1a63", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "All operations run in ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] }, { "id": "7bdce606-553e-4914-9cb2-9f1600f85870", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Extra space is ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "db8a73ed-2402-418e-a2e0-58cc9e2b7415", "title": "Min Stack code", "folderId": "09c72ef7-ed9d-46b2-bae2-6f775e999ab4", "time": 1785346507204, "createdAt": 1785297485728, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass MinStack {\n\n private Stack stack;\n private Stack minStack;\n\n public MinStack() {\n stack = new Stack<>();\n minStack = new Stack<>();\n }\n\n public void push(int val) {\n\n stack.push(val);\n\n if (minStack.isEmpty() || val <= minStack.peek()) {\n minStack.push(val);\n }\n }\n\n public void pop() {\n\n if (stack.peek().equals(minStack.peek())) {\n minStack.pop();\n }\n\n stack.pop();\n }\n\n public int top() {\n return stack.peek();\n }\n\n public int getMin() {\n return minStack.peek();\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n String[] operations = Input.getArray(\"operations\");\n String[] values = Input.getArray(\"values\");\n\n MinStack minStack = new MinStack();\n\n for (int i = 0; i < operations.length; i++) {\n\n String operation = operations[i].replace(\"\\\"\", \"\").trim();\n \n switch (operation) {\n \n case \"push\":\n int value = Integer.parseInt(values[i].replace(\"\\\"\", \"\").trim());\n minStack.push(value);\n System.out.println(\"push(\" + value + \")\");\n break;\n \n case \"pop\":\n minStack.pop();\n System.out.println(\"pop()\");\n break;\n \n case \"top\":\n System.out.println(\"top() = \" + minStack.top());\n break;\n \n case \"getMin\":\n System.out.println(\"getMin() = \" + minStack.getMin());\n break;\n }\n}\n }\n}", "language": "java", "output": "push(-2)\npush(0)\npush(-3)\ngetMin() = -3\npop()\ntop() = 0\ngetMin() = -2\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785297514572_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785297514572_0_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"]" }, { "id": "in_1785297514572_0_1", "key": "values", "value": "[\"-2\",\"0\",\"-3\",\"\",\"\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785297514572_1_0", "key": "operations", "value": "[\"push\",\"push\",\"getMin\"]" }, { "id": "in_1785297514572_1_1", "key": "values", "value": "[\"5\",\"3\",\"\"]" } ] }, { "id": "tc_1785297514572_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785297514572_2_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"pop\",\"getMin\"]" }, { "id": "in_1785297514572_2_1", "key": "values", "value": "[\"2\",\"1\",\"4\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785297514572_3_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"getMin\"]" }, { "id": "in_1785297514572_3_1", "key": "values", "value": "[\"10\",\"20\",\"5\",\"\"]" } ] }, { "id": "tc_1785297514572_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785297514572_4_0", "key": "operations", "value": "[\"push\",\"push\",\"pop\",\"top\"]" }, { "id": "in_1785297514572_4_1", "key": "values", "value": "[\"7\",\"8\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785297514572_5_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"pop\",\"pop\",\"getMin\"]" }, { "id": "in_1785297514572_5_1", "key": "values", "value": "[\"5\",\"4\",\"3\",\"\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785297514572_6_0", "key": "operations", "value": "[\"push\",\"getMin\"]" }, { "id": "in_1785297514572_6_1", "key": "values", "value": "[\"100\",\"\"]" } ] }, { "id": "tc_1785297514572_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785297514572_7_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"top\"]" }, { "id": "in_1785297514572_7_1", "key": "values", "value": "[\"1\",\"2\",\"3\",\"\"]" } ] }, { "id": "tc_1785297514572_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785297514572_8_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"getMin\"]" }, { "id": "in_1785297514572_8_1", "key": "values", "value": "[\"9\",\"7\",\"8\",\"\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785297514572_9_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"pop\",\"top\",\"getMin\"]" }, { "id": "in_1785297514572_9_1", "key": "values", "value": "[\"4\",\"2\",\"6\",\"\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785297514572_10_0", "key": "operations", "value": "[\"push\",\"getMin\"]" }, { "id": "in_1785297514572_10_1", "key": "values", "value": "[\"-1\",\"\"]" } ] }, { "id": "tc_1785297514572_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785297514572_11_0", "key": "operations", "value": "[\"push\",\"push\",\"pop\",\"getMin\"]" }, { "id": "in_1785297514572_11_1", "key": "values", "value": "[\"2\",\"2\",\"\",\"\"]" } ] }, { "id": "tc_1785297514572_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785297514572_12_0", "key": "operations", "value": "[\"push\",\"push\",\"push\",\"pop\",\"pop\",\"top\"]" }, { "id": "in_1785297514572_12_1", "key": "values", "value": "[\"1\",\"2\",\"3\",\"\",\"\",\"\"]" } ] } ], "selectedTestCaseId": "tc_1785297514572_0" } } ] } ] }, { "_id": "108d03fd-0258-4d5d-aabe-a74f39c9a0e9", "name": "18. Daily Temperatures", "createdAt": 1785343023092, "updatedAt": 1785343023092, "parentId": "46783974-31cf-404c-b2bb-567dafe40add", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "74ea9c24-5e99-4df4-aff6-fcbf70f19117", "title": "Daily Temperatures Doc", "folderId": "108d03fd-0258-4d5d-aabe-a74f39c9a0e9", "time": 1785347042936, "createdAt": 1785343026283, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "c65a129e-60c9-4a35-9dd1-bf21f3f3d8ad", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "18. Daily Temperatures", "styles": {} } ], "children": [] }, { "id": "77144565-9e5d-40cb-9f65-2a74de46f84d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "e43fc820-4a02-490b-84bd-c49edf95fd83", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an array of integers ", "styles": {} }, { "type": "text", "text": "temperatures", "styles": { "code": true } }, { "type": "text", "text": " representing the daily temperatures, return an array ", "styles": {} }, { "type": "text", "text": "answer", "styles": { "code": true } }, { "type": "text", "text": " such that:", "styles": {} } ], "children": [] }, { "id": "7b87b34d-1d57-4d23-91d0-688d66fcc252", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "answer[i]", "styles": { "code": true } }, { "type": "text", "text": " is the number of days you have to wait after the ", "styles": {} }, { "type": "text", "text": "iᵗʰ", "styles": { "code": true } }, { "type": "text", "text": " day to get a warmer temperature.", "styles": {} } ], "children": [] }, { "id": "caba86f7-6f21-41be-b66c-df323d9d22a6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If there is no future day with a warmer temperature, keep ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": " at that position.", "styles": {} } ], "children": [] }, { "id": "12c46e54-9cc3-4b46-8cdc-ed6795688355", "type": "divider", "props": {}, "children": [] }, { "id": "eb92f9c6-0c93-41bb-b255-8ebef306a9fe", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "b0d1d294-d0be-48ac-8e68-f1c890f2b160", "type": "column_list", "props": {}, "children": [ { "id": "696686a6-217a-4f10-a182-4628626fae3f", "type": "column", "props": { "width": "1" }, "children": [ { "id": "b8e01f2a-4de2-4f0e-b17a-18924b20ff29", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "33f84e57-c267-454c-8ebb-4c0a5e3b1765", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "8b36522b-a999-4d9b-80f3-3613a14f835a", "type": "codeBlock", "props": { "language": "javascript", "code": "temperatures = [73,74,75,71,69,72,76,73]", "wordWrap": "true" }, "children": [] }, { "id": "7e171fc6-634f-4659-912f-fd46abc2f9a9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "5ff65631-8e1a-450c-a165-9a46cbd0026f", "type": "codeBlock", "props": { "language": "javascript", "code": "[1,1,4,2,1,1,0,0]", "wordWrap": "true" }, "children": [] }, { "id": "48e7d7f5-4515-4ead-84f8-aa9342c63960", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "56475dff-416a-486c-9fdd-9a85dd996fdf", "type": "column", "props": { "width": "1" }, "children": [ { "id": "203e5933-df59-4b13-8e45-2fed07a28986", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "f368ec6d-68a3-4b61-abc1-362061c0a9b9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "38f1c696-6664-4858-a6a0-5ce03eb077b9", "type": "codeBlock", "props": { "language": "javascript", "code": "temperatures = [30,40,50,60]", "wordWrap": "true" }, "children": [] }, { "id": "813c3b9f-d280-40ad-924c-445f80c50030", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "92456bdb-d0f7-47a5-a7a7-67d833be916c", "type": "codeBlock", "props": { "language": "javascript", "code": "[1,1,1,0]", "wordWrap": "true" }, "children": [] }, { "id": "7490af38-08b0-4a46-bcb7-57454f981c68", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "ebf37d9b-d5b9-43a9-8bf1-eac1052246c0", "type": "column", "props": { "width": "1" }, "children": [ { "id": "1aefb302-4ab0-40af-b925-cf4be718c2e6", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "40466415-8f02-4343-98ce-96dc35f97c6d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "ac18b214-68a6-42c6-bbaf-1c7f08879299", "type": "codeBlock", "props": { "language": "javascript", "code": "temperatures = [30,60,90]", "wordWrap": "true" }, "children": [] }, { "id": "bde816c9-bbcf-4880-a560-0a6a0b51b223", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "970a2bd3-57b3-43a0-9a0f-1e1e161de53e", "type": "codeBlock", "props": { "language": "javascript", "code": "[1,1,0]", "wordWrap": "true" }, "children": [] }, { "id": "ac06d808-2beb-4263-af8f-aab652d632a9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "92abe954-bbaf-4eca-8daa-187ba2838d94", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "efc2bf86-5213-4946-a841-96795fc554d2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a ", "styles": {} }, { "type": "text", "text": "Monotonic Decreasing Stack", "styles": { "bold": true } }, { "type": "text", "text": " to keep track of the indices of temperatures.", "styles": {} } ], "children": [] }, { "id": "b5214f86-8923-428c-a442-b1606161c371", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "e74df58a-8a27-41d4-8ff2-18c08e4d36eb", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse the temperature array from left to right.", "styles": {} } ], "children": [] }, { "id": "2fe2096f-4151-40c9-969d-ca5e5547382a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store indices in the stack.", "styles": {} } ], "children": [] }, { "id": "891a0046-6b1f-470b-96cb-f2317464059f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "While the current temperature is greater than the temperature at the top index of the stack:", "styles": {} } ], "children": [ { "id": "d9ff12fc-4071-42c2-9782-609d825380ce", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Pop the index.", "styles": {} } ], "children": [] }, { "id": "93dd15df-8c95-419e-bab2-ad9181bffcc6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Calculate the number of days waited.", "styles": {} } ], "children": [] } ] }, { "id": "717353a6-9b62-4516-baeb-38cb6b1349c9", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Push the current index onto the stack.", "styles": {} } ], "children": [] }, { "id": "ae77b502-e7b7-4545-a25b-6087b2039031", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Any indices remaining in the stack have no warmer future day, so their answer remains ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "6b1dc262-b144-46da-a81e-3e4bdc8a3216", "type": "divider", "props": {}, "children": [] }, { "id": "77f00e3f-7027-428d-984a-8b6b5d202d67", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "c5236cb5-3d66-4941-9589-1c8615428eb9", "type": "codeBlock", "props": { "language": "javascript", "code": "temperatures = [73,74,75,71,69,72,76,73]\n\nStack = []\n\n73\n\nStack = [0]\n\n----------------\n\n74 > 73\n\nanswer[0] = 1\n\nStack = [1]\n\n----------------\n\n75 > 74\n\nanswer[1] = 1\n\nStack = [2]\n\n----------------\n\n71\n\nStack = [2,3]\n\n----------------\n\n69\n\nStack = [2,3,4]\n\n----------------\n\n72 > 69\n\nanswer[4] = 1\n\n72 > 71\n\nanswer[3] = 2\n\nStack = [2,5]\n\n----------------\n\n76 > 72\n\nanswer[5] = 1\n\n76 > 75\n\nanswer[2] = 4\n\nStack = [6]\n\n----------------\n\n73\n\nStack = [6,7]\n\nRemaining indices have no warmer day.\n\nFinal Answer\n\n[1,1,4,2,1,1,0,0]", "wordWrap": "true" }, "children": [] }, { "id": "b478a402-17f3-46cf-9d6f-25625e62bfeb", "type": "divider", "props": {}, "children": [] }, { "id": "cc597625-eda3-4e28-ae6b-7c150b47d095", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "ad9fb4ac-6a55-4778-9914-d9b04734379e", "type": "addDoc", "props": { "fileId": "157c0810-3d84-4ac4-9507-45762a191ebc", "title": "Daily Temperatures Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "f7890ca9-137b-46dc-9c1c-2ec13fd2d5ef", "type": "divider", "props": {}, "children": [] }, { "id": "9280e162-c7bc-4b89-89aa-4439538f5f77", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "0c9e8a39-1669-432c-8d6a-911c895e49ad", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "274e6983-6fbd-47fb-9b09-575cb2ac1ae6", "type": "divider", "props": {}, "children": [] }, { "id": "1968bb31-7866-4531-8f13-6bcb1d3c77e6", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "51b4478f-2e0c-45d3-a06d-b5e47ebce5bf", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A ", "styles": {} }, { "type": "text", "text": "Monotonic Stack", "styles": { "bold": true } }, { "type": "text", "text": " helps solve \"next greater element\" type problems efficiently.", "styles": {} } ], "children": [] }, { "id": "a5a4ebf0-dcfc-439f-945a-e4ab4f62c755", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead of checking every future day, maintain indices in decreasing temperature order and resolve them when a warmer temperature is found.", "styles": {} } ], "children": [] }, { "id": "050fe0a7-097e-4c16-b4a3-ecffb61d908c", "type": "divider", "props": {}, "children": [] }, { "id": "b45702c5-0f76-40bb-bed8-1f2c9503b1d8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "2cce0b81-8110-4767-b52e-87e7f0115d1a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a monotonic decreasing stack.", "styles": {} } ], "children": [] }, { "id": "3fac6d4e-4e11-4243-8d50-f88201c47435", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store indices, not temperatures.", "styles": {} } ], "children": [] }, { "id": "4991aab0-14fb-4567-9d14-1d4843a1bb72", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Pop until the current temperature is greater than the stack top.", "styles": {} } ], "children": [] }, { "id": "e83b90b9-0df9-451a-8906-abbed6b5489b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Each index is pushed and popped at most once.", "styles": {} } ], "children": [] }, { "id": "331e1044-d1df-4234-b379-46963e1e77f0", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "157c0810-3d84-4ac4-9507-45762a191ebc", "title": "Daily Temperatures Java", "folderId": "108d03fd-0258-4d5d-aabe-a74f39c9a0e9", "time": 1785347025535, "createdAt": 1785343496464, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.Stack;\nimport java.util.Arrays;\n\nclass Solution {\n\n public int[] dailyTemperatures(int[] temperatures) {\n\n int n = temperatures.length;\n int[] answer = new int[n];\n\n Stack stack = new Stack<>();\n\n for (int i = 0; i < n; i++) {\n\n while (!stack.isEmpty() &&\n temperatures[i] > temperatures[stack.peek()]) {\n\n int index = stack.pop();\n answer[index] = i - index;\n }\n\n stack.push(i);\n }\n\n return answer;\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n int[] temperatures = Input.getIntArray(\"temperatures\");\n int[] output = Input.getIntArray(\"output\");\n\n Solution sol = new Solution();\n\n int[] result = sol.dailyTemperatures(temperatures);\n\n System.out.println(\"Expected: \" + Arrays.toString(output));\n System.out.println(\"Actual : \" + Arrays.toString(result));\n }\n}", "language": "java", "output": "Expected: [1, 1, 4, 2, 1, 1, 0, 0]\nActual : [1, 1, 4, 2, 1, 1, 0, 0]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785343515614_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785343515614_0_0", "key": "temperatures", "value": "[73,74,75,71,69,72,76,73]" }, { "id": "in_1785343515614_0_1", "key": "output", "value": "[1,1,4,2,1,1,0,0]" } ] }, { "id": "tc_1785343515614_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785343515614_1_0", "key": "temperatures", "value": "[30,40,50,60]" }, { "id": "in_1785343515614_1_1", "key": "output", "value": "[1,1,1,0]" } ] }, { "id": "tc_1785343515614_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785343515614_2_0", "key": "temperatures", "value": "[30,60,90]" }, { "id": "in_1785343515614_2_1", "key": "output", "value": "[1,1,0]" } ] }, { "id": "tc_1785343515614_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785343515614_3_0", "key": "temperatures", "value": "[90,80,70,60]" }, { "id": "in_1785343515614_3_1", "key": "output", "value": "[0,0,0,0]" } ] }, { "id": "tc_1785343515614_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785343515614_4_0", "key": "temperatures", "value": "[70,70,70]" }, { "id": "in_1785343515614_4_1", "key": "output", "value": "[0,0,0]" } ] }, { "id": "tc_1785343515614_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785343515614_5_0", "key": "temperatures", "value": "[60,61,60,61]" }, { "id": "in_1785343515614_5_1", "key": "output", "value": "[1,0,1,0]" } ] }, { "id": "tc_1785343515614_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785343515614_6_0", "key": "temperatures", "value": "[65,66,67,68]" }, { "id": "in_1785343515614_6_1", "key": "output", "value": "[1,1,1,0]" } ] }, { "id": "tc_1785343515614_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785343515614_7_0", "key": "temperatures", "value": "[68,67,66,69]" }, { "id": "in_1785343515614_7_1", "key": "output", "value": "[3,2,1,0]" } ] }, { "id": "tc_1785343515614_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785343515614_8_0", "key": "temperatures", "value": "[75]" }, { "id": "in_1785343515614_8_1", "key": "output", "value": "[0]" } ] }, { "id": "tc_1785343515614_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785343515614_9_0", "key": "temperatures", "value": "[70,69,71,68,72]" }, { "id": "in_1785343515614_9_1", "key": "output", "value": "[2,1,2,1,0]" } ] }, { "id": "tc_1785343515614_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785343515614_10_0", "key": "temperatures", "value": "[]" }, { "id": "in_1785343515614_10_1", "key": "output", "value": "[]" } ] }, { "id": "tc_1785343515614_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785343515614_11_0", "key": "temperatures", "value": "[100]" }, { "id": "in_1785343515614_11_1", "key": "output", "value": "[0]" } ] }, { "id": "tc_1785343515614_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785343515614_12_0", "key": "temperatures", "value": "[50,50,51]" }, { "id": "in_1785343515614_12_1", "key": "output", "value": "[2,1,0]" } ] } ], "selectedTestCaseId": "tc_1785343515614_0" } } ] } ] } ], "files": [] }, { "_id": "8483ae06-503c-4159-ab94-e9c1766c92ba", "name": "5. Trees", "createdAt": 1785343629218, "updatedAt": 1785343629218, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "96d30f96-edee-409b-ab3c-efa5d3b8a107", "name": "19. Maximum Depth", "createdAt": 1785343666518, "updatedAt": 1785343666518, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "ad79e3f9-e67d-4abe-b4df-48cd516b446f", "title": "maximum-depth-of-binary-tree", "folderId": "96d30f96-edee-409b-ab3c-efa5d3b8a107", "time": 1785347006932, "createdAt": 1785343702057, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "23ce223a-751e-4e9c-886b-69df1b74a768", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "19. Maximum Depth of Binary Tree", "styles": {} } ], "children": [] }, { "id": "89a90659-50ac-4825-bdb5-ae3927a34533", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "11f45552-921e-4053-ab3c-3b817548e9eb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the ", "styles": {} }, { "type": "text", "text": "root", "styles": { "code": true } }, { "type": "text", "text": " of a binary tree, return its ", "styles": {} }, { "type": "text", "text": "maximum depth", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "e842cf41-83bf-40b1-b7c5-bd1ef92f7729", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.", "styles": {} } ], "children": [] }, { "id": "e3e75e4a-e5b3-45c8-b6b3-5be5aa9c5c36", "type": "divider", "props": {}, "children": [] }, { "id": "37ad5d9f-2431-44ec-9942-51b86a0ae2c2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "b3183bf1-c081-4503-8952-30bb411c02a0", "type": "column_list", "props": {}, "children": [ { "id": "d7226494-b8c6-430e-934d-3cebe57bcb18", "type": "column", "props": { "width": "1" }, "children": [ { "id": "c49cb6bc-3117-4092-aa77-bbc372dfe9c9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "74b8799e-57ee-45a7-847f-75df49821422", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "3de4f3fe-0418-4f1d-abf3-321ee07f208b", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [3,9,20,null,null,15,7]", "wordWrap": "true" }, "children": [] }, { "id": "4f43651a-3fed-45e6-9d3e-51898ee6aed2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "8ffa7051-649e-4790-b0fd-07e1c23acf89", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "3cf4a5c9-8c1c-410f-827e-f2afacfaf1f2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "0857fbd7-59e2-4490-96cf-3a8a3ca83579", "type": "codeBlock", "props": { "language": "javascript", "code": " 3\n / \\\n 9 20\n / \\\n 15 7\n\nLongest path:\n3 → 20 → 15", "wordWrap": "true" }, "children": [] }, { "id": "b81250a7-0743-4fbe-a20a-2733b766cad4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "c06d8f0c-d507-44fe-b72d-30ae14a8b393", "type": "column", "props": { "width": "1" }, "children": [ { "id": "8ff4795e-e44f-4a49-adf7-f7c3f5978c3b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "67d23c4c-b09e-40bc-b6c8-cda27d6ae96f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "d3dbedb0-eb81-4f34-8e92-69892bfcdb36", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [1,null,2]", "wordWrap": "true" }, "children": [] }, { "id": "521e1e62-944f-421e-9948-66bfb4a67840", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "aff9903f-3b16-4202-a88c-f8b4871f4f1e", "type": "codeBlock", "props": { "language": "javascript", "code": "2", "wordWrap": "true" }, "children": [] }, { "id": "8e6956f6-a57a-4502-9e30-7a9b704650f8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "4fbbb217-e0c8-40f3-85cb-08f48ecd2cc3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "3acab231-a817-457b-a575-e790a51b7859", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Recursion (Depth First Search)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d688467b-746b-4c92-a6fa-5020a0ccc451", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "ab6f1b85-603d-4712-8d64-d7650d7185f4", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the current node is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "3e58d3f3-b087-4641-a2f1-d1853c7b0454", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively find the depth of the left subtree.", "styles": {} } ], "children": [] }, { "id": "c7249da7-fff8-4013-a08f-73e3329a23a6", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively find the depth of the right subtree.", "styles": {} } ], "children": [] }, { "id": "d4a54616-7c71-495d-a032-0c68c6412318", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "1 + max(leftDepth, rightDepth)", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "14d604f3-7842-49c1-a711-e4f82af9e618", "type": "divider", "props": {}, "children": [] }, { "id": "439af966-b7ef-4cd0-9e47-709cdd2546f5", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "90b09333-8d6b-4190-a910-2c5470f16c3b", "type": "codeBlock", "props": { "language": "javascript", "code": " 3\n / \\\n 9 20\n / \\\n 15 7\n\nDepth(9) = 1\n\nDepth(15) = 1\n\nDepth(7) = 1\n\nDepth(20) = 1 + max(1,1)\n = 2\n\nDepth(3) = 1 + max(1,2)\n = 3\n\nAnswer = 3", "wordWrap": "true" }, "children": [] }, { "id": "2d9d036b-6810-4265-a322-3b5803da0a13", "type": "divider", "props": {}, "children": [] }, { "id": "b9f179a1-23e3-4461-95be-191accbf2e4b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "cf3b0699-fc62-4067-b871-0c7e87af5c9d", "type": "addDoc", "props": { "fileId": "a1f3a3dd-1729-4924-be90-4cb9b4b5b7eb", "title": "Maximum Depth Of Binary Tree Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "d13f0a49-e6a1-46b5-b28f-05c5e0644f0e", "type": "divider", "props": {}, "children": [] }, { "id": "812276f9-b0e0-4e55-9b1a-02a081046f51", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "dbaa45ff-a6a8-4558-96f8-f414f833dde3", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(h)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "351ceea0-808e-43c8-a085-2b4678c37e5d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "e5060180-64c0-4e75-a601-bdd0baa011e7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes", "styles": {} } ], "children": [] }, { "id": "4196ae34-7ce1-40e9-a71b-26031a956435", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "h", "styles": { "bold": true } }, { "type": "text", "text": " = Height of the tree (recursive call stack)", "styles": {} } ], "children": [] }, { "id": "21cb8977-42db-4eb8-a7b3-c9b34ebd8a38", "type": "divider", "props": {}, "children": [] }, { "id": "e89b782a-fb1c-4a9e-b3e9-0e4ac91a98c3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "4ff2f47a-6380-40cf-92bf-a2b34a9528f9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This is one of the most common recursion interview questions.", "styles": {} } ], "children": [] }, { "id": "d58e7f8c-ddbe-45c4-8b7d-a99a6a0ae8dd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always think:", "styles": {} } ], "children": [] }, { "id": "db470853-558b-4e45-903e-45129d98a8da", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Base Case → ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } } ], "children": [] }, { "id": "8f2ee9b2-cb97-40db-8176-0a5586829ac7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursive Case → Solve left and right subtrees", "styles": {} } ], "children": [] }, { "id": "8dd137e9-cda0-40d8-977d-0d6f4a37eecd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Combine the results using ", "styles": {} }, { "type": "text", "text": "max()", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "9a7973b6-10fa-4584-9fd3-3d759c9f898c", "type": "divider", "props": {}, "children": [] }, { "id": "17a6f26a-7aae-4903-bdbf-5c812db98dd8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "ac1a0538-2a74-4bd9-8449-27c565ca1334", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use DFS recursion.", "styles": {} } ], "children": [] }, { "id": "928ddf94-e72c-4e30-8af2-1bd43f1503c1", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Base case returns ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "2e965bd4-090d-43a1-93d5-80fd2d16c653", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively calculate left and right subtree depths.", "styles": {} } ], "children": [] }, { "id": "81e691c1-76f5-4e9a-b7ee-6a545a08f563", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "1 + max(left, right)", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "a646b1c0-bb55-4c7b-9c24-cde6cc45101e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "a1f3a3dd-1729-4924-be90-4cb9b4b5b7eb", "title": "Maximum Depth Of Binary Tree Java", "folderId": "96d30f96-edee-409b-ab3c-efa5d3b8a107", "time": 1785346520495, "createdAt": 1785344012584, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public int maxDepth(TreeNode root) {\n\n if (root == null) {\n return 0;\n }\n\n int leftDepth = maxDepth(root.left);\n int rightDepth = maxDepth(root.right);\n\n return 1 + Math.max(leftDepth, rightDepth);\n }\n\n // Helper method to build tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n Integer[] root = Input.getIntegerArray(\"root\");\n int output = Input.getInt(\"output\");\n\n TreeNode tree = buildTree(root);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.maxDepth(tree));\n }\n}", "language": "java", "output": "Expected: 3\nActual: 3\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785344111619_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785344111619_0_0", "key": "root", "value": "[3,9,20,null,null,15,7]" }, { "id": "in_1785344111619_0_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785344111619_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785344111619_1_0", "key": "root", "value": "[1,null,2]" }, { "id": "in_1785344111619_1_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785344111619_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785344111619_2_0", "key": "root", "value": "[1]" }, { "id": "in_1785344111619_2_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785344111619_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785344111619_3_0", "key": "root", "value": "[1,2,3,4,5]" }, { "id": "in_1785344111619_3_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785344111619_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785344111619_4_0", "key": "root", "value": "[1,2,null,3,null,4]" }, { "id": "in_1785344111619_4_1", "key": "output", "value": "4" } ] }, { "id": "tc_1785344111619_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785344111619_5_0", "key": "root", "value": "[1,2,3,4,null,null,5]" }, { "id": "in_1785344111619_5_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785344111619_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785344111619_6_0", "key": "root", "value": "[10,20,30,40,50,60,70]" }, { "id": "in_1785344111619_6_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785344111619_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785344111619_7_0", "key": "root", "value": "[5,null,8,null,10]" }, { "id": "in_1785344111619_7_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785344111619_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785344111619_8_0", "key": "root", "value": "[]" }, { "id": "in_1785344111619_8_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785344111620_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785344111620_9_0", "key": "root", "value": "[1,2]" }, { "id": "in_1785344111620_9_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785344111620_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785344111620_10_0", "key": "root", "value": "[]" }, { "id": "in_1785344111620_10_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785344111620_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785344111620_11_0", "key": "root", "value": "[1]" }, { "id": "in_1785344111620_11_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785344111620_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785344111620_12_0", "key": "root", "value": "[1,null,2,null,3,null,4]" }, { "id": "in_1785344111620_12_1", "key": "output", "value": "4" } ] } ], "selectedTestCaseId": "tc_1785344111619_0" } } ] } ] }, { "_id": "804dbe50-2d12-4156-b541-1fd865a59991", "name": "20. Invert Binary Tree", "createdAt": 1785346747094, "updatedAt": 1785346747094, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "844a40be-b082-4ddd-991b-bbf4b54d768e", "title": "Invert Binary Tree Doc", "folderId": "804dbe50-2d12-4156-b541-1fd865a59991", "time": 1785347235965, "createdAt": 1785346750310, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "99907972-3885-4bad-a608-51a7825f2deb", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "20. Invert Binary Tree", "styles": {} } ], "children": [] }, { "id": "f2637a34-62e1-4253-85ba-be3bafd90946", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "268cfe9f-a31f-40ae-b1d3-1a5e998c3fe9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the ", "styles": {} }, { "type": "text", "text": "root", "styles": { "code": true } }, { "type": "text", "text": " of a binary tree, invert the tree and return its root.", "styles": {} } ], "children": [] }, { "id": "27523d1c-a0c5-4af1-a911-d5f1bb56fb47", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Inverting a binary tree means swapping the left and right child of every node.", "styles": {} } ], "children": [] }, { "id": "cf5e1567-a8b2-436c-81fa-0b6a6b48efa1", "type": "divider", "props": {}, "children": [] }, { "id": "b5cfc30e-a7c8-4e5b-bc8c-752c0cab91f2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "96cdbe58-b2b3-47e2-996e-55e26b5a2125", "type": "column_list", "props": {}, "children": [ { "id": "dc6fafda-e463-45a0-9084-2905fa8dbf84", "type": "column", "props": { "width": "1" }, "children": [ { "id": "837012c5-73db-4853-90dc-0f7223ebb28b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "8b2829cc-b8d7-46db-9f05-2173141adf31", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "76b79f77-61fb-40a0-8987-ed495c246824", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [4,2,7,1,3,6,9]", "wordWrap": "true" }, "children": [] }, { "id": "81e55542-b5af-42b7-9803-e283718d268a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "f41ff4d5-0792-4308-9140-ccf1867f9800", "type": "codeBlock", "props": { "language": "javascript", "code": "[4,7,2,9,6,3,1]", "wordWrap": "true" }, "children": [] }, { "id": "873090db-67aa-4682-a61f-7fd19e43dfb1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "832c65be-a36f-4552-b82b-6ccde5d6b16c", "type": "codeBlock", "props": { "language": "javascript", "code": "Original Tree Inverted Tree\n\n 4 4\n / \\ / \\\n 2 7 7 2\n / \\ / \\ / \\ / \\\n 1 3 6 9 9 6 3 1", "wordWrap": "true" }, "children": [] }, { "id": "0e20920b-4ba6-4a73-836a-625b04ef15aa", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "d354c2f4-9757-4888-b2fa-eb5d30e75fc6", "type": "column", "props": { "width": "1" }, "children": [ { "id": "4be22b29-be67-4c31-bdfd-7d0a6635e50e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "1ca67ad8-c3b4-475a-bede-fc4bcb9130ba", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "46c73e51-b7cb-48f1-9efb-64c530cd4251", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [2,1,3]", "wordWrap": "true" }, "children": [] }, { "id": "96bdb85b-ea4d-4d5a-8e09-398cc1af3959", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "c52ebf0a-12e1-4b06-a52e-03f3dc3ff441", "type": "codeBlock", "props": { "language": "javascript", "code": "[2,3,1]", "wordWrap": "true" }, "children": [] }, { "id": "d85c7cee-6779-4863-8114-40ec38726429", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "62b99851-6fc0-4960-b4a1-8ef0a2e55a98", "type": "column", "props": { "width": "1" }, "children": [ { "id": "ffd664cd-cc98-47ea-a4a0-05e075873f99", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "17db6365-ba5d-43d6-8e78-6e943d77b9ae", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "6ffeeeb1-1aa4-4ab9-bf2b-c3c209dcd931", "type": "codeBlock", "props": { "language": "javascript", "code": "root = []", "wordWrap": "true" }, "children": [] }, { "id": "3bc880d6-3d8e-4b67-beb8-8f718e80ad04", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "ab4d5b05-fea9-40be-ae14-26f82353302f", "type": "codeBlock", "props": { "language": "javascript", "code": "[]", "wordWrap": "true" }, "children": [] }, { "id": "98e1e0bc-dab2-4236-86a4-d3b8c1c53fe2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "f095797c-e9d3-4a13-ad33-4552eb8196db", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "b39826ec-530a-4042-a77a-3002012ed236", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Recursion (Depth First Search)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "1880c87b-1aac-4df3-8495-720bdbcd9c4a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "9469f888-f046-4867-9e3e-9ee71666dfda", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the current node is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "4f2cf109-b9f0-4c1e-bca7-ff0dcf716986", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively invert the left subtree.", "styles": {} } ], "children": [] }, { "id": "80a6bd6a-c40f-4f0e-bfca-370db82f7949", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively invert the right subtree.", "styles": {} } ], "children": [] }, { "id": "e7d963a2-2eb5-4b4b-9e6c-11633ef3e9f5", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Swap the left and right children.", "styles": {} } ], "children": [] }, { "id": "829ccba8-ae45-4507-8057-a61d066087f9", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the root node.", "styles": {} } ], "children": [] }, { "id": "6fd3079c-415f-42f4-a9fc-194fb1384997", "type": "divider", "props": {}, "children": [] }, { "id": "7ae8e8c4-54b6-4e8b-a021-6f4e34bf177e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "5366472d-8d25-4e29-aa55-4ffa518ba34e", "type": "codeBlock", "props": { "language": "javascript", "code": " 4\n / \\\n 2 7\n / \\ / \\\n 1 3 6 9\n\nInvert Left Subtree\n\n 2\n / \\\n 3 1\n\nInvert Right Subtree\n\n 7\n / \\\n 9 6\n\nSwap Root Children\n\n 4\n / \\\n 7 2\n / \\ / \\\n 9 6 3 1", "wordWrap": "true" }, "children": [] }, { "id": "291271c6-24d9-449e-8f3f-f5ec5b5239e3", "type": "divider", "props": {}, "children": [] }, { "id": "12632890-e595-4757-8a98-da3276ef9b10", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "08f029a1-9dc3-463e-a677-f87847d1ee3b", "type": "addDoc", "props": { "fileId": "6ca757b1-3b3f-42ba-ac2e-d760dd77f4cd", "title": "Invert Binary tree Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "739c0d7b-e2dc-49f7-a81c-3a1db9d1f1c6", "type": "divider", "props": {}, "children": [] }, { "id": "94ac3249-6847-4d8d-a72b-01c377112a0b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "a8f8b1e7-211d-43ee-a812-1ec8ca825111", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(h)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "3abe8af5-97f3-4bd0-9d34-54763c481950", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "65e4f2b6-3f48-4199-8465-b097d9200f0a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes", "styles": {} } ], "children": [] }, { "id": "b71c3ce9-ddd8-48cb-97c6-0beab1aba18d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "h", "styles": { "bold": true } }, { "type": "text", "text": " = Height of the tree", "styles": {} } ], "children": [] }, { "id": "3a1511b2-c7d4-4474-8b2a-d6b72625ddf7", "type": "divider", "props": {}, "children": [] }, { "id": "a77096ca-b15b-43ed-a3e2-48bdbd7d4f23", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "60081fa0-e94b-467a-be80-29b27bd095a8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Think recursively.", "styles": {} } ], "children": [] }, { "id": "a5b5197b-9087-456b-ac0e-bcd056ea10a7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "For every node:", "styles": {} } ], "children": [] }, { "id": "69843029-5400-4713-99af-03f450551005", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Invert the left subtree.", "styles": {} } ], "children": [] }, { "id": "49391f58-1dd8-426c-a683-a0bc21d05071", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Invert the right subtree.", "styles": {} } ], "children": [] }, { "id": "f0077db3-38cc-4082-94f2-401094416b36", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Swap the two child pointers.", "styles": {} } ], "children": [] }, { "id": "6957937f-ec1f-4768-9613-a55539568953", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Repeat until reaching a ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": " node.", "styles": {} } ], "children": [] }, { "id": "a7eea36c-c655-4c2a-8381-4645f1c4dd71", "type": "divider", "props": {}, "children": [] }, { "id": "04f5d9b9-c78c-4e4e-bb1e-d80a701629f8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "b2b87211-898f-423a-93e0-008adc2d7d68", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use recursion (DFS).", "styles": {} } ], "children": [] }, { "id": "29ae4ff4-e562-44f5-a4d7-f520c45285b1", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Base case is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "7099a122-b8e2-45db-92b8-917dad93d6ce", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Swap left and right children.", "styles": {} } ], "children": [] }, { "id": "696c09f8-735f-4cc9-bcaf-e1529546217d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Visit every node exactly once.", "styles": {} } ], "children": [] }, { "id": "4b21ae57-73d7-4b4b-a590-727e88f820d7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "6ca757b1-3b3f-42ba-ac2e-d760dd77f4cd", "title": "Invert Binary tree Java", "folderId": "804dbe50-2d12-4156-b541-1fd865a59991", "time": 1785347219685, "createdAt": 1785346950576, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public TreeNode invertTree(TreeNode root) {\n\n if (root == null) {\n return null;\n }\n\n TreeNode left = invertTree(root.left);\n TreeNode right = invertTree(root.right);\n\n root.left = right;\n root.right = left;\n\n return root;\n }\n\n // Build binary tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n // Convert tree back to level-order list\n private static List levelOrder(TreeNode root) {\n\n List result = new ArrayList<>();\n\n if (root == null) {\n return result;\n }\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n while (!queue.isEmpty()) {\n\n TreeNode current = queue.poll();\n\n if (current == null) {\n result.add(\"null\");\n } else {\n\n result.add(String.valueOf(current.val));\n\n queue.offer(current.left);\n queue.offer(current.right);\n }\n }\n\n while (!result.isEmpty() &&\n result.get(result.size() - 1).equals(\"null\")) {\n result.remove(result.size() - 1);\n }\n\n return result;\n }\n\n // Test the code\n public static void main(String[] args) {\n\n // This is custom code to read Test case in Devscribe\n Integer[] root = Input.getIntegerArray(\"root\");\n\n TreeNode tree = buildTree(root);\n\n Solution sol = new Solution();\n\n TreeNode inverted = sol.invertTree(tree);\n\n System.out.println(levelOrder(inverted));\n }\n}", "language": "java", "output": "[4, 7, 2, 9, 6, 3, 1]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785346969659_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785346969659_0_0", "key": "root", "value": "[4,2,7,1,3,6,9]" } ] }, { "id": "tc_1785346969659_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785346969659_1_0", "key": "root", "value": "[2,1,3]" } ] }, { "id": "tc_1785346969659_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785346969659_2_0", "key": "root", "value": "[]" } ] }, { "id": "tc_1785346969659_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785346969659_3_0", "key": "root", "value": "[1]" } ] }, { "id": "tc_1785346969659_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785346969659_4_0", "key": "root", "value": "[1,2]" } ] }, { "id": "tc_1785346969659_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785346969659_5_0", "key": "root", "value": "[1,null,2]" } ] }, { "id": "tc_1785346969659_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785346969659_6_0", "key": "root", "value": "[8,4,10,2,6,9,12]" } ] }, { "id": "tc_1785346969659_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785346969659_7_0", "key": "root", "value": "[5,3,8,1,4,7,9]" } ] }, { "id": "tc_1785346969659_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785346969659_8_0", "key": "root", "value": "[10,20,null,30]" } ] }, { "id": "tc_1785346969660_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785346969660_9_0", "key": "root", "value": "[1,2,3,4,5,6,7]" } ] }, { "id": "tc_1785346969660_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785346969660_10_0", "key": "root", "value": "[]" } ] }, { "id": "tc_1785346969660_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785346969660_11_0", "key": "root", "value": "[1]" } ] }, { "id": "tc_1785346969660_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785346969660_12_0", "key": "root", "value": "[1,null,2,null,3]" } ] } ], "selectedTestCaseId": "tc_1785346969659_3" } } ] } ] }, { "_id": "db84a4c6-aae0-4beb-ac05-620b61cdd717", "name": "21. Lowest Common Ancestor", "createdAt": 1785347451782, "updatedAt": 1785347451782, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "1e20c2bd-6631-471d-bd91-bd076fd73609", "title": "Lowest Common Ancestor Doc", "folderId": "db84a4c6-aae0-4beb-ac05-620b61cdd717", "time": 1785380506717, "createdAt": 1785347455646, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "88d77faa-fec9-4386-b331-10b95c8e9965", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "21. Lowest Common Ancestor of a Binary Search Tree", "styles": {} } ], "children": [] }, { "id": "08bfbc27-24b6-42bc-8ae5-4affc23957f8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "1abf7de2-fcaf-41bf-8597-e49d3b5c32e5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the root of a ", "styles": {} }, { "type": "text", "text": "Binary Search Tree (BST)", "styles": { "bold": true } }, { "type": "text", "text": " and two nodes ", "styles": {} }, { "type": "text", "text": "p", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "q", "styles": { "code": true } }, { "type": "text", "text": ", return their ", "styles": {} }, { "type": "text", "text": "Lowest Common Ancestor (LCA)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "c933097f-a956-4ea5-9f1b-ef56141ed601", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The Lowest Common Ancestor is the lowest node in the tree that has both ", "styles": {} }, { "type": "text", "text": "p", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "q", "styles": { "code": true } }, { "type": "text", "text": " as descendants (where a node can be a descendant of itself).", "styles": {} } ], "children": [] }, { "id": "80a3da18-86c9-4c24-ba5f-bd670789659f", "type": "divider", "props": {}, "children": [] }, { "id": "5e0377ec-8735-413b-8e74-57bf687ea172", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "b5c216d3-b71b-4b41-a965-ce88aa5a5ec4", "type": "column_list", "props": {}, "children": [ { "id": "706401eb-8fac-4954-bbd1-15bd20985d21", "type": "column", "props": { "width": "1" }, "children": [ { "id": "f8699c18-1151-44bc-af19-a4089c596e1d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "89903603-8d02-4782-a41c-aed1196fb4b0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "5efde07a-b8c0-494e-9594-19754ec790c0", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [6,2,8,0,4,7,9,null,null,3,5]\np = 2\nq = 8", "wordWrap": "true" }, "children": [] }, { "id": "6f867b3e-9b93-44d6-8f23-6d978d85ed4a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "7689bb1c-f8d4-460a-9b45-6be3b664216e", "type": "codeBlock", "props": { "language": "javascript", "code": "6", "wordWrap": "true" }, "children": [] }, { "id": "128c564a-4a44-44ef-a378-ce515a288967", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "c5dc4b8c-ae24-409b-b8f9-1ae907af4ed8", "type": "column", "props": { "width": "1" }, "children": [ { "id": "5a416a03-9b7b-4fe6-bd3e-cec61c1d68f8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "f8867912-53d7-440c-aa98-df8e89864dc9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "54a053d1-52df-48ec-85bf-8802febd4799", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [6,2,8,0,4,7,9,null,null,3,5]\np = 2\nq = 4", "wordWrap": "true" }, "children": [] }, { "id": "8b2d3df0-598a-45a6-bef1-e6a4a9287232", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "0e1c0c71-3297-42f5-a193-d61c9b0e7be7", "type": "codeBlock", "props": { "language": "javascript", "code": "2", "wordWrap": "true" }, "children": [] }, { "id": "ffdda4f3-cb38-43cc-a115-0201fb11ecc9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "44b2042c-16b2-406c-9f88-ee76541621f5", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "9165b6dd-9056-4692-8b7a-901a943ea5a7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Since this is a ", "styles": {} }, { "type": "text", "text": "Binary Search Tree", "styles": { "bold": true } }, { "type": "text", "text": ", we can use its properties.", "styles": {} } ], "children": [] }, { "id": "cf468431-8cd2-4dd1-9136-4193eb2f4386", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "BST Property", "styles": {} } ], "children": [] }, { "id": "b91c402b-bc77-4d28-96f9-3d2492fdf6eb", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Left subtree values are smaller.", "styles": {} } ], "children": [] }, { "id": "1d096573-daad-4249-9ac7-79bd1e9c1d1b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Right subtree values are larger.", "styles": {} } ], "children": [] }, { "id": "0775ea30-3974-422a-a834-904453f169fd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "bb000875-a955-46a9-84ec-5befe8707705", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If both ", "styles": {} }, { "type": "text", "text": "p", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "q", "styles": { "code": true } }, { "type": "text", "text": " are smaller than the current node, move left.", "styles": {} } ], "children": [] }, { "id": "1feda62f-553d-45bc-b277-b5356a8f87ed", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If both are larger than the current node, move right.", "styles": {} } ], "children": [] }, { "id": "88d8ea83-cf33-4d97-b6a9-6386137efcfa", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise, the current node is the Lowest Common Ancestor.", "styles": {} } ], "children": [] }, { "id": "68b53fb0-3a2a-42ea-8644-ee37ecfb1b26", "type": "divider", "props": {}, "children": [] }, { "id": "e899cd18-f321-4bb2-8b2e-e1316b4f4e01", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "beba4a6f-8179-470b-893f-83c0c136810f", "type": "codeBlock", "props": { "language": "javascript", "code": " 6\n / \\\n 2 8\n / \\ / \\\n 0 4 7 9\n / \\\n 3 5\n\np = 2\nq = 8\n\n2 < 6\n8 > 6\n\nNodes split here.\n\nAnswer = 6", "wordWrap": "true" }, "children": [] }, { "id": "97b19dd4-27b7-4b6f-91da-ec738b971ffa", "type": "divider", "props": {}, "children": [] }, { "id": "bd0440d3-b3ca-4509-8f9b-6056f5d625f0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "5993f978-d02e-475b-ba8a-bdb7213838b4", "type": "addDoc", "props": { "fileId": "773e53bf-41d3-40c8-9065-ef1b60f9b70b", "title": "Lowest Common Ancestor Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "c9d41468-601b-47b2-8e47-4492d5e5ecc3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "e0b48f5b-0661-462e-b1ed-761e1c7712b8", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(h)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "b3068480-99d1-4eed-93f3-099edacbcbbc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "01c4794f-5f56-42d6-ba82-bba5e0bd3bdd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "h", "styles": { "bold": true } }, { "type": "text", "text": " = Height of the BST.", "styles": {} } ], "children": [] }, { "id": "5b6e287b-486d-441c-b6db-f42af296a22c", "type": "divider", "props": {}, "children": [] }, { "id": "d8d83c63-e7a6-4462-8272-db541113f8c4", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "d44854e7-9519-40f8-9e2a-a4aad9fd8b83", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Unlike a normal Binary Tree, you don't need to search both subtrees.", "styles": {} } ], "children": [] }, { "id": "b30476bb-812b-4b75-b2fc-36d2cc723a1e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use the BST property to move only in one direction until the split point is found.", "styles": {} } ], "children": [] }, { "id": "963b6e35-e577-4a91-8569-806e614c6fc0", "type": "divider", "props": {}, "children": [] }, { "id": "b1cacff0-0613-470a-aa25-e0757a805276", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "178f0180-f00a-460c-97c9-fcd851d20bf6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Uses BST property.", "styles": {} } ], "children": [] }, { "id": "fff99f32-a4d8-4c34-84e8-a13a0aa49481", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse only one path.", "styles": {} } ], "children": [] }, { "id": "f7d2f661-984c-4708-bc10-0dc2b14927f5", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Split point is the LCA.", "styles": {} } ], "children": [] }, { "id": "00bc972a-fa23-43ec-9e6b-023a1ad200a4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Better than Binary Tree LCA.", "styles": {} } ], "children": [] }, { "id": "c83c2a4f-95d2-4c56-904c-525d0ce43840", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(h)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "773e53bf-41d3-40c8-9065-ef1b60f9b70b", "title": "Lowest Common Ancestor Java", "folderId": "db84a4c6-aae0-4beb-ac05-620b61cdd717", "time": 1785380435013, "createdAt": 1785347573297, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {\n\n while (root != null) {\n\n if (p.val < root.val && q.val < root.val) {\n root = root.left;\n }\n else if (p.val > root.val && q.val > root.val) {\n root = root.right;\n }\n else {\n return root;\n }\n }\n\n return null;\n }\n\n // Build tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n // Find node by value\n private static TreeNode findNode(TreeNode root, int value) {\n\n while (root != null) {\n\n if (value < root.val) {\n root = root.left;\n } else if (value > root.val) {\n root = root.right;\n } else {\n return root;\n }\n }\n\n return null;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n Integer[] root = Input.getIntegerArray(\"root\");\n int p = Input.getInt(\"p\");\n int q = Input.getInt(\"q\");\n int output = Input.getInt(\"output\");\n\n TreeNode tree = buildTree(root);\n\n TreeNode nodeP = findNode(tree, p);\n TreeNode nodeQ = findNode(tree, q);\n\n Solution sol = new Solution();\n\n TreeNode ans = sol.lowestCommonAncestor(tree, nodeP, nodeQ);\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + (ans != null ? ans.val : \"null\"));\n }\n}", "language": "java", "output": "Expected: 6\nActual: 6\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785347606684_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785347606684_0_0", "key": "root", "value": "[6,2,8,0,4,7,9,null,null,3,5]" }, { "id": "in_1785347606684_0_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_0_2", "key": "q", "value": "8" }, { "id": "in_1785347606684_0_3", "key": "output", "value": "6" } ] }, { "id": "tc_1785347606684_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785347606684_1_0", "key": "root", "value": "[6,2,8,0,4,7,9,null,null,3,5]" }, { "id": "in_1785347606684_1_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_1_2", "key": "q", "value": "4" }, { "id": "in_1785347606684_1_3", "key": "output", "value": "2" } ] }, { "id": "tc_1785347606684_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785347606684_2_0", "key": "root", "value": "[2,1]" }, { "id": "in_1785347606684_2_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_2_2", "key": "q", "value": "1" }, { "id": "in_1785347606684_2_3", "key": "output", "value": "2" } ] }, { "id": "tc_1785347606684_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785347606684_3_0", "key": "root", "value": "[5,3,6,2,4,null,7]" }, { "id": "in_1785347606684_3_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_3_2", "key": "q", "value": "4" }, { "id": "in_1785347606684_3_3", "key": "output", "value": "3" } ] }, { "id": "tc_1785347606684_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785347606684_4_0", "key": "root", "value": "[5,3,6,2,4,null,7]" }, { "id": "in_1785347606684_4_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_4_2", "key": "q", "value": "7" }, { "id": "in_1785347606684_4_3", "key": "output", "value": "5" } ] }, { "id": "tc_1785347606684_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785347606684_5_0", "key": "root", "value": "[10,5,15,3,7,12,18]" }, { "id": "in_1785347606684_5_1", "key": "p", "value": "12" }, { "id": "in_1785347606684_5_2", "key": "q", "value": "18" }, { "id": "in_1785347606684_5_3", "key": "output", "value": "15" } ] }, { "id": "tc_1785347606684_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785347606684_6_0", "key": "root", "value": "[10,5,15,3,7,12,18]" }, { "id": "in_1785347606684_6_1", "key": "p", "value": "3" }, { "id": "in_1785347606684_6_2", "key": "q", "value": "7" }, { "id": "in_1785347606684_6_3", "key": "output", "value": "5" } ] }, { "id": "tc_1785347606684_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785347606684_7_0", "key": "root", "value": "[1]" }, { "id": "in_1785347606684_7_1", "key": "p", "value": "1" }, { "id": "in_1785347606684_7_2", "key": "q", "value": "1" }, { "id": "in_1785347606684_7_3", "key": "output", "value": "1" } ] }, { "id": "tc_1785347606684_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785347606684_8_0", "key": "root", "value": "[2,1]" }, { "id": "in_1785347606684_8_1", "key": "p", "value": "1" }, { "id": "in_1785347606684_8_2", "key": "q", "value": "2" }, { "id": "in_1785347606684_8_3", "key": "output", "value": "2" } ] }, { "id": "tc_1785347606684_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785347606684_9_0", "key": "root", "value": "[8,4,10,2,6,9,12]" }, { "id": "in_1785347606684_9_1", "key": "p", "value": "2" }, { "id": "in_1785347606684_9_2", "key": "q", "value": "12" }, { "id": "in_1785347606684_9_3", "key": "output", "value": "8" } ] } ], "selectedTestCaseId": "tc_1785347606684_0" } } ] } ] }, { "_id": "13cbf677-dd99-4262-a8c3-112b802aabd0", "name": "22. Diameter of Binary Tree", "createdAt": 1785379948448, "updatedAt": 1785379948448, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "79f9e4fd-2509-45ae-a931-d897d8d9dd00", "title": "Diameter of Binary Tree Doc", "folderId": "13cbf677-dd99-4262-a8c3-112b802aabd0", "time": 1785380523189, "createdAt": 1785379952305, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "8afef513-14d4-4dc3-b91a-c80ab56ec70f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "22. Diameter of Binary Tree", "styles": {} } ], "children": [] }, { "id": "a1ccb47f-6b5d-4cae-948e-32a79aef56f0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "3d15c552-6214-4de3-9a94-52769da4b606", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the ", "styles": {} }, { "type": "text", "text": "root", "styles": { "code": true } }, { "type": "text", "text": " of a binary tree, return the ", "styles": {} }, { "type": "text", "text": "length of the diameter", "styles": { "bold": true } }, { "type": "text", "text": " of the tree.", "styles": {} } ], "children": [] }, { "id": "14c9d053-ea27-4903-a788-62cde6837d61", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The diameter of a binary tree is the length of the ", "styles": {} }, { "type": "text", "text": "longest path between any two nodes", "styles": { "bold": true } }, { "type": "text", "text": " in the tree. This path may or may not pass through the root.", "styles": {} } ], "children": [] }, { "id": "3fa905dc-480d-4f70-b8a8-dd32117abf2f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The length of a path between two nodes is represented by the ", "styles": {} }, { "type": "text", "text": "number of edges", "styles": { "bold": true } }, { "type": "text", "text": " between them.", "styles": {} } ], "children": [] }, { "id": "d749005c-0220-4f5b-b94c-77af97408112", "type": "divider", "props": {}, "children": [] }, { "id": "b543ef00-1cc6-4f3f-816f-a04257069d98", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "1e63d0e1-0630-48ff-a4ca-1fbd1d5d6093", "type": "column_list", "props": {}, "children": [ { "id": "f4a8716d-d291-4504-8102-dc7060354588", "type": "column", "props": { "width": "1" }, "children": [ { "id": "8a0b1464-cfbb-43eb-8077-504984cf9ef2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "e90e2877-a8b3-46d4-9377-8aa8b4430556", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "7b32a125-1eec-4737-95ef-ea542786e81a", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [1,2,3,4,5]", "wordWrap": "true" }, "children": [] }, { "id": "5d9787db-4f61-465e-9bcb-c688a8df3d8f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "2f3f54ba-a5cd-4661-b5e7-a1be3f7c3374", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "fc7d5241-145b-4d2d-aeb3-10b05a6b654e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "fa17169b-56de-4b6d-82b3-78a79429d615", "type": "codeBlock", "props": { "language": "javascript", "code": " 1\n / \\ Longest Path: 4 → 2 → 1 → 3\n 2 3 Diameter = 3 edges\n / \\\n 4 5", "wordWrap": "true" }, "children": [] }, { "id": "9128329b-43d2-4dd0-ab97-a9c4e5c9ef5e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "8455cf45-1894-4d31-9672-f908d431ee56", "type": "column", "props": { "width": "1" }, "children": [ { "id": "12f4f703-0c33-4744-8593-b37f2f066cff", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "27c30fc1-e6a0-44a6-9988-b91271c0d689", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "80ce7ad4-04b9-4473-833b-771bab285ba6", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [1,2]", "wordWrap": "true" }, "children": [] }, { "id": "a4fb704c-3251-471f-8751-b3369caaeb84", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "07079085-cc0c-4326-bfd4-c39465f8cfb1", "type": "codeBlock", "props": { "language": "javascript", "code": "1", "wordWrap": "true" }, "children": [] }, { "id": "c6cc1f3d-7335-4209-acdc-8f95974e779b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "83dd502a-7e40-434d-9bb7-fcf8e7fb41bb", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "216fa7ac-cec7-458f-8a3d-48aa14b687ae", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Depth First Search (DFS)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "dea12072-2ff2-4023-9c54-66a1469a18c8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "For every node:", "styles": {} } ], "children": [] }, { "id": "327f51cd-a4ba-4ef5-bcbe-d6900f2a3a42", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Calculate the height of its left subtree.", "styles": {} } ], "children": [] }, { "id": "01a4d719-8709-468b-8dde-391437e3fc2d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Calculate the height of its right subtree.", "styles": {} } ], "children": [] }, { "id": "1d4d456b-ed14-478e-882c-35d745510956", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The diameter passing through this node is:", "styles": {} } ], "children": [] }, { "id": "212a4c02-8964-46ab-8ebb-7fa98d8a186d", "type": "codeBlock", "props": { "language": "javascript", "code": "leftHeight + rightHeight", "wordWrap": "true" }, "children": [] }, { "id": "38bd6b6d-e9f6-4175-9008-4e6f16da169d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the global maximum diameter while returning the height of each subtree.", "styles": {} } ], "children": [] }, { "id": "9da3b2c9-da0c-4e90-a864-f376f46f590c", "type": "divider", "props": {}, "children": [] }, { "id": "971d5957-e6fd-4141-b824-3d0ee81424a7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "2326e57c-887e-4d3e-87fe-9d10982308b4", "type": "codeBlock", "props": { "language": "javascript", "code": " 1\n / \\\n 2 3\n / \\\n 4 5\n\nHeight(4) = 1\nHeight(5) = 1\n\nDiameter at node 2 = 1 + 1 = 2\n\nHeight(2) = 2\n\nHeight(3) = 1\n\nDiameter at node 1 = 2 + 1 = 3\n\nAnswer = 3", "wordWrap": "true" }, "children": [] }, { "id": "ab87257f-e5b4-4309-aa8a-982fa8062bb7", "type": "divider", "props": {}, "children": [] }, { "id": "6df0ed18-6325-4d34-b053-2fb35a08a284", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "cf1f2539-c001-4eba-a046-51198f684dc1", "type": "addDoc", "props": { "fileId": "5b826671-644e-4404-af00-e3e34cfc25b8", "title": "Diameter of Binary Tree Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "6f34d387-21d8-4cce-b4c1-e873e66fde5b", "type": "divider", "props": {}, "children": [] }, { "id": "35e8ff5c-1303-4f26-a374-73a975af9c85", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "ff72b410-35ea-4c26-bee3-77b803b56292", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(h)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "80f754bf-9855-4f0d-9d34-90760b8ac3f4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "a98e162c-f293-4e5e-92cb-a8be9ce4b5b6", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes", "styles": {} } ], "children": [] }, { "id": "70448ff1-64e3-4629-9867-1350e24008f3", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "h", "styles": { "bold": true } }, { "type": "text", "text": " = Height of the tree", "styles": {} } ], "children": [] }, { "id": "9135bcaa-86e9-45ab-9bcd-e932eb702217", "type": "divider", "props": {}, "children": [] }, { "id": "bcfaccc2-b4f5-4568-ac63-43f23f631bc0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "5bc185e3-40cb-4129-9c4e-e8925ff17a5a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Don't calculate the height separately for every node.", "styles": {} } ], "children": [] }, { "id": "ddf54a7f-f6cd-4903-b66f-ed3aefe46b34", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Instead, compute the height while simultaneously updating the maximum diameter.", "styles": {} } ], "children": [] }, { "id": "5d87d3c1-26d7-4ace-aaa3-a0503149fad5", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "This avoids an ", "styles": {} }, { "type": "text", "text": "O(n²)", "styles": { "bold": true } }, { "type": "text", "text": " solution and gives an ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " solution.", "styles": {} } ], "children": [] }, { "id": "7d1f511a-8146-43b0-849f-7d57f2784c8e", "type": "divider", "props": {}, "children": [] }, { "id": "2c5a269f-527a-4388-b8ff-7cbf9d1ed7c0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "bfce36e2-ef62-49f5-ade9-6be5377df61a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use DFS recursion.", "styles": {} } ], "children": [] }, { "id": "b546b884-5dc0-45c1-a896-f8a4365257ef", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Height is returned to the parent.", "styles": {} } ], "children": [] }, { "id": "16986409-ec07-4617-aeca-595d28f43515", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Diameter is updated at every node.", "styles": {} } ], "children": [] }, { "id": "f4062e16-2b2b-4393-a022-35636c32c9fb", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Diameter is measured in ", "styles": {} }, { "type": "text", "text": "edges", "styles": { "bold": true } }, { "type": "text", "text": ", not nodes.", "styles": {} } ], "children": [] }, { "id": "d1a5e054-c614-4c02-b011-ac60e7b77b15", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "5b826671-644e-4404-af00-e3e34cfc25b8", "title": "Diameter of Binary Tree Java", "folderId": "13cbf677-dd99-4262-a8c3-112b802aabd0", "time": 1785380386108, "createdAt": 1785380142752, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n private int diameter = 0;\n\n public int diameterOfBinaryTree(TreeNode root) {\n\n height(root);\n return diameter;\n }\n\n private int height(TreeNode node) {\n\n if (node == null) {\n return 0;\n }\n\n int leftHeight = height(node.left);\n int rightHeight = height(node.right);\n\n diameter = Math.max(diameter, leftHeight + rightHeight);\n\n return 1 + Math.max(leftHeight, rightHeight);\n }\n\n // Build tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n Integer[] root = Input.getIntegerArray(\"root\");\n int output = Input.getInt(\"output\");\n\n TreeNode tree = buildTree(root);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.diameterOfBinaryTree(tree));\n }\n}", "language": "java", "output": "Expected: 3\nActual: 3\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785380162186_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785380162186_0_0", "key": "root", "value": "[1,2,3,4,5]" }, { "id": "in_1785380162186_0_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785380162186_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785380162186_1_0", "key": "root", "value": "[1,2]" }, { "id": "in_1785380162186_1_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785380162186_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785380162186_2_0", "key": "root", "value": "[1]" }, { "id": "in_1785380162186_2_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785380162186_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785380162186_3_0", "key": "root", "value": "[1,2,3,4,5,6,7]" }, { "id": "in_1785380162186_3_1", "key": "output", "value": "4" } ] }, { "id": "tc_1785380162186_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785380162186_4_0", "key": "root", "value": "[1,2,null,3,null,4]" }, { "id": "in_1785380162186_4_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785380162186_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785380162186_5_0", "key": "root", "value": "[1,null,2,null,3,null,4]" }, { "id": "in_1785380162186_5_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785380162186_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785380162186_6_0", "key": "root", "value": "[4,2,6,1,3,5,7]" }, { "id": "in_1785380162186_6_1", "key": "output", "value": "4" } ] }, { "id": "tc_1785380162186_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785380162186_7_0", "key": "root", "value": "[10,20,30,40,null,null,50]" }, { "id": "in_1785380162186_7_1", "key": "output", "value": "4" } ] }, { "id": "tc_1785380162186_8", "name": "Test Case 9", "inputs": [ { "id": "in_1785380162186_8_0", "key": "root", "value": "[]" }, { "id": "in_1785380162186_8_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785380162186_9", "name": "Test Case 10", "inputs": [ { "id": "in_1785380162186_9_0", "key": "root", "value": "[1,2,3,null,4]" }, { "id": "in_1785380162186_9_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785380162186_10", "name": "Edge Case 1", "inputs": [ { "id": "in_1785380162186_10_0", "key": "root", "value": "[]" }, { "id": "in_1785380162186_10_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785380162186_11", "name": "Edge Case 2", "inputs": [ { "id": "in_1785380162186_11_0", "key": "root", "value": "[1]" }, { "id": "in_1785380162186_11_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785380162186_12", "name": "Edge Case 3", "inputs": [ { "id": "in_1785380162186_12_0", "key": "root", "value": "[1,2,null,3,null,4,null,5]" }, { "id": "in_1785380162186_12_1", "key": "output", "value": "4" } ] } ], "selectedTestCaseId": "tc_1785380162186_0" } } ] } ] }, { "_id": "8e02a985-2538-4077-be65-c838ebc03851", "name": "23. Same Tree", "createdAt": 1785380681492, "updatedAt": 1785380681492, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "a3445f8c-ffae-4df9-827f-d31a3bfcdf41", "title": "Same Tree Doc", "folderId": "8e02a985-2538-4077-be65-c838ebc03851", "time": 1785380822759, "createdAt": 1785380684233, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "ea3a7118-d31f-4b0f-8153-1de93c480032", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "23. Same Tree", "styles": {} } ], "children": [] }, { "id": "c1c072ed-3a6d-4e41-8e68-0858a3b5ddf6", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "d50b8991-8790-46ae-b7be-e399a11e1852", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given the roots of two binary trees ", "styles": {} }, { "type": "text", "text": "p", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "q", "styles": { "code": true } }, { "type": "text", "text": ", write a function to check if they are the same.", "styles": {} } ], "children": [] }, { "id": "6489cfc0-20ed-49f2-9d38-ad05b354cc44", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Two binary trees are considered the same if they are ", "styles": {} }, { "type": "text", "text": "structurally identical", "styles": { "bold": true } }, { "type": "text", "text": ", and the nodes have the ", "styles": {} }, { "type": "text", "text": "same value", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "6d7c5ddc-6d14-44c2-a33d-a51da3cbb51b", "type": "divider", "props": {}, "children": [] }, { "id": "38ff82c2-145e-44ba-9337-155b37f5db8d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "30c19d34-ddfe-4a92-adbd-e018e052f42b", "type": "column_list", "props": {}, "children": [ { "id": "39375945-cc59-49c3-9e3e-8ca84cb0d86e", "type": "column", "props": { "width": "1" }, "children": [ { "id": "8e46d224-e514-4b15-a243-7b2dfecd0760", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "37b2b6dc-9cc1-4475-b2f3-498f4657316a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "03dc9d6c-e2f5-49e3-bd66-e4b4191ffe18", "type": "codeBlock", "props": { "language": "javascript", "code": "p = [1,2,3]\nq = [1,2,3]", "wordWrap": "true" }, "children": [] }, { "id": "481477a6-0030-4c10-8919-2218189db5bc", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "d26f4a2e-0f55-4110-b866-77534f0d7c66", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "023b29c2-79e7-4eca-ae37-1df430f70ad8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "34633e31-7ec1-4c9e-a1cd-db0986c2b9b1", "type": "column", "props": { "width": "1" }, "children": [ { "id": "7d1d1acf-b11f-4451-a11e-a0b5cc8c7c34", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "17e0bbb4-8ee7-4014-b968-4f3dd76b207c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "95c80daa-b950-4355-ba3a-cde62d0ca109", "type": "codeBlock", "props": { "language": "javascript", "code": "p = [1,2]\nq = [1,null,2]", "wordWrap": "true" }, "children": [] }, { "id": "2cc8eef1-7f78-4ace-a480-bd5d6b1d7049", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "1a10aa34-ee92-4ddf-9611-63c8d5c86217", "type": "codeBlock", "props": { "language": "javascript", "code": "false", "wordWrap": "true" }, "children": [] }, { "id": "534b3889-4d8f-4af9-afca-3138cf681a4c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "c117824c-2e5e-4657-8a48-a6dabe7b9737", "type": "column", "props": { "width": "1" }, "children": [ { "id": "8b778bbf-8dd2-4e7c-8af3-1b6e28df0258", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "b1196620-badd-4a45-9e2a-dc125e2e07ed", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "e64a6d46-f3e2-43e6-bc13-ea58a890200b", "type": "codeBlock", "props": { "language": "javascript", "code": "p = [1,2,1]\nq = [1,1,2]", "wordWrap": "true" }, "children": [] }, { "id": "ad8d4cf8-556d-466f-8ed7-9fe0f4ce7c44", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "4afa344c-3bb6-469e-b4c6-490597550b13", "type": "codeBlock", "props": { "language": "javascript", "code": "false", "wordWrap": "true" }, "children": [] }, { "id": "d4322aa7-033d-447e-b626-3ae9cdf0c2fd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "6c4f49bd-2a36-4b1d-8e9d-7897c9404fc9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "5102ca41-c37e-4e7c-ade3-a1bcda21d61c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Depth First Search (DFS)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "a1d39297-338e-4598-8544-4eee2303ea85", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "e4374f17-9971-49b5-81b2-85edb23ff444", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If both nodes are ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "9287dff5-fb00-4b19-928e-7bd169cc307f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If one node is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "283be7bc-e4ac-467b-84d8-3c8133ff4f1d", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If node values are different, return ", "styles": {} }, { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "545fc7db-6825-4a0a-86d2-df36ed93a45a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively compare:", "styles": {} } ], "children": [ { "id": "aa5945ec-2f9e-4a06-8afc-cba3c25495d7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Left subtrees", "styles": {} } ], "children": [] }, { "id": "7c78643e-4608-4334-8a7f-5e7c112214d8", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Right subtrees", "styles": {} } ], "children": [] } ] }, { "id": "8808b0c8-f22c-45f8-877c-ecf443706c8d", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return ", "styles": {} }, { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " only if both subtrees are identical.", "styles": {} } ], "children": [] }, { "id": "bc832c67-8943-4cc8-ad80-ecae15be7005", "type": "divider", "props": {}, "children": [] }, { "id": "8a5515de-3f9b-4e94-ba52-6717f86457a9", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "8b448f4d-3e49-4027-b50d-a78e93568d0e", "type": "codeBlock", "props": { "language": "javascript", "code": " p q\n\n 1 1\n / \\ / \\\n 2 3 2 3\n\n1 == 1 ✔\n\nCompare Left\n2 == 2 ✔\n\nCompare Right\n3 == 3 ✔\n\nBoth subtrees are identical.\n\nAnswer = true", "wordWrap": "true" }, "children": [] }, { "id": "4db94518-6e1e-416f-9cde-cabf63e8198d", "type": "divider", "props": {}, "children": [] }, { "id": "f9bff84c-fe03-4d71-b38c-c3d8e03d4e89", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "c84ff25a-7b9e-47ae-bef7-9a3cd730438a", "type": "addDoc", "props": { "fileId": "5305b498-037c-4156-afa5-4b56fa563572", "title": "Same Tree Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "fc31d457-ba72-4405-a101-ed625ec6820b", "type": "divider", "props": {}, "children": [] }, { "id": "38b56224-c20f-4218-b663-62f411270152", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "e1ea4124-258c-4ccd-b5a4-c8ac85d185aa", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(h)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "0a18b144-9ea3-4b28-9a39-df08730ae17f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "533d345f-5763-46aa-81ac-4bdca356f55e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes", "styles": {} } ], "children": [] }, { "id": "e8726e65-595b-4d0e-bc9b-c7f4c09c758b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "h", "styles": { "bold": true } }, { "type": "text", "text": " = Height of the tree", "styles": {} } ], "children": [] }, { "id": "fb9fcdc5-e58e-4c79-b32f-cce2e5e02ad6", "type": "divider", "props": {}, "children": [] }, { "id": "5d32b6ee-885a-43c1-812c-ba3fc0dd4c43", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "2649255b-9a5b-4dd1-b8c0-e0429047b7c8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always check the base cases first:", "styles": {} } ], "children": [] }, { "id": "619f35a2-8994-4165-8560-4bca480aa9cf", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Both nodes are ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } } ], "children": [] }, { "id": "e9a71807-7c6c-48cb-a01d-499d91b9f019", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "One node is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } } ], "children": [] }, { "id": "edfe06f8-72b8-40a2-ae54-d01c08405c08", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Values are different", "styles": {} } ], "children": [] }, { "id": "20ef39f0-1168-441a-be35-5e57722bb253", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Then recursively compare both left and right subtrees.", "styles": {} } ], "children": [] }, { "id": "0adc8f39-4318-4362-87cf-161b2ab689a5", "type": "divider", "props": {}, "children": [] }, { "id": "b8cc5a9f-ffaa-4e76-8367-b9ac9559e6a3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "2300143b-f464-4e1d-a550-b20b95154c08", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use DFS recursion.", "styles": {} } ], "children": [] }, { "id": "a62818ea-a89d-4511-9665-5c51cb8992ba", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare both structure and node values.", "styles": {} } ], "children": [] }, { "id": "24de8741-0843-4407-b3b6-86bec5355c70", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Both left and right subtrees must match.", "styles": {} } ], "children": [] }, { "id": "04c1801f-ad21-4e2d-9715-b97925cb81a9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "5305b498-037c-4156-afa5-4b56fa563572", "title": "Same Tree Java", "folderId": "8e02a985-2538-4077-be65-c838ebc03851", "time": 1785380835771, "createdAt": 1785380689166, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Solution {\n\n public boolean isSameTree(TreeNode p, TreeNode q) {\n\n if (p == null && q == null) {\n return true;\n }\n\n if (p == null || q == null) {\n return false;\n }\n\n if (p.val != q.val) {\n return false;\n }\n\n return isSameTree(p.left, q.left) &&\n isSameTree(p.right, q.right);\n }\n\n // Build tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n Integer[] p = Input.getIntegerArray(\"p\");\n Integer[] q = Input.getIntegerArray(\"q\");\n boolean output = Input.getBool(\"output\");\n\n TreeNode tree1 = buildTree(p);\n TreeNode tree2 = buildTree(q);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.isSameTree(tree1, tree2));\n }\n}", "language": "java", "output": "Expected: true\nActual: true\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785380808052_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785380808052_0_0", "key": "p", "value": "[1,2,3]" }, { "id": "in_1785380808052_0_1", "key": "q", "value": "[1,2,3]" }, { "id": "in_1785380808052_0_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785380808052_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785380808052_1_0", "key": "p", "value": "[1,2]" }, { "id": "in_1785380808052_1_1", "key": "q", "value": "[1,null,2]" }, { "id": "in_1785380808052_1_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808052_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785380808052_2_0", "key": "p", "value": "[1,2,1]" }, { "id": "in_1785380808052_2_1", "key": "q", "value": "[1,1,2]" }, { "id": "in_1785380808052_2_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808052_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785380808052_3_0", "key": "p", "value": "[1]" }, { "id": "in_1785380808052_3_1", "key": "q", "value": "[1]" }, { "id": "in_1785380808052_3_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785380808052_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785380808052_4_0", "key": "p", "value": "[]" }, { "id": "in_1785380808052_4_1", "key": "q", "value": "[]" }, { "id": "in_1785380808052_4_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785380808052_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785380808052_5_0", "key": "p", "value": "[1,2,3]" }, { "id": "in_1785380808052_5_1", "key": "q", "value": "[1,2,4]" }, { "id": "in_1785380808052_5_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808052_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785380808052_6_0", "key": "p", "value": "[5,3,7,2,4,6,8]" }, { "id": "in_1785380808052_6_1", "key": "q", "value": "[5,3,7,2,4,6,8]" }, { "id": "in_1785380808052_6_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785380808053_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785380808052_7_0", "key": "p", "value": "[5,3,7,2,4,6,8]" }, { "id": "in_1785380808052_7_1", "key": "q", "value": "[5,3,7,2,4,6,9]" }, { "id": "in_1785380808052_7_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808053_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785380808053_8_0", "key": "p", "value": "[]" }, { "id": "in_1785380808053_8_1", "key": "q", "value": "[1]" }, { "id": "in_1785380808053_8_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808053_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785380808053_9_0", "key": "p", "value": "[1]" }, { "id": "in_1785380808053_9_1", "key": "q", "value": "[]" }, { "id": "in_1785380808053_9_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785380808053_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785380808053_10_0", "key": "p", "value": "[1,null,2,null,3]" }, { "id": "in_1785380808053_10_1", "key": "q", "value": "[1,null,2,null,3]" }, { "id": "in_1785380808053_10_2", "key": "output", "value": "true" } ] } ], "selectedTestCaseId": "tc_1785380808052_0" } } ] } ] }, { "_id": "13ab42ad-9c7c-4932-8a3f-6ef3a66d7845", "name": "24. Serialize and Deserialize", "createdAt": 1785380890637, "updatedAt": 1785380890637, "parentId": "8483ae06-503c-4159-ab94-e9c1766c92ba", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "9c426d41-bfd9-404f-a679-7204ffe4115a", "title": "Serialize and Deserialize Doc", "folderId": "13ab42ad-9c7c-4932-8a3f-6ef3a66d7845", "time": 1785381202905, "createdAt": 1785381164698, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "cce09682-b0f4-4b1b-bc46-b970585f3a7b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "24. Serialize and Deserialize Binary Tree", "styles": {} } ], "children": [] }, { "id": "8fce349e-227f-48cc-ada9-a23f5647b6e5", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "b4d5ba1b-0cd7-4cd7-8669-73f639100f0b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Design an algorithm to serialize and deserialize a binary tree.", "styles": {} } ], "children": [] }, { "id": "49efccf5-251f-4591-939b-363c572703dd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Serialize", "styles": { "bold": true } }, { "type": "text", "text": " converts a binary tree into a string.", "styles": {} } ], "children": [] }, { "id": "78089ddc-97da-454e-b828-b3b86e53efa9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Deserialize", "styles": { "bold": true } }, { "type": "text", "text": " reconstructs the original binary tree from the serialized string.", "styles": {} } ], "children": [] }, { "id": "914cc50a-5e8a-4554-8567-529eeeca0c00", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "There is no restriction on how the serialization should work, as long as deserialization recreates the exact same tree.", "styles": {} } ], "children": [] }, { "id": "64c4b627-9623-4cfa-9451-48bbd43d5662", "type": "divider", "props": {}, "children": [] }, { "id": "59264be4-49d2-4be8-a0b0-22f76fad0714", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "0aef7177-4213-4cf1-bde9-b7fd40843ba2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "497d2536-10c1-4148-9fb2-302722b10646", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "9139992c-562d-415e-aed1-b1094684e8c7", "type": "codeBlock", "props": { "language": "javascript", "code": "root = [1,2,3,null,null,4,5]", "wordWrap": "true" }, "children": [] }, { "id": "6842fac3-cec4-4f97-b018-02249f18e451", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Serialized", "styles": { "bold": true } } ], "children": [] }, { "id": "0ce86b36-4029-41f5-a927-f5a76c517771", "type": "codeBlock", "props": { "language": "javascript", "code": "1,2,3,null,null,4,5,null,null,null,null", "wordWrap": "true" }, "children": [] }, { "id": "857ff23e-b18d-4ad6-a2e6-714fa79a48f3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Deserialized", "styles": { "bold": true } } ], "children": [] }, { "id": "80ad7ebb-8a53-40c5-9d68-4baa3b0206d7", "type": "codeBlock", "props": { "language": "javascript", "code": " 1\n / \\\n 2 3\n / \\\n 4 5", "wordWrap": "true" }, "children": [] }, { "id": "b408a261-f028-4ca5-a124-6bdbd65377bd", "type": "divider", "props": {}, "children": [] }, { "id": "82ec358a-affd-4471-89b3-ed1ec5922488", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "75a28a26-e088-4907-9e76-6484b85e4d2f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Level Order Traversal (Breadth First Search)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "9e30c103-f591-4351-b88c-aab9b40d92b8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Serialization", "styles": {} } ], "children": [] }, { "id": "5d3e2519-10de-4f9d-b779-f513b4f9ab34", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse the tree using a queue.", "styles": {} } ], "children": [] }, { "id": "460fd58c-b3ea-43b4-964e-464380702681", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store every node value.", "styles": {} } ], "children": [] }, { "id": "65494dc9-4ad4-4ac3-8311-5d6b9768ef5d", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store ", "styles": {} }, { "type": "text", "text": "\"null\"", "styles": { "code": true } }, { "type": "text", "text": " for missing children.", "styles": {} } ], "children": [] }, { "id": "e50cd4d9-9be2-401c-bbfc-f81d08397f3c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Join all values using commas.", "styles": {} } ], "children": [] }, { "id": "febe95a5-a970-4ce2-ac3a-ffd09b41831a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Deserialization", "styles": {} } ], "children": [] }, { "id": "3da633e0-bef7-47c8-b563-8a3ce77538a3", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Split the serialized string.", "styles": {} } ], "children": [] }, { "id": "0669a5b0-9b12-4dcf-8bc4-8d090bf64109", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create the root node.", "styles": {} } ], "children": [] }, { "id": "2f5d56c3-59b9-465f-8a32-295a44ed4a71", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use a queue.", "styles": {} } ], "children": [] }, { "id": "182f87ea-8eef-42c0-98b4-e852e854d949", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Assign left and right children one by one.", "styles": {} } ], "children": [] }, { "id": "85c3cdcf-00d4-4fcc-a5b3-73346a9f479a", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the reconstructed tree.", "styles": {} } ], "children": [] }, { "id": "2685b832-4920-4885-9db1-5784d3ad2836", "type": "divider", "props": {}, "children": [] }, { "id": "c87c7ded-5925-4c6e-82b3-586bfc55607a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "1d20046d-bbe3-4946-bc38-cff47543f080", "type": "codeBlock", "props": { "language": "javascript", "code": "Tree\n\n 1\n / \\\n 2 3\n / \\\n 4 5\n\nQueue\n\n1\n\nOutput\n1\n\nQueue\n\n2,3\n\nOutput\n1,2,3\n\nQueue\n\nnull,null,4,5\n\nOutput\n1,2,3,null,null,4,5\n\nContinue until queue becomes empty.", "wordWrap": "true" }, "children": [] }, { "id": "aa42159c-b9b1-4c78-8053-b46ec97e40f3", "type": "divider", "props": {}, "children": [] }, { "id": "ea8acee1-e10f-46a5-a488-dab7f1db92dc", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "6d88becf-d3b9-403e-8249-30da385651c4", "type": "quote", "props": { "backgroundColor": "default", "textColor": "default" }, "content": [ { "type": "text", "text": "Embed the Java code file here.", "styles": {} } ], "children": [] }, { "id": "c21c5c3c-3742-4072-be88-ac9ad1754608", "type": "divider", "props": {}, "children": [] }, { "id": "70c42246-5df8-44a2-acb6-034ac70a415e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "463a7338-c09a-41bb-8b21-39a82cbf87ac", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "59d2a7e6-755c-45e0-85c0-25e510c7e465", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "c9349547-7a25-4e7b-b585-64dc9e8be41c", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of nodes", "styles": {} } ], "children": [] }, { "id": "291cf122-7c79-4d94-ad19-684e841e6c1e", "type": "divider", "props": {}, "children": [] }, { "id": "3e5c7da8-9ea1-48ff-8ddb-e56392a8fcfa", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "97b9ba25-bd95-48eb-a53b-618947c65247", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always serialize ", "styles": {} }, { "type": "text", "text": "null", "styles": { "bold": true } }, { "type": "text", "text": " nodes.", "styles": {} } ], "children": [] }, { "id": "2d18eb63-2b27-49ca-8026-ba1bfc55129b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise, the exact tree structure cannot be reconstructed during deserialization.", "styles": {} } ], "children": [] }, { "id": "d70414a4-3340-4afd-8979-812dc99dd5ae", "type": "divider", "props": {}, "children": [] }, { "id": "00bd6943-294b-4f05-8ee0-8f57a6ab3f4e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "ca71121c-0de8-4055-919a-9850a9fae9ef", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use BFS.", "styles": {} } ], "children": [] }, { "id": "583cccc9-99dd-4a2a-b133-1fbaf75a9656", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store null nodes.", "styles": {} } ], "children": [] }, { "id": "8d1038e0-786c-407b-841c-1488ae87014d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Queue is used in both serialization and deserialization.", "styles": {} } ], "children": [] }, { "id": "ef9a09f1-bc7e-4cab-919f-4fef1e36e409", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "03e8bf46-98de-4586-91ea-391f30daa3ef", "title": "Serialize and Deserialize Java", "folderId": "13ab42ad-9c7c-4932-8a3f-6ef3a66d7845", "time": 1785381240160, "createdAt": 1785381168875, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass TreeNode {\n\n int val;\n TreeNode left;\n TreeNode right;\n\n TreeNode(int val) {\n this.val = val;\n }\n}\n\nclass Codec {\n\n // Serialize Tree\n public String serialize(TreeNode root) {\n\n if (root == null) {\n return \"\";\n }\n\n StringBuilder sb = new StringBuilder();\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n while (!queue.isEmpty()) {\n\n TreeNode node = queue.poll();\n\n if (node == null) {\n sb.append(\"null,\");\n continue;\n }\n\n sb.append(node.val).append(\",\");\n\n queue.offer(node.left);\n queue.offer(node.right);\n }\n\n sb.setLength(sb.length() - 1);\n\n return sb.toString();\n }\n\n // Deserialize Tree\n public TreeNode deserialize(String data) {\n\n if (data == null || data.isEmpty()) {\n return null;\n }\n\n String[] values = data.split(\",\");\n\n TreeNode root = new TreeNode(Integer.parseInt(values[0]));\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (!values[index].equals(\"null\")) {\n current.left = new TreeNode(Integer.parseInt(values[index]));\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && !values[index].equals(\"null\")) {\n current.right = new TreeNode(Integer.parseInt(values[index]));\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n\n // Convert tree to level-order string\n private static List levelOrder(TreeNode root) {\n\n List result = new ArrayList<>();\n\n if (root == null) {\n return result;\n }\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n while (!queue.isEmpty()) {\n\n TreeNode node = queue.poll();\n\n if (node == null) {\n result.add(\"null\");\n } else {\n\n result.add(String.valueOf(node.val));\n\n queue.offer(node.left);\n queue.offer(node.right);\n }\n }\n\n while (!result.isEmpty() &&\n result.get(result.size() - 1).equals(\"null\")) {\n result.remove(result.size() - 1);\n }\n\n return result;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n Integer[] root = Input.getIntegerArray(\"root\");\n\n TreeNode tree = buildTree(root);\n\n Codec codec = new Codec();\n\n String serialized = codec.serialize(tree);\n\n TreeNode deserialized = codec.deserialize(serialized);\n\n System.out.println(\"Serialized:\");\n System.out.println(serialized);\n\n System.out.println();\n\n System.out.println(\"Deserialized:\");\n System.out.println(levelOrder(deserialized));\n }\n\n // Build tree from level-order array\n private static TreeNode buildTree(Integer[] values) {\n\n if (values.length == 0 || values[0] == null) {\n return null;\n }\n\n TreeNode root = new TreeNode(values[0]);\n\n Queue queue = new LinkedList<>();\n queue.offer(root);\n\n int index = 1;\n\n while (!queue.isEmpty() && index < values.length) {\n\n TreeNode current = queue.poll();\n\n if (index < values.length && values[index] != null) {\n current.left = new TreeNode(values[index]);\n queue.offer(current.left);\n }\n index++;\n\n if (index < values.length && values[index] != null) {\n current.right = new TreeNode(values[index]);\n queue.offer(current.right);\n }\n index++;\n }\n\n return root;\n }\n}", "language": "java", "output": "Serialized:\n1,2,3,null,null,4,5,null,null,null,null\n\nDeserialized:\n[1, 2, 3, null, null, 4, 5]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785381235959_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785381235959_0_0", "key": "root", "value": "[1,2,3,null,null,4,5]" } ] }, { "id": "tc_1785381235959_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785381235959_1_0", "key": "root", "value": "[1]" } ] }, { "id": "tc_1785381235959_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785381235959_2_0", "key": "root", "value": "[]" } ] }, { "id": "tc_1785381235959_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785381235959_3_0", "key": "root", "value": "[4,2,7,1,3,6,9]" } ] }, { "id": "tc_1785381235959_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785381235959_4_0", "key": "root", "value": "[1,2,null,3]" } ] }, { "id": "tc_1785381235959_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785381235959_5_0", "key": "root", "value": "[5,3,8,1,4,7,9]" } ] }, { "id": "tc_1785381235959_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785381235959_6_0", "key": "root", "value": "[10,20,30,40,null,null,50]" } ] }, { "id": "tc_1785381235959_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785381235959_7_0", "key": "root", "value": "[8]" } ] }, { "id": "tc_1785381235959_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785381235959_8_0", "key": "root", "value": "[]" } ] }, { "id": "tc_1785381235959_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785381235959_9_0", "key": "root", "value": "[1]" } ] }, { "id": "tc_1785381235959_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785381235959_10_0", "key": "root", "value": "[1,null,2,null,3,null,4]" } ] } ], "selectedTestCaseId": "tc_1785381235959_0" } } ] } ] } ], "files": [] }, { "_id": "f302655b-f36a-437a-b15d-5ec7687aa3f5", "name": "6. Graphs", "createdAt": 1785381277548, "updatedAt": 1785381289843, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "2c9f3add-d21c-47c0-bbf1-adb8dc5b1a91", "name": "25. Number of Islands", "createdAt": 1785381463188, "updatedAt": 1785381463188, "parentId": "f302655b-f36a-437a-b15d-5ec7687aa3f5", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "8cc76908-eec8-4a15-9cf5-c8575a87ff96", "title": "Number of Islands Doc", "folderId": "2c9f3add-d21c-47c0-bbf1-adb8dc5b1a91", "time": 1785381684913, "createdAt": 1785381468672, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "c3ecb075-1ad2-40dc-8a8c-60b37589fef7", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "25. Number of Islands", "styles": {} } ], "children": [] }, { "id": "061ffb91-d84b-4215-ab8f-25807efd430b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "adaec603-3b06-43a4-9b18-befb34d60db9", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an ", "styles": {} }, { "type": "text", "text": "m x n", "styles": { "code": true } }, { "type": "text", "text": " 2D binary grid where:", "styles": {} } ], "children": [] }, { "id": "1fcef6b4-638e-49c0-a336-9bcc627aa6cc", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "'1'", "styles": { "code": true } }, { "type": "text", "text": " represents land.", "styles": {} } ], "children": [] }, { "id": "c32b69a1-5ed4-4ea0-bd9c-5292f832738a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "'0'", "styles": { "code": true } }, { "type": "text", "text": " represents water.", "styles": {} } ], "children": [] }, { "id": "834ca677-ba6e-4971-9ca3-b4d40a63b3af", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the number of islands.", "styles": {} } ], "children": [] }, { "id": "91898c3e-1f5a-45a1-87af-c01a57e8404a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are surrounded by water.", "styles": {} } ], "children": [] }, { "id": "6bfe1f72-e679-4b33-a313-422cb944ae97", "type": "divider", "props": {}, "children": [] }, { "id": "4e134f36-0ead-4a8a-a5d4-963ba0697090", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "937023b0-30ea-48ba-b9e3-ac4a539878da", "type": "column_list", "props": {}, "children": [ { "id": "a0bdbf4a-a325-47b5-bf3b-7e460415cacf", "type": "column", "props": { "width": "1" }, "children": [ { "id": "4b75acaf-5b92-4803-8713-4571a9b5333b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "42b95bee-80ee-4bc4-9622-cf4491cf1431", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "59e5e3b1-3036-46a1-b1f0-ff6424894713", "type": "codeBlock", "props": { "language": "javascript", "code": "grid =\n[\n [\"1\",\"1\",\"1\",\"1\",\"0\"],\n [\"1\",\"1\",\"0\",\"1\",\"0\"],\n [\"1\",\"1\",\"0\",\"0\",\"0\"],\n [\"0\",\"0\",\"0\",\"0\",\"0\"]\n]", "wordWrap": "true" }, "children": [] }, { "id": "cdbfaaa3-324b-445d-aef8-18a25b661224", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "f20fc105-2592-498c-844c-77d592862cc1", "type": "codeBlock", "props": { "language": "javascript", "code": "1", "wordWrap": "true" }, "children": [] }, { "id": "d93f2b63-277e-4781-a0ee-5ffa606d7fb8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "ebdbf7be-8e23-482b-9887-ff8afad8ee6c", "type": "column", "props": { "width": "1" }, "children": [ { "id": "6b0015e8-f8f5-4679-a28e-5adf4a79f48c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "f9d32ade-f163-4847-aaef-5fbf712ec87a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "27ff0c00-6d10-4fde-a86f-dc56ed0b3587", "type": "codeBlock", "props": { "language": "javascript", "code": "grid =\n[\n [\"1\",\"1\",\"0\",\"0\",\"0\"],\n [\"1\",\"1\",\"0\",\"0\",\"0\"],\n [\"0\",\"0\",\"1\",\"0\",\"0\"],\n [\"0\",\"0\",\"0\",\"1\",\"1\"]\n]", "wordWrap": "true" }, "children": [] }, { "id": "f99bcdde-8550-420e-841c-4b008c61a2fd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "da528182-7e81-4276-991c-585b5cd0b513", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "34d0e69b-2223-437a-b557-74767568641f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "543fc98b-7583-4f43-b0f1-67683667a6a3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "9655aecb-99a7-4e6e-bfa0-dce1bac63349", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Depth First Search (DFS)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "7c32d8dc-6af0-48f2-bed2-fd07e8cbedc1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "9ae70689-d110-4ad5-81c9-7047ac61fd5f", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse every cell.", "styles": {} } ], "children": [] }, { "id": "5f5ebf1f-e265-4b02-8d84-008a41aeaf11", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the cell is land (", "styles": {} }, { "type": "text", "text": "'1'", "styles": { "code": true } }, { "type": "text", "text": "):", "styles": {} } ], "children": [ { "id": "53b6768a-a40a-4a56-819f-4518a214cb1e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Increase island count.", "styles": {} } ], "children": [] }, { "id": "04ede608-0828-46aa-a4fb-5f48831afd23", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Run DFS to visit all connected land cells.", "styles": {} } ], "children": [] } ] }, { "id": "56e87816-6334-49e9-ad42-c79f2cf55e6b", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Mark every visited land cell as water (", "styles": {} }, { "type": "text", "text": "'0'", "styles": { "code": true } }, { "type": "text", "text": ") to avoid revisiting.", "styles": {} } ], "children": [] }, { "id": "4adeefa7-d2b6-4e0e-96f4-cb921c83918a", "type": "divider", "props": {}, "children": [] }, { "id": "b836bf50-25b5-4323-beeb-8b39960dd358", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "15014655-b3e8-4bd9-b839-dc260be4e6ed", "type": "codeBlock", "props": { "language": "javascript", "code": "Grid\n\n1 1 0\n1 0 0\n0 1 1\n\nStart at (0,0)\n\nVisit\n\n1 1\n1\n\nIsland Count = 1\n\nContinue scanning...\n\nNext land at (2,1)\n\nVisit\n\n1 1\n\nIsland Count = 2\n\nAnswer = 2", "wordWrap": "true" }, "children": [] }, { "id": "cebedac8-956c-4ced-bf29-f9dfc83f977c", "type": "divider", "props": {}, "children": [] }, { "id": "49e71dff-252f-416e-9af2-03a92317e1dd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "40ad643a-2a5d-456e-903b-2521aeb7b3fc", "type": "addDoc", "props": { "fileId": "08fb84f8-0858-4c7d-b3b1-c04d156588b0", "title": "Number of Islands Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "8edd13b6-c816-44bf-a509-3507842cd7ac", "type": "divider", "props": {}, "children": [] }, { "id": "25f92b37-86cc-44f5-9f35-b960db9dda90", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "ff46661b-8892-41aa-be05-d88ce1b1435f", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(m × n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(m × n)", "styles": { "bold": true } }, { "type": "text", "text": " (Recursion Stack)", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "96a074b2-52bd-43eb-a4de-d1f06f750751", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "92b440c8-136f-4f2e-b3fe-75fb3801db9a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "m", "styles": { "bold": true } }, { "type": "text", "text": " = Number of rows", "styles": {} } ], "children": [] }, { "id": "b29dda15-3f20-4327-b53a-ed968d315f36", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of columns", "styles": {} } ], "children": [] }, { "id": "4c306b2e-2d3d-4aec-8976-2237a47c2997", "type": "divider", "props": {}, "children": [] }, { "id": "ddc7f51a-139f-4ce3-9524-ef3577f168ac", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "d28dc08b-1d3c-446e-bf40-dfbf4af12980", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Whenever you encounter a 2D grid traversal problem involving connected components, think of ", "styles": {} }, { "type": "text", "text": "DFS", "styles": { "bold": true } }, { "type": "text", "text": " or ", "styles": {} }, { "type": "text", "text": "BFS", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d22dd4b9-10ee-4dd8-9e1a-15fb11f81de7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "After visiting a land cell, mark it as visited immediately.", "styles": {} } ], "children": [] }, { "id": "223c7215-7815-492f-b42f-8ec0f99a2813", "type": "divider", "props": {}, "children": [] }, { "id": "3f4d8709-bbb0-4146-ae0a-565cc255c29c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "7d21412d-6cdf-4f23-b8bd-cc8290052c9e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Traverse every cell once.", "styles": {} } ], "children": [] }, { "id": "51d009e2-da3c-400c-a807-88287e637435", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Start DFS only from unvisited land.", "styles": {} } ], "children": [] }, { "id": "2da2d6e3-422b-4f99-82b4-3e9dc874a58d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Mark visited cells.", "styles": {} } ], "children": [] }, { "id": "5cdbeb43-4b30-4180-8bbb-7d8b3019ee59", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Each DFS represents one island.", "styles": {} } ], "children": [] }, { "id": "9326826b-3454-4be5-914a-af267d0950d0", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(m × n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "08fb84f8-0858-4c7d-b3b1-c04d156588b0", "title": "Number of Islands Java", "folderId": "2c9f3add-d21c-47c0-bbf1-adb8dc5b1a91", "time": 1785381619938, "createdAt": 1785381474242, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int numIslands(char[][] grid) {\n\n if (grid == null || grid.length == 0) {\n return 0;\n }\n\n int islands = 0;\n\n for (int i = 0; i < grid.length; i++) {\n\n for (int j = 0; j < grid[0].length; j++) {\n\n if (grid[i][j] == '1') {\n\n islands++;\n dfs(grid, i, j);\n }\n }\n }\n\n return islands;\n }\n\n private void dfs(char[][] grid, int row, int col) {\n\n if (row < 0 ||\n col < 0 ||\n row >= grid.length ||\n col >= grid[0].length ||\n grid[row][col] == '0') {\n\n return;\n }\n\n grid[row][col] = '0';\n\n dfs(grid, row + 1, col);\n dfs(grid, row - 1, col);\n dfs(grid, row, col + 1);\n dfs(grid, row, col - 1);\n }\n\n // Convert String[] to char[][]\n private static char[][] buildGrid(String[] rows) {\n\n char[][] grid = new char[rows.length][];\n\n for (int i = 0; i < rows.length; i++) {\n grid[i] = rows[i].toCharArray();\n }\n\n return grid;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n String[] rows = Input.getArray(\"grid\");\n int output = Input.getInt(\"output\");\n\n char[][] grid = buildGrid(rows);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.numIslands(grid));\n }\n}", "language": "java", "output": "Expected: 1\nActual: 1\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785381613640_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785381613640_0_0", "key": "grid", "value": "[\"11110\",\"11010\",\"11000\",\"00000\"]" }, { "id": "in_1785381613640_0_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785381613640_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785381613640_1_0", "key": "grid", "value": "[\"11000\",\"11000\",\"00100\",\"00011\"]" }, { "id": "in_1785381613640_1_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785381613640_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785381613640_2_0", "key": "grid", "value": "[\"1\"]" }, { "id": "in_1785381613640_2_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785381613640_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785381613640_3_0", "key": "grid", "value": "[\"0\"]" }, { "id": "in_1785381613640_3_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785381613640_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785381613640_4_0", "key": "grid", "value": "[\"111\",\"111\",\"111\"]" }, { "id": "in_1785381613640_4_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785381613640_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785381613640_5_0", "key": "grid", "value": "[\"101\",\"010\",\"101\"]" }, { "id": "in_1785381613640_5_1", "key": "output", "value": "5" } ] }, { "id": "tc_1785381613640_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785381613640_6_0", "key": "grid", "value": "[\"10001\",\"00000\",\"10001\"]" }, { "id": "in_1785381613640_6_1", "key": "output", "value": "4" } ] }, { "id": "tc_1785381613640_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785381613640_7_0", "key": "grid", "value": "[\"11100\",\"00110\",\"00110\",\"00001\"]" }, { "id": "in_1785381613640_7_1", "key": "output", "value": "3" } ] }, { "id": "tc_1785381613640_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785381613640_8_0", "key": "grid", "value": "[]" }, { "id": "in_1785381613640_8_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785381613640_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785381613640_9_0", "key": "grid", "value": "[\"000\",\"000\"]" }, { "id": "in_1785381613640_9_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785381613640_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785381613640_10_0", "key": "grid", "value": "[\"11111\"]" }, { "id": "in_1785381613640_10_1", "key": "output", "value": "1" } ] } ], "selectedTestCaseId": "tc_1785381613640_0" } } ] } ] }, { "_id": "92c804a9-d7ca-43b5-8486-d54302bb972a", "name": "26. Clone Graph", "createdAt": 1785381876712, "updatedAt": 1785381876712, "parentId": "f302655b-f36a-437a-b15d-5ec7687aa3f5", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "cf0da9fb-1a26-4194-8643-cbc5413b515c", "title": "Clone Graph Doc", "folderId": "92c804a9-d7ca-43b5-8486-d54302bb972a", "time": 1785382143931, "createdAt": 1785381879641, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "db500aeb-9c71-4086-ae96-7aa4004eb618", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "26. Clone Graph", "styles": {} } ], "children": [] }, { "id": "e57d5d7f-cf75-4678-89bf-373533e39451", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "23ff1e92-929e-4976-81b5-098a9ae9ec29", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given a reference of a node in a connected undirected graph, return a ", "styles": {} }, { "type": "text", "text": "deep copy (clone)", "styles": { "bold": true } }, { "type": "text", "text": " of the graph.", "styles": {} } ], "children": [] }, { "id": "6cb47ef4-e7ca-4ade-9c29-cf555f44e07e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Each node contains:", "styles": {} } ], "children": [] }, { "id": "ef8c4b14-39d5-4907-bbf2-8a844bd76182", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "int val", "styles": { "code": true } } ], "children": [] }, { "id": "eaf2a51d-1fc0-44d9-b2c5-79334f919685", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "List neighbors", "styles": { "code": true } } ], "children": [] }, { "id": "9f2ae34f-768c-4e80-95ed-401692dab149", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A deep copy means every node and every edge in the original graph should be recreated.", "styles": {} } ], "children": [] }, { "id": "e9258959-e53d-4009-97f0-8c88098c70a0", "type": "divider", "props": {}, "children": [] }, { "id": "7c5d0646-f760-4590-af9a-e84d33ed05ed", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "9a759694-ad3c-4fee-92a4-11260748c969", "type": "column_list", "props": {}, "children": [ { "id": "7e2e7616-359b-46f0-8986-05353e426256", "type": "column", "props": { "width": "1" }, "children": [ { "id": "4a777497-9bfe-467d-92d2-d77d9af88008", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "d2e21f34-28f1-4497-bc81-d08470562586", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "4b64a00c-d46f-424a-bdef-9e1886c2cdb9", "type": "codeBlock", "props": { "language": "javascript", "code": "adjList = [[2,4],[1,3],[2,4],[1,3]]", "wordWrap": "true" }, "children": [] }, { "id": "a1537df0-c5d4-4de3-9c40-ee4428203311", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "624307cb-e3e2-4441-9d18-cdb939f5ac26", "type": "codeBlock", "props": { "language": "javascript", "code": "[[2,4],[1,3],[2,4],[1,3]]", "wordWrap": "true" }, "children": [] }, { "id": "3a47e895-c09f-46ef-b5ae-2f4c850ab615", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "b9fa05b5-55fe-4054-b4ea-615a74682150", "type": "codeBlock", "props": { "language": "javascript", "code": "Original Graph Clone Graph\n\n 1 ----- 2 1 ----- 2\n | | | |\n | | | |\n 4 ----- 3 4 ----- 3", "wordWrap": "true" }, "children": [] }, { "id": "680cb0c9-7247-47ba-8b68-6b88a5842b75", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Both graphs have the same structure but consist of different node objects.", "styles": {} } ], "children": [] } ] }, { "id": "1e4abbb5-b17f-4b92-bf47-0b9cf88832ee", "type": "column", "props": { "width": "1" }, "children": [ { "id": "120242ca-eb19-453a-9961-336ae4a45050", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "52d9f71e-5214-4c28-8c92-890bfe1f4d2f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "b4c1ef39-b463-416d-bd15-4c49f3215399", "type": "codeBlock", "props": { "language": "javascript", "code": "adjList = [[]]", "wordWrap": "true" }, "children": [] }, { "id": "eabc0033-2d34-4a36-a5a3-67dce63285d2", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "9c58b684-5486-4b12-85e5-e650358cbda8", "type": "codeBlock", "props": { "language": "javascript", "code": "[[]]", "wordWrap": "true" }, "children": [] }, { "id": "4b01696c-e05d-4dcf-bda6-c9fb76b64f44", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "e4edf6f0-fb81-4ecc-89ea-04270752da8c", "type": "column", "props": { "width": "1" }, "children": [ { "id": "f2bea275-fc66-48c0-afec-13a4cac9ea40", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "ce98d5c3-10a7-42cf-945c-4ff8a03b4b8e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "fda93ec8-ccda-4f5e-a21b-5f90a8ac68c4", "type": "codeBlock", "props": { "language": "javascript", "code": "adjList = []", "wordWrap": "true" }, "children": [] }, { "id": "9b0620eb-f63c-4cc8-b86d-46e72abb638b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "abbd8265-8d59-4f29-8fb8-4f114998ab1c", "type": "codeBlock", "props": { "language": "javascript", "code": "[]", "wordWrap": "true" }, "children": [] }, { "id": "db81e6d6-30b0-4522-b10b-0ce534e0528c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "9617389b-b1b5-4ef3-b29f-4f5b3d9771bc", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "bc53fc0e-207c-488c-b3d9-6b72ac3f7d57", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Depth First Search (DFS)", "styles": { "bold": true } }, { "type": "text", "text": " with a ", "styles": {} }, { "type": "text", "text": "HashMap", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "12831879-2810-4c78-b04b-77198c07cc4e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "76691f59-0b16-464e-a385-ecc16aad9a41", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the node is ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "null", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d0086a03-4c48-4e33-aa6c-5ab2acaf3f51", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the node has already been cloned, return the cloned node.", "styles": {} } ], "children": [] }, { "id": "ecd35998-85e8-465b-bfa5-fba81042ce4c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Create a new node.", "styles": {} } ], "children": [] }, { "id": "fab681f4-ebb0-46ee-94bb-85dd50fef212", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Store the mapping:", "styles": {} } ], "children": [ { "id": "e5d327e6-e357-4ec7-a527-f8af228315d0", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Original Node → Cloned Node", "styles": {} } ], "children": [] } ] }, { "id": "a2698a79-bfb3-44e0-ae4f-3f6f8eb4b96e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Recursively clone every neighbor.", "styles": {} } ], "children": [] }, { "id": "c14383ac-5f32-429f-acb0-790699bd39b3", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Add cloned neighbors to the cloned node.", "styles": {} } ], "children": [] }, { "id": "04fa02f3-03a3-4b4d-911c-41dc31f95715", "type": "divider", "props": {}, "children": [] }, { "id": "f103ff4c-cefa-404e-a2f6-39ac2db7b6ac", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "607bf7c5-0513-4163-846f-b5f36c171fb6", "type": "codeBlock", "props": { "language": "javascript", "code": "Original\n\n1 ---- 2\n| |\n4 ---- 3\n\nClone(1)\n\nCreate clone of 1\n\n↓\n\nClone neighbor 2\n\n↓\n\nClone neighbor 3\n\n↓\n\nClone neighbor 4\n\n↓\n\nAlready visited?\n\nReturn cloned node.\n\nGraph cloned successfully.", "wordWrap": "true" }, "children": [] }, { "id": "75b23a5a-682c-4567-afb9-f51a9e63311c", "type": "divider", "props": {}, "children": [] }, { "id": "8550c063-5506-49ff-bca4-d234ab66d4aa", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "eb709ebc-f4d9-4c5b-85d9-f14bd2ffd007", "type": "addDoc", "props": { "fileId": "f9082675-3a86-492c-a3f4-74c9cc7101e7", "title": "Clone Graph Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "27fe339c-a9fe-4f9c-9aeb-ce8773024511", "type": "divider", "props": {}, "children": [] }, { "id": "d17c4f1c-7236-4602-b133-51a337339a4d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "fad98759-9e56-451c-8ff9-510ea6ac012c", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(V + E)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(V)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "194f192f-949b-4fa6-ac00-e08866af13a8", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "47f420ff-d1c9-490b-aa3f-ef24f80690d2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "V", "styles": { "bold": true } }, { "type": "text", "text": " = Number of vertices", "styles": {} } ], "children": [] }, { "id": "2d388826-8b1c-42db-80dd-9f0dd20e17ec", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "E", "styles": { "bold": true } }, { "type": "text", "text": " = Number of edges", "styles": {} } ], "children": [] }, { "id": "fd653711-5c3c-449a-ad17-247d5f53dec1", "type": "divider", "props": {}, "children": [] }, { "id": "e35dc21a-d664-4906-b408-8fd329395247", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "21e1f911-b4b5-49c0-9459-52ada3ea3aad", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Always maintain a map from the original node to the cloned node.", "styles": {} } ], "children": [] }, { "id": "00581560-829d-4b3c-a1f8-31197017b40f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Without this map, cycles in the graph will cause infinite recursion.", "styles": {} } ], "children": [] }, { "id": "7d3fa6af-cb0a-4a23-a248-1c36a8ba5e34", "type": "divider", "props": {}, "children": [] }, { "id": "080c2d4d-7f4f-49df-9b02-431530d95992", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "f27733bf-3672-4c11-9bb0-14c204ae9147", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "DFS + HashMap.", "styles": {} } ], "children": [] }, { "id": "5e0f94c7-cbd1-4a7f-a807-3d015681fa70", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Clone each node only once.", "styles": {} } ], "children": [] }, { "id": "b127dd5d-0834-49aa-a1bc-188a95dc98ca", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Handle cycles safely.", "styles": {} } ], "children": [] }, { "id": "233a8352-2e53-4aa0-986e-429137a84342", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Deep copy every edge.", "styles": {} } ], "children": [] }, { "id": "e4e965d0-3fb3-40ef-89ca-880007c48f2d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(V + E)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "f9082675-3a86-492c-a3f4-74c9cc7101e7", "title": "Clone Graph Java", "folderId": "92c804a9-d7ca-43b5-8486-d54302bb972a", "time": 1785382923998, "createdAt": 1785381884781, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Node {\n\n public int val;\n public List neighbors;\n\n public Node() {\n val = 0;\n neighbors = new ArrayList<>();\n }\n\n public Node(int val) {\n this.val = val;\n neighbors = new ArrayList<>();\n }\n}\n\nclass Solution {\n\n private Map map = new HashMap<>();\n\n public Node cloneGraph(Node node) {\n\n if (node == null) {\n return null;\n }\n\n if (map.containsKey(node)) {\n return map.get(node);\n }\n\n Node clone = new Node(node.val);\n\n map.put(node, clone);\n\n for (Node neighbor : node.neighbors) {\n clone.neighbors.add(cloneGraph(neighbor));\n }\n\n return clone;\n }\n\n // Build graph from adjacency list\n private static Node buildGraph(int[][] adjList) {\n\n if (adjList.length == 0) {\n return null;\n }\n\n Node[] nodes = new Node[adjList.length];\n\n for (int i = 0; i < adjList.length; i++) {\n nodes[i] = new Node(i + 1);\n }\n\n for (int i = 0; i < adjList.length; i++) {\n\n for (int neighbor : adjList[i]) {\n nodes[i].neighbors.add(nodes[neighbor - 1]);\n }\n }\n\n return nodes[0];\n }\n\n // Convert graph back to adjacency list\n private static List> toAdjList(Node node) {\n\n List> result = new ArrayList<>();\n\n if (node == null) {\n return result;\n }\n\n Map visited = new HashMap<>();\n Queue queue = new LinkedList<>();\n\n queue.offer(node);\n visited.put(node, 1);\n\n while (!queue.isEmpty()) {\n\n Node current = queue.poll();\n\n while (result.size() < current.val) {\n result.add(new ArrayList<>());\n }\n\n for (Node neighbor : current.neighbors) {\n\n result.get(current.val - 1).add(neighbor.val);\n\n if (!visited.containsKey(neighbor)) {\n visited.put(neighbor, neighbor.val);\n queue.offer(neighbor);\n }\n }\n }\n\n return result;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n int[][] adjList = Input.get2DIntArray(\"adjList\");\n\n Node graph = buildGraph(adjList);\n\n Solution sol = new Solution();\n\n Node clone = sol.cloneGraph(graph);\n\n System.out.println(toAdjList(clone));\n }\n}", "language": "java", "output": "[[2, 4], [1, 3], [2, 4], [1, 3]]\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785382172096_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785382172096_0_0", "key": "adjList", "value": "[[2,4],[1,3],[2,4],[1,3]]" } ] }, { "id": "tc_1785382172096_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785382172096_1_0", "key": "adjList", "value": "[[]]" } ] }, { "id": "tc_1785382172096_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785382172096_2_0", "key": "adjList", "value": "[]" } ] }, { "id": "tc_1785382172096_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785382172096_3_0", "key": "adjList", "value": "[[2],[1]]" } ] }, { "id": "tc_1785382172096_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785382172096_4_0", "key": "adjList", "value": "[[2,3],[1],[1]]" } ] }, { "id": "tc_1785382172096_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785382172096_5_0", "key": "adjList", "value": "[[2],[1,3],[2,4],[3]]" } ] }, { "id": "tc_1785382172096_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785382172096_6_0", "key": "adjList", "value": "[[2,3,4],[1],[1],[1]]" } ] }, { "id": "tc_1785382172096_7", "name": "Edge Case 1", "inputs": [ { "id": "in_1785382172096_7_0", "key": "adjList", "value": "[]" } ] }, { "id": "tc_1785382172096_8", "name": "Edge Case 2", "inputs": [ { "id": "in_1785382172096_8_0", "key": "adjList", "value": "[[]]" } ] }, { "id": "tc_1785382172097_9", "name": "Edge Case 3", "inputs": [ { "id": "in_1785382172096_9_0", "key": "adjList", "value": "[[2],[1]]" } ] } ], "selectedTestCaseId": "tc_1785382172096_0" } } ] } ] }, { "_id": "02634ced-d90d-4226-91aa-bea1117d96ae", "name": "27. Detect Cycle in Graph", "createdAt": 1785383039855, "updatedAt": 1785383039855, "parentId": "f302655b-f36a-437a-b15d-5ec7687aa3f5", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "58c8c252-dd66-4e66-89b2-d0a4866503c1", "title": "Cycle in Undirected Graph Doc", "folderId": "02634ced-d90d-4226-91aa-bea1117d96ae", "time": 1785383151601, "createdAt": 1785383047583, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "310aa86f-a998-493b-93fc-5d84a57ea08c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "27. Detect Cycle in Undirected Graph", "styles": {} } ], "children": [] }, { "id": "5cd03fb6-034d-45a5-8272-f39ed7227b8c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "21bad51e-f17a-4a26-8f3e-6bfb9a63c227", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an undirected graph with ", "styles": {} }, { "type": "text", "text": "V", "styles": { "bold": true } }, { "type": "text", "text": " vertices and an adjacency list, determine whether the graph contains a cycle.", "styles": {} } ], "children": [] }, { "id": "28346de4-949e-4f0c-9307-dd6c410d8311", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return:", "styles": {} } ], "children": [] }, { "id": "9cdd3c2e-67f7-4a2a-88fa-e48149b59b95", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "true", "styles": { "code": true } }, { "type": "text", "text": " if the graph contains a cycle.", "styles": {} } ], "children": [] }, { "id": "4b8885c9-41e3-4aba-86c5-871396cb6ff3", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "false", "styles": { "code": true } }, { "type": "text", "text": " otherwise.", "styles": {} } ], "children": [] }, { "id": "9a299e61-5007-4195-91a7-ff9dd96a3be8", "type": "divider", "props": {}, "children": [] }, { "id": "33150c48-2a3b-4424-9b55-eacde942ec2f", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "1bf89458-957b-433f-8b4b-20677eefbf68", "type": "column_list", "props": {}, "children": [ { "id": "4583281e-5e27-4e15-bed2-d1f86f348a89", "type": "column", "props": { "width": "1" }, "children": [ { "id": "e732d557-f215-412f-a1a3-33a77b82e4ff", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "0f638f38-40c3-43cb-8eef-1b31a2273b77", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "ffc43357-2df4-41d7-83ae-4fb3812f4e5e", "type": "codeBlock", "props": { "language": "javascript", "code": "V = 5\n\n0 --- 1\n| |\n| |\n3 --- 2\n\n4", "wordWrap": "true" }, "children": [] }, { "id": "a33063fb-4511-4676-99a1-26bb37dff392", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "359683e0-ae53-484b-a7ae-9019dcd1f7ec", "type": "codeBlock", "props": { "language": "javascript", "code": "true", "wordWrap": "true" }, "children": [] }, { "id": "0aa6fc41-5515-4fc1-806c-a0b79e040a70", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "e4bd8d06-8670-4df1-af15-0a23a9ccf0de", "type": "column", "props": { "width": "1" }, "children": [ { "id": "4628fb98-b049-40d6-adac-78116ca0ef4b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "d95d7147-4234-4472-8c84-8be52a072516", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "94f6034b-37d1-44d2-a3e4-0fe8e383702f", "type": "codeBlock", "props": { "language": "javascript", "code": "V = 4\n\n0 --- 1 --- 2 --- 3", "wordWrap": "true" }, "children": [] }, { "id": "48a1c518-9a50-4257-bd42-aed0f046f1a3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "02e3c7a3-3476-427b-a772-0d22e69bb305", "type": "codeBlock", "props": { "language": "javascript", "code": "false", "wordWrap": "true" }, "children": [] }, { "id": "7c7e7e48-6a89-4202-a831-e21ef98c9d39", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "fa4a2fc2-d2db-4482-82ca-7ab63d5e5fbd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "e537251e-df18-47e8-9dde-13c0f19e352e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Depth First Search (DFS)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "825ec285-2c51-4941-af30-d2f56b88a4cd", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "d0bc1f62-3600-4ab0-bbda-2c51ff938a23", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain a visited array.", "styles": {} } ], "children": [] }, { "id": "503f54ee-43fa-4622-be41-cb0705a77209", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Start DFS from every unvisited node.", "styles": {} } ], "children": [] }, { "id": "234ddd40-10c5-4138-900f-db17f77c4b05", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Mark the current node as visited.", "styles": {} } ], "children": [] }, { "id": "f93d98c5-bf0b-41b8-98d4-fb95457e894c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Visit every neighbor.", "styles": {} } ], "children": [] }, { "id": "ba42d506-12e8-4a0a-a01d-d83fb3ed6147", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Ignore the parent node.", "styles": {} } ], "children": [] }, { "id": "630a87b3-41f8-4590-833d-9f1303cbf3fb", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If a visited neighbor is found that is ", "styles": {} }, { "type": "text", "text": "not the parent", "styles": { "bold": true } }, { "type": "text", "text": ", a cycle exists.", "styles": {} } ], "children": [] }, { "id": "cce8a622-f18e-4d19-9065-a7ce81ee16c0", "type": "divider", "props": {}, "children": [] }, { "id": "b4340ea4-2053-4afa-b0d7-fcda9ad27b02", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "9dabe697-46e0-437e-ac31-f31a9f8f6a8a", "type": "codeBlock", "props": { "language": "javascript", "code": "Graph\n\n0 ----- 1\n| |\n| |\n3 ----- 2\n\nDFS(0)\n\n0 → 1 → 2 → 3\n\n3 has neighbor 0\n\n0 is already visited\nand is NOT parent\n\nCycle Found ✔", "wordWrap": "true" }, "children": [] }, { "id": "b94c1379-7a54-4a16-8b25-0065d6a824f2", "type": "divider", "props": {}, "children": [] }, { "id": "16a36f50-7b8b-48e1-acd5-40bc2072de34", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "8f58698c-e950-48db-bfff-6f356d235f42", "type": "addDoc", "props": { "fileId": "39cd7d18-c387-4615-9623-ac568b7007d8", "title": "Cycle in Undirected Graph Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "e5c0e4e2-55ae-4e95-89b7-914203bb10e0", "type": "divider", "props": {}, "children": [] }, { "id": "1309cc80-9335-4049-8f5e-5a47b0e00eb6", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "1c57ada6-10f5-4e80-be00-eb4ce437068a", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(V + E)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(V)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "b77de39f-c382-460b-bacb-12c45bcd6d05", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "e0050589-550b-4fc8-af2e-152416d692d7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "V", "styles": { "bold": true } }, { "type": "text", "text": " = Number of vertices", "styles": {} } ], "children": [] }, { "id": "daf8ae54-0ff8-4d6f-97c5-149b72d045df", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "E", "styles": { "bold": true } }, { "type": "text", "text": " = Number of edges", "styles": {} } ], "children": [] }, { "id": "b8e3acda-9296-4b45-af30-d61798116e4c", "type": "divider", "props": {}, "children": [] }, { "id": "81d27853-c56e-4911-b94a-e94328e2b9bb", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "f7032e4f-8e02-45f8-bc98-380169595bb7", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "In an undirected graph, revisiting the ", "styles": {} }, { "type": "text", "text": "parent", "styles": { "bold": true } }, { "type": "text", "text": " is expected.", "styles": {} } ], "children": [] }, { "id": "941e9169-2fe0-431f-bdbc-139994b75314", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A cycle exists only if you reach a previously visited node that is ", "styles": {} }, { "type": "text", "text": "not", "styles": { "bold": true } }, { "type": "text", "text": " the parent.", "styles": {} } ], "children": [] }, { "id": "1805bbf1-ab25-4cc2-9d52-bbab1aee7cb5", "type": "divider", "props": {}, "children": [] }, { "id": "c17e229a-5869-4f87-aa6d-7ceeed75682e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "91b12d0e-dba8-4023-9492-a0383238192a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "DFS traversal.", "styles": {} } ], "children": [] }, { "id": "ec3e552f-ee81-4be2-a9e5-06cfb0f10e24", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain visited array.", "styles": {} } ], "children": [] }, { "id": "abfe8874-6213-472e-8fae-3b8444524a6a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Track parent node.", "styles": {} } ], "children": [] }, { "id": "2703edfc-8fa9-423e-acca-ce5401e64c3b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Ignore parent edge.", "styles": {} } ], "children": [] }, { "id": "e3c521ed-558e-41db-876d-08b1d0f2a31b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(V + E)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "39cd7d18-c387-4615-9623-ac568b7007d8", "title": "Cycle in Undirected Graph Java", "folderId": "02634ced-d90d-4226-91aa-bea1117d96ae", "time": 1785383326086, "createdAt": 1785383050918, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "import java.util.*;\n\nclass Solution {\n\n public boolean hasCycle(int V, List> adj) {\n\n boolean[] visited = new boolean[V];\n\n for (int i = 0; i < V; i++) {\n\n if (!visited[i]) {\n\n if (dfs(i, -1, visited, adj)) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n private boolean dfs(int node,\n int parent,\n boolean[] visited,\n List> adj) {\n\n visited[node] = true;\n\n for (int neighbor : adj.get(node)) {\n\n if (!visited[neighbor]) {\n\n if (dfs(neighbor, node, visited, adj)) {\n return true;\n }\n\n } else if (neighbor != parent) {\n\n return true;\n }\n }\n\n return false;\n }\n\n // Build adjacency list from edge list\n private static List> buildGraph(int V, int[][] edges) {\n\n List> adj = new ArrayList<>();\n\n for (int i = 0; i < V; i++) {\n adj.add(new ArrayList<>());\n }\n\n for (int[] edge : edges) {\n\n int u = edge[0];\n int v = edge[1];\n\n adj.get(u).add(v);\n adj.get(v).add(u);\n }\n\n return adj;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n int V = Input.getInt(\"V\");\n int[][] edges = Input.get2DIntArray(\"edges\");\n boolean output = Input.getBool(\"output\");\n\n List> graph = buildGraph(V, edges);\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.hasCycle(V, graph));\n }\n}", "language": "java", "output": "Expected: true\nActual: true\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785383093820_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785383093820_0_0", "key": "V", "value": "4" }, { "id": "in_1785383093820_0_1", "key": "edges", "value": "[[0,1],[1,2],[2,3],[3,0]]" }, { "id": "in_1785383093820_0_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785383093820_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785383093820_1_0", "key": "V", "value": "4" }, { "id": "in_1785383093820_1_1", "key": "edges", "value": "[[0,1],[1,2],[2,3]]" }, { "id": "in_1785383093820_1_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785383093820_2_0", "key": "V", "value": "5" }, { "id": "in_1785383093820_2_1", "key": "edges", "value": "[[0,1],[1,2],[2,0],[3,4]]" }, { "id": "in_1785383093820_2_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785383093820_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785383093820_3_0", "key": "V", "value": "5" }, { "id": "in_1785383093820_3_1", "key": "edges", "value": "[[0,1],[1,2],[2,3],[3,4]]" }, { "id": "in_1785383093820_3_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785383093820_4_0", "key": "V", "value": "6" }, { "id": "in_1785383093820_4_1", "key": "edges", "value": "[[0,1],[1,2],[2,3],[3,1],[4,5]]" }, { "id": "in_1785383093820_4_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785383093820_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785383093820_5_0", "key": "V", "value": "1" }, { "id": "in_1785383093820_5_1", "key": "edges", "value": "[]" }, { "id": "in_1785383093820_5_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785383093820_6_0", "key": "V", "value": "2" }, { "id": "in_1785383093820_6_1", "key": "edges", "value": "[[0,1]]" }, { "id": "in_1785383093820_6_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785383093820_7_0", "key": "V", "value": "3" }, { "id": "in_1785383093820_7_1", "key": "edges", "value": "[[0,1],[1,2],[2,0]]" }, { "id": "in_1785383093820_7_2", "key": "output", "value": "true" } ] }, { "id": "tc_1785383093820_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785383093820_8_0", "key": "V", "value": "0" }, { "id": "in_1785383093820_8_1", "key": "edges", "value": "[]" }, { "id": "in_1785383093820_8_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785383093820_9_0", "key": "V", "value": "4" }, { "id": "in_1785383093820_9_1", "key": "edges", "value": "[]" }, { "id": "in_1785383093820_9_2", "key": "output", "value": "false" } ] }, { "id": "tc_1785383093820_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785383093820_10_0", "key": "V", "value": "2" }, { "id": "in_1785383093820_10_1", "key": "edges", "value": "[[0,1],[1,0]]" }, { "id": "in_1785383093820_10_2", "key": "output", "value": "true" } ] } ], "selectedTestCaseId": "tc_1785383093820_0" } } ] } ] } ], "files": [] }, { "_id": "1f379e43-79c4-44e9-9fa9-64262c64c3fe", "name": "7. Dynamic Programming", "createdAt": 1785383194370, "updatedAt": 1785383194370, "parentId": "7ed5c858-39da-489a-a5bf-555bd8c814f6", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [ { "_id": "11a8be88-45de-4504-a2d9-d7522176f973", "name": "28. Fibonacci Number", "createdAt": 1785383280467, "updatedAt": 1785383280467, "parentId": "1f379e43-79c4-44e9-9fa9-64262c64c3fe", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "307365fc-d51a-4636-95be-54d6190ec761", "title": "Fibonacci Number Doc", "folderId": "11a8be88-45de-4504-a2d9-d7522176f973", "time": 1785383407401, "createdAt": 1785383283334, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "e8ea744c-73a0-47ed-8c1f-3d01138482e8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "28. Fibonacci Number", "styles": {} } ], "children": [] }, { "id": "aeebf742-3a7a-4933-b66e-508346f89170", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "67f7c8ca-5eac-4280-87ab-7979b6bea289", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The Fibonacci numbers are defined as:", "styles": {} } ], "children": [] }, { "id": "333c74ef-0f17-4468-bace-c07e658bcdb6", "type": "codeBlock", "props": { "language": "javascript", "code": "F(0) = 0\nF(1) = 1\nF(n) = F(n - 1) + F(n - 2), for n > 1", "wordWrap": "true" }, "children": [] }, { "id": "718724b4-9efe-45c9-b1a7-4b6636c2901f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given an integer ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": ", return the ", "styles": {} }, { "type": "text", "text": "nᵗʰ", "styles": { "code": true } }, { "type": "text", "text": " Fibonacci number.", "styles": {} } ], "children": [] }, { "id": "141067a4-7d1a-4112-8eab-ba555e482942", "type": "divider", "props": {}, "children": [] }, { "id": "3a9f8a62-5439-429c-b80d-7c6b6b628973", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "de9befe7-8f7b-4700-8c9b-f3fe087bde47", "type": "column_list", "props": {}, "children": [ { "id": "1de4a4e2-a5d1-4c61-b839-7470c799cc58", "type": "column", "props": { "width": "1" }, "children": [ { "id": "a7f8100b-abac-453f-a2f7-e33e7528334d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "18ced8b7-61b2-499e-9635-3deb72ac16b6", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "ec0b9f43-4b7c-4de9-8829-decb40126406", "type": "codeBlock", "props": { "language": "javascript", "code": "n = 2", "wordWrap": "true" }, "children": [] }, { "id": "220c2060-2d64-424a-8131-4892b0e9f430", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "b66ca0c2-79fe-4195-812c-affdae575e3b", "type": "codeBlock", "props": { "language": "javascript", "code": "1", "wordWrap": "true" }, "children": [] }, { "id": "221b8651-3ddf-49e5-a488-ca2563aa3f48", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "3573e42d-c572-449d-ae70-f41f67866c58", "type": "codeBlock", "props": { "language": "javascript", "code": "F(2) = F(1) + F(0)\n = 1 + 0\n = 1", "wordWrap": "true" }, "children": [] }, { "id": "db36ceec-8320-42c4-8be4-fa2bc3642c18", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "4143cecb-f9fb-45e1-8fcb-9fda9f0678f9", "type": "column", "props": { "width": "1" }, "children": [ { "id": "8b571c84-33e3-4f80-a7cd-698b0e79665b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "d33a4a32-28bc-4509-9de6-f78ed777d9a4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "145c4dd8-656d-406d-bc41-61cc25a887c4", "type": "codeBlock", "props": { "language": "javascript", "code": "n = 4", "wordWrap": "true" }, "children": [] }, { "id": "d0f17bfd-8e46-49b6-b042-144a288bda08", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "1f66c741-3355-420e-9206-0c75d79ed809", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "47feecae-a276-4f9f-93e3-274897484a80", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "919ca6e7-210a-49b3-8adf-c6bacb3def70", "type": "codeBlock", "props": { "language": "javascript", "code": "F(4) = F(3) + F(2)\n = 2 + 1\n = 3", "wordWrap": "true" }, "children": [] }, { "id": "97ad2c81-8d4a-4d67-bb34-6991c163ceef", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "b75e8d4b-d2fd-41a4-a4e8-19aa00c83aa3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "a24935f5-6dd9-4185-940b-d951f886f58c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Dynamic Programming (Iterative)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "be93e162-e218-4e37-93a8-af4a61fd0f0e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Steps", "styles": {} } ], "children": [] }, { "id": "cccf134c-928c-4371-9d30-fc83d36c100c", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Handle the base cases:", "styles": {} } ], "children": [ { "id": "31ac63a0-0873-4c33-a87d-1903df6bce32", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "n = 0", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "0", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "8a2bc46a-2e98-4ec1-918b-0ea6ae35bf60", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If ", "styles": {} }, { "type": "text", "text": "n = 1", "styles": { "code": true } }, { "type": "text", "text": ", return ", "styles": {} }, { "type": "text", "text": "1", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] } ] }, { "id": "c65835bb-c22d-4906-8dd0-5935592c7b12", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Maintain two variables:", "styles": {} } ], "children": [ { "id": "1ae9f337-56c5-4b19-8bd7-ea6ae9001502", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "prev2", "styles": { "code": true } }, { "type": "text", "text": " → Fibonacci(n-2)", "styles": {} } ], "children": [] }, { "id": "32f542ca-7dd7-46ff-bdab-7e310bbe574b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "prev1", "styles": { "code": true } }, { "type": "text", "text": " → Fibonacci(n-1)", "styles": {} } ], "children": [] } ] }, { "id": "e7e069d2-5d89-40f2-ad26-8115e1111f7e", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Iterate from ", "styles": {} }, { "type": "text", "text": "2", "styles": { "code": true } }, { "type": "text", "text": " to ", "styles": {} }, { "type": "text", "text": "n", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "10554cf4-b8f6-48bd-b44b-c81bf650b663", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compute the current Fibonacci number.", "styles": {} } ], "children": [] }, { "id": "7b8d82f0-bf87-4494-9cd7-c77707031cbc", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Update the previous values.", "styles": {} } ], "children": [] }, { "id": "88005012-b704-425f-813d-b4e0462ea3fa", "type": "numberedListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Return the final result.", "styles": {} } ], "children": [] }, { "id": "1de1ab9b-4cdd-41dc-b4fe-fc41a837c1d5", "type": "divider", "props": {}, "children": [] }, { "id": "0661e2e6-8a64-42e0-88b7-b43879dd10b2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "5b1e6f79-b904-4cce-a242-0bed97fbf181", "type": "codeBlock", "props": { "language": "javascript", "code": "n = 5\n\nprev2 = 0\nprev1 = 1\n\ni = 2\n\ncurrent = 1\n\nprev2 = 1\nprev1 = 1\n\ni = 3\n\ncurrent = 2\n\nprev2 = 1\nprev1 = 2\n\ni = 4\n\ncurrent = 3\n\nprev2 = 2\nprev1 = 3\n\ni = 5\n\ncurrent = 5\n\nAnswer = 5", "wordWrap": "true" }, "children": [] }, { "id": "cb55cc38-6a33-41aa-b63d-830e246aa3d5", "type": "divider", "props": {}, "children": [] }, { "id": "ff456d5a-d53c-42cb-be66-60d72acd085a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "e4327682-3c6e-4994-8656-06254f90b080", "type": "addDoc", "props": { "fileId": "bb0d695c-a8c6-4398-88f9-487942177563", "title": "Fibonacci Number Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "d3656a08-8879-45bf-b88d-a73fa850092d", "type": "divider", "props": {}, "children": [] }, { "id": "3f67b4b4-3c3a-486b-9c31-3ba756cedbaa", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "ef931208-0dbe-47f4-90dc-c469cb2560ed", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(1)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "c8e391d6-67ea-4e28-99c2-9c04ad5b76e5", "type": "divider", "props": {}, "children": [] }, { "id": "848f2e0b-f476-47d7-9845-46a205d9b1ea", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "1dd56dd5-33e9-4915-ab81-569fbefb2f1c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Although Fibonacci can be solved recursively, the recursive solution has exponential time complexity. The iterative dynamic programming approach is much more efficient and uses constant extra space.", "styles": {} } ], "children": [] }, { "id": "e4d4b4c7-5d27-43eb-9077-0fa98d1c3c1d", "type": "divider", "props": {}, "children": [] }, { "id": "4492ee50-342e-4ffd-b27e-3fdc4c5ad22d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "6e92fce3-d564-44e5-a83c-5349a448fa55", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Handle base cases first.", "styles": {} } ], "children": [] }, { "id": "86f37690-20a0-4e10-a2c7-c35d11819818", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use two variables instead of an array.", "styles": {} } ], "children": [] }, { "id": "11e34db3-cd7e-48c6-9042-12b2a319ef8d", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Dynamic Programming avoids repeated calculations.", "styles": {} } ], "children": [] }, { "id": "e3c830a6-1b87-48df-9e3a-6048668bb3c9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n)", "styles": { "bold": true } }, { "type": "text", "text": " time and ", "styles": {} }, { "type": "text", "text": "O(1)", "styles": { "bold": true } }, { "type": "text", "text": " space.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "bb0d695c-a8c6-4398-88f9-487942177563", "title": "Fibonacci Number Java", "folderId": "11a8be88-45de-4504-a2d9-d7522176f973", "time": 1785383416199, "createdAt": 1785383291087, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int fib(int n) {\n\n if (n == 0) {\n return 0;\n }\n\n if (n == 1) {\n return 1;\n }\n\n int prev2 = 0;\n int prev1 = 1;\n\n for (int i = 2; i <= n; i++) {\n\n int current = prev1 + prev2;\n\n prev2 = prev1;\n prev1 = current;\n }\n\n return prev1;\n }\n\n public static void main(String[] args) {\n\n // Custom DevScribe input\n int n = Input.getInt(\"n\");\n int output = Input.getInt(\"output\");\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.fib(n));\n }\n}", "language": "java", "output": "Expected: 0\nActual: 0\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785383349250_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785383349250_0_0", "key": "n", "value": "0" }, { "id": "in_1785383349250_0_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785383349250_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785383349250_1_0", "key": "n", "value": "1" }, { "id": "in_1785383349250_1_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785383349250_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785383349250_2_0", "key": "n", "value": "2" }, { "id": "in_1785383349250_2_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785383349250_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785383349250_3_0", "key": "n", "value": "3" }, { "id": "in_1785383349250_3_1", "key": "output", "value": "2" } ] }, { "id": "tc_1785383349250_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785383349250_4_0", "key": "n", "value": "5" }, { "id": "in_1785383349250_4_1", "key": "output", "value": "5" } ] }, { "id": "tc_1785383349250_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785383349250_5_0", "key": "n", "value": "10" }, { "id": "in_1785383349250_5_1", "key": "output", "value": "55" } ] }, { "id": "tc_1785383349250_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785383349250_6_0", "key": "n", "value": "20" }, { "id": "in_1785383349250_6_1", "key": "output", "value": "6765" } ] }, { "id": "tc_1785383349250_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785383349250_7_0", "key": "n", "value": "30" }, { "id": "in_1785383349250_7_1", "key": "output", "value": "832040" } ] }, { "id": "tc_1785383349250_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785383349250_8_0", "key": "n", "value": "0" }, { "id": "in_1785383349250_8_1", "key": "output", "value": "0" } ] }, { "id": "tc_1785383349250_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785383349250_9_0", "key": "n", "value": "1" }, { "id": "in_1785383349250_9_1", "key": "output", "value": "1" } ] }, { "id": "tc_1785383349250_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785383349250_10_0", "key": "n", "value": "45" }, { "id": "in_1785383349250_10_1", "key": "output", "value": "1134903170" } ] } ], "selectedTestCaseId": "tc_1785383349250_0" } } ] } ] }, { "_id": "8297e225-3e00-4363-9cb4-15f1e88e535d", "name": "29. Knapsack Problem", "createdAt": 1785383466876, "updatedAt": 1785383466876, "parentId": "1f379e43-79c4-44e9-9fa9-64262c64c3fe", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "6181eec4-ae78-4866-a0d5-0b67fca08fe1", "title": "Knapsack Problem Doc", "folderId": "8297e225-3e00-4363-9cb4-15f1e88e535d", "time": 1785383581405, "createdAt": 1785383469689, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "fecafcd8-ca88-480d-8368-cacf7e488b34", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "29. 0/1 Knapsack Problem", "styles": {} } ], "children": [] }, { "id": "424675e8-1dc5-4c6a-b556-700bb191b5d6", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "926c9acf-94ef-45a4-ad4a-518cfc88e8ca", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given:", "styles": {} } ], "children": [] }, { "id": "bc295cf3-ef74-4535-9abc-bb55be7e83b7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "An array ", "styles": {} }, { "type": "text", "text": "weights[]", "styles": { "code": true } }, { "type": "text", "text": " representing the weight of each item.", "styles": {} } ], "children": [] }, { "id": "a16001ca-9b8c-4d8f-89ac-2402658028b7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "An array ", "styles": {} }, { "type": "text", "text": "values[]", "styles": { "code": true } }, { "type": "text", "text": " representing the value of each item.", "styles": {} } ], "children": [] }, { "id": "2efea0b3-0504-4cd7-b10c-28d3395fa46f", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A knapsack with capacity ", "styles": {} }, { "type": "text", "text": "W", "styles": { "code": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "32ff6f14-a2f9-496f-873a-d3535dae4773", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Determine the ", "styles": {} }, { "type": "text", "text": "maximum total value", "styles": { "bold": true } }, { "type": "text", "text": " that can be obtained by selecting items.", "styles": {} } ], "children": [] }, { "id": "260d9b3a-0b80-48d9-9d22-ba47a5886948", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Each item can either be:", "styles": {} } ], "children": [] }, { "id": "1909bde1-5d67-4a3b-9c6b-5944c0c38adb", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Picked ", "styles": {} }, { "type": "text", "text": "once", "styles": { "bold": true } }, { "type": "text", "text": ", or", "styles": {} } ], "children": [] }, { "id": "037693f4-8352-478f-a00c-6e51d2b313dd", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Not picked.", "styles": {} } ], "children": [] }, { "id": "52973b67-d3b2-4d0a-b544-59d5863f66fd", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "An item ", "styles": {} }, { "type": "text", "text": "cannot", "styles": { "bold": true } }, { "type": "text", "text": " be selected multiple times.", "styles": {} } ], "children": [] }, { "id": "43585134-a265-49bf-bb1e-e010880a50fb", "type": "divider", "props": {}, "children": [] }, { "id": "a9f95d56-4e02-4df3-beb7-c02ff8b599a3", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "a11ee270-4a4e-49ec-a021-206eb3906310", "type": "column_list", "props": {}, "children": [ { "id": "8eb7fb6d-68ad-4b5f-b0ba-e3881784573a", "type": "column", "props": { "width": "1" }, "children": [ { "id": "b2166f76-6b1c-47e6-91c3-b118a8e1b777", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "85b26bcb-45dd-4ee2-b5f9-19324341dc0d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "2689dd30-c081-4541-9dde-7c4a954fab97", "type": "codeBlock", "props": { "language": "javascript", "code": "weights = [1,3,4,5]\nvalues = [1,4,5,7]\nW = 7", "wordWrap": "true" }, "children": [] }, { "id": "f0554b37-2242-408f-9067-1635f11a356e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "4e91c909-650f-418d-b007-b19b495a3880", "type": "codeBlock", "props": { "language": "javascript", "code": "9", "wordWrap": "true" }, "children": [] }, { "id": "fe9b63b2-98e5-4636-85a8-571022d05297", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "7aa17136-4595-4a25-9abe-82d2747310e7", "type": "codeBlock", "props": { "language": "javascript", "code": "Choose:\n\nWeight = 3 Value = 4\nWeight = 4 Value = 5\n\nTotal Weight = 7\nTotal Value = 9", "wordWrap": "true" }, "children": [] }, { "id": "81c41104-ae45-4749-a501-e29b20c2c9b0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "986cff66-142f-414b-be29-4914f6488e7a", "type": "column", "props": { "width": "1" }, "children": [ { "id": "d415a0b4-71fd-4080-a403-93482284e472", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "6e2d3c8b-28b8-4f01-9f5f-2972257b62bb", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "1ce327c3-78ae-4b68-a757-593115525018", "type": "codeBlock", "props": { "language": "javascript", "code": "weights = [2,3,4]\nvalues = [4,5,6]\nW = 5", "wordWrap": "true" }, "children": [] }, { "id": "cf35002e-213f-42c7-9d35-cf62ede4d771", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "406d8315-5e29-4964-84ca-d03bef47a572", "type": "codeBlock", "props": { "language": "javascript", "code": "9", "wordWrap": "true" }, "children": [] }, { "id": "05dd2c57-eee6-4daf-a352-9e210f0b144c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "07711313-ce5f-4e3f-aa1d-d1b14fb57212", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "1a7685d7-d20b-4938-94dc-4fdf18cb7d6f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Dynamic Programming", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "805c1f3c-134f-46b7-9ab0-5fa8b27c31b1", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Let:", "styles": {} } ], "children": [] }, { "id": "2cd492ca-3280-4b4d-84b4-04d27a040c98", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][w]\n\nMaximum value using first i items\nwith capacity w.", "wordWrap": "true" }, "children": [] }, { "id": "e4080b46-083b-4edd-a542-bdde47b65192", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Transition", "styles": {} } ], "children": [] }, { "id": "5ea801d0-df29-4539-a5da-86a956cb0f0f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "For every item,", "styles": {} } ], "children": [] }, { "id": "f7d0efa1-b5fc-4475-bfcf-782b84b87c8f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If current weight fits:", "styles": {} } ], "children": [] }, { "id": "346ee631-5952-4579-bc47-9ca7f3f03490", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][w] = max(\n\nTake current item,\n\nSkip current item\n\n)", "wordWrap": "true" }, "children": [] }, { "id": "f0a358f4-7ead-4cb0-8b5b-54612e40310f", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise,", "styles": {} } ], "children": [] }, { "id": "ad64d63a-765e-4b92-b20a-1ed2d0ea9d58", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][w] = dp[i-1][w]", "wordWrap": "true" }, "children": [] }, { "id": "9e610a74-0869-40d4-bb75-69d89126f83e", "type": "divider", "props": {}, "children": [] }, { "id": "f935a962-ed90-43b3-911f-fcee4dd7db66", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "0cde88c5-d888-4c89-8321-c58893746b01", "type": "codeBlock", "props": { "language": "javascript", "code": "weights = [1,3,4]\n\nvalues = [1,4,5]\n\nCapacity = 4\n\nPick item 1\n\nValue = 1\n\nCapacity left = 3\n\nPick item 2\n\nValue = 4\n\nTotal = 5\n\nBetter than taking item 3 alone.\n\nAnswer = 5", "wordWrap": "true" }, "children": [] }, { "id": "f88cea2c-f120-425f-8165-f66ec0a60c50", "type": "divider", "props": {}, "children": [] }, { "id": "a5eb6933-bd1e-40f6-98fa-d231901ac140", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "d6af9ed6-f775-4342-817c-7d67f29181b8", "type": "addDoc", "props": { "fileId": "eb266d96-768d-4f2f-ade2-b93aa509c28a", "title": "Knapsack Problem Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "c0a2c6d5-f69f-49cd-99b6-0b8fe812fb82", "type": "divider", "props": {}, "children": [] }, { "id": "1c7c6199-8d05-412e-a3d0-d7bb8e5f7ca8", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "6ad8e46e-12c3-4ec2-b629-0dc5be501d89", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n × W)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(n × W)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "09c28264-b770-4715-851f-89bcbdd97f8b", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "a22afd41-e3f1-4657-a2e9-9ccfb03e8178", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Number of items", "styles": {} } ], "children": [] }, { "id": "023fdebb-1c4c-470b-b410-82bd10f51691", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "W", "styles": { "bold": true } }, { "type": "text", "text": " = Knapsack capacity", "styles": {} } ], "children": [] }, { "id": "da0f1360-6fb6-4f3e-9047-4bee1abef9de", "type": "divider", "props": {}, "children": [] }, { "id": "2ec79051-df7c-4caa-a696-187e3b1740e0", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "7ce373cd-4e0e-4292-986b-bff681f26596", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The 0/1 Knapsack is one of the most important Dynamic Programming problems.", "styles": {} } ], "children": [] }, { "id": "5c459122-84ce-41fc-b907-31147a627be0", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Remember:", "styles": {} } ], "children": [] }, { "id": "13aa4f7c-0a57-45ec-a545-498fa7b263b7", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "0/1 Knapsack → Pick once", "styles": {} } ], "children": [] }, { "id": "4e1bfb98-7c20-45e3-a8c4-27351e1a29bc", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Unbounded Knapsack → Pick unlimited times", "styles": {} } ], "children": [] }, { "id": "e6078479-cb71-4d1a-99b8-949cca91a951", "type": "divider", "props": {}, "children": [] }, { "id": "ad178fa3-fdc8-4810-b94d-21465b70b2aa", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "e8734eea-25d1-4ea5-a377-b3ddc54be00e", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Dynamic Programming problem.", "styles": {} } ], "children": [] }, { "id": "97110cd1-85e6-4728-8d86-993621de6ed4", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Each item is picked at most once.", "styles": {} } ], "children": [] }, { "id": "c1bc74a9-4374-4e63-95d3-eef634e4bd59", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Build DP table bottom-up.", "styles": {} } ], "children": [] }, { "id": "a954c8d8-49c5-4735-a4b2-a105b854e148", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(n × W)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "eb266d96-768d-4f2f-ade2-b93aa509c28a", "title": "Knapsack Problem Java", "folderId": "8297e225-3e00-4363-9cb4-15f1e88e535d", "time": 1785383589552, "createdAt": 1785383473449, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int knapSack(int W, int[] weights, int[] values) {\n\n int n = weights.length;\n\n int[][] dp = new int[n + 1][W + 1];\n\n for (int i = 1; i <= n; i++) {\n\n for (int w = 0; w <= W; w++) {\n\n if (weights[i - 1] <= w) {\n\n dp[i][w] = Math.max(\n values[i - 1] + dp[i - 1][w - weights[i - 1]],\n dp[i - 1][w]\n );\n\n } else {\n\n dp[i][w] = dp[i - 1][w];\n }\n }\n }\n\n return dp[n][W];\n }\n\n public static void main(String[] args) {\n\n int W = Input.getInt(\"W\");\n int[] weights = Input.getIntArray(\"weights\");\n int[] values = Input.getIntArray(\"values\");\n int output = Input.getInt(\"output\");\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.knapSack(W, weights, values));\n }\n}", "language": "java", "output": "Expected: 9\nActual: 9\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785383520168_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785383520168_0_0", "key": "W", "value": "7" }, { "id": "in_1785383520168_0_1", "key": "weights", "value": "[1,3,4,5]" }, { "id": "in_1785383520168_0_2", "key": "values", "value": "[1,4,5,7]" }, { "id": "in_1785383520168_0_3", "key": "output", "value": "9" } ] }, { "id": "tc_1785383520168_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785383520168_1_0", "key": "W", "value": "5" }, { "id": "in_1785383520168_1_1", "key": "weights", "value": "[2,3,4]" }, { "id": "in_1785383520168_1_2", "key": "values", "value": "[4,5,6]" }, { "id": "in_1785383520168_1_3", "key": "output", "value": "9" } ] }, { "id": "tc_1785383520168_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785383520168_2_0", "key": "W", "value": "10" }, { "id": "in_1785383520168_2_1", "key": "weights", "value": "[2,3,5,7]" }, { "id": "in_1785383520168_2_2", "key": "values", "value": "[20,30,50,70]" }, { "id": "in_1785383520168_2_3", "key": "output", "value": "100" } ] }, { "id": "tc_1785383520168_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785383520168_3_0", "key": "W", "value": "4" }, { "id": "in_1785383520168_3_1", "key": "weights", "value": "[4]" }, { "id": "in_1785383520168_3_2", "key": "values", "value": "[10]" }, { "id": "in_1785383520168_3_3", "key": "output", "value": "10" } ] }, { "id": "tc_1785383520168_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785383520168_4_0", "key": "W", "value": "3" }, { "id": "in_1785383520168_4_1", "key": "weights", "value": "[4]" }, { "id": "in_1785383520168_4_2", "key": "values", "value": "[10]" }, { "id": "in_1785383520168_4_3", "key": "output", "value": "0" } ] }, { "id": "tc_1785383520168_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785383520168_5_0", "key": "W", "value": "8" }, { "id": "in_1785383520168_5_1", "key": "weights", "value": "[2,3,4,5]" }, { "id": "in_1785383520168_5_2", "key": "values", "value": "[3,4,5,8]" }, { "id": "in_1785383520168_5_3", "key": "output", "value": "12" } ] }, { "id": "tc_1785383520168_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785383520168_6_0", "key": "W", "value": "50" }, { "id": "in_1785383520168_6_1", "key": "weights", "value": "[10,20,30]" }, { "id": "in_1785383520168_6_2", "key": "values", "value": "[60,100,120]" }, { "id": "in_1785383520168_6_3", "key": "output", "value": "220" } ] }, { "id": "tc_1785383520168_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785383520168_7_0", "key": "W", "value": "6" }, { "id": "in_1785383520168_7_1", "key": "weights", "value": "[1,2,3]" }, { "id": "in_1785383520168_7_2", "key": "values", "value": "[10,15,40]" }, { "id": "in_1785383520168_7_3", "key": "output", "value": "65" } ] }, { "id": "tc_1785383520168_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785383520168_8_0", "key": "W", "value": "0" }, { "id": "in_1785383520168_8_1", "key": "weights", "value": "[1,2,3]" }, { "id": "in_1785383520168_8_2", "key": "values", "value": "[10,20,30]" }, { "id": "in_1785383520168_8_3", "key": "output", "value": "0" } ] }, { "id": "tc_1785383520168_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785383520168_9_0", "key": "W", "value": "10" }, { "id": "in_1785383520168_9_1", "key": "weights", "value": "[]" }, { "id": "in_1785383520168_9_2", "key": "values", "value": "[]" }, { "id": "in_1785383520168_9_3", "key": "output", "value": "0" } ] }, { "id": "tc_1785383520168_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785383520168_10_0", "key": "W", "value": "1" }, { "id": "in_1785383520168_10_1", "key": "weights", "value": "[1]" }, { "id": "in_1785383520168_10_2", "key": "values", "value": "[100]" }, { "id": "in_1785383520168_10_3", "key": "output", "value": "100" } ] } ], "selectedTestCaseId": "tc_1785383520168_0" } } ] } ] }, { "_id": "96e41475-540e-4ea4-84eb-35885d12c7a7", "name": "30. Longest Common Subsequence", "createdAt": 1785383634551, "updatedAt": 1785383634551, "parentId": "1f379e43-79c4-44e9-9fa9-64262c64c3fe", "github_repo": null, "github_branch": "main", "github_path": "/", "github_last_sync_sha": null, "github_sync_enabled": 0, "github_encryption_enabled": 0, "github_passphrase": null, "subfolders": [], "files": [ { "_id": "33882cbd-1103-4732-ae00-584f87966a53", "title": "Longest Common Subsequence Doc", "folderId": "96e41475-540e-4ea4-84eb-35885d12c7a7", "time": 1785383766524, "createdAt": 1785383640537, "updatedAt": 1785385669906, "fileType": "document-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "document-editor", "data": { "blocks": [ { "id": "9365ce15-071c-471c-bc29-7c8601053ed1", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 1, "isToggleable": false }, "content": [ { "type": "text", "text": "30. Longest Common Subsequence", "styles": {} } ], "children": [] }, { "id": "02421335-6af4-494f-bfa5-06637d371d25", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Problem", "styles": {} } ], "children": [] }, { "id": "fc7febc9-86b7-4e63-bfa0-412161ec7f9e", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Given two strings ", "styles": {} }, { "type": "text", "text": "text1", "styles": { "code": true } }, { "type": "text", "text": " and ", "styles": {} }, { "type": "text", "text": "text2", "styles": { "code": true } }, { "type": "text", "text": ", return the length of their ", "styles": {} }, { "type": "text", "text": "Longest Common Subsequence (LCS)", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "c9f8be44-46ed-4900-8e81-f9d865fe1e4d", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "A subsequence is a sequence that can be derived from another sequence by deleting some or no characters without changing the order of the remaining characters.", "styles": {} } ], "children": [] }, { "id": "20f83577-d040-4f3b-b81d-31dbeb808c47", "type": "divider", "props": {}, "children": [] }, { "id": "0d3c5872-0bc9-4b2f-b524-a6636927b47d", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Example", "styles": {} } ], "children": [] }, { "id": "9a1d5be0-c5e8-4d15-9543-116d5edebe6b", "type": "column_list", "props": {}, "children": [ { "id": "b9a47d0f-f8af-48be-b1eb-ee8b317098d7", "type": "column", "props": { "width": "1" }, "children": [ { "id": "141f29e5-c66d-422f-8ad0-41dd1f5d448a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 1", "styles": {} } ], "children": [] }, { "id": "2eabfea2-d7b7-4961-8f0d-c442ee2bfe72", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "2bd92b1f-19a9-4e22-a76f-66f6a5507cb2", "type": "codeBlock", "props": { "language": "javascript", "code": "text1 = \"abcde\"\ntext2 = \"ace\"", "wordWrap": "true" }, "children": [] }, { "id": "b1c84a68-f237-47dd-9288-5f22183d90da", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "273b5211-186b-47c3-abe7-5a6a9f18fbb7", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "bde248b0-9586-41f8-ab23-135c4becc7e3", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Explanation", "styles": { "bold": true } } ], "children": [] }, { "id": "2424aac3-8166-4526-a1da-2093fbc78070", "type": "codeBlock", "props": { "language": "javascript", "code": "The longest common subsequence is:\n\nace\n\nLength = 3", "wordWrap": "true" }, "children": [] }, { "id": "b100d291-d6b7-4402-99f4-a6532169c170", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "bc0aa88d-1bc0-460c-b335-78aa611255c9", "type": "column", "props": { "width": "1" }, "children": [ { "id": "70b883a5-2ead-435f-bc68-74321ccce38b", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 2", "styles": {} } ], "children": [] }, { "id": "c9bd3458-06cc-4a89-9087-8d569b21b8a6", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "ae7dff28-e133-408c-b657-e6e710596d64", "type": "codeBlock", "props": { "language": "javascript", "code": "text1 = \"abc\"\ntext2 = \"abc\"", "wordWrap": "true" }, "children": [] }, { "id": "2be68519-c93b-4500-b4d8-3cf989bdf325", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "ec11fc6f-ea4a-4c97-be19-e967c71e100a", "type": "codeBlock", "props": { "language": "javascript", "code": "3", "wordWrap": "true" }, "children": [] }, { "id": "32b73e39-bd75-41d6-9f1b-c72c3956ddce", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] }, { "id": "9c9f900b-915e-42ab-b51b-5ca70cfc2f0c", "type": "column", "props": { "width": "1" }, "children": [ { "id": "3f235834-640f-456f-b143-e928574f53ea", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Example 3", "styles": {} } ], "children": [] }, { "id": "52e455c9-6565-4bf2-b593-e9e17faeb338", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Input", "styles": { "bold": true } } ], "children": [] }, { "id": "14d9a210-7e2d-4e3c-8e8f-66b43de74212", "type": "codeBlock", "props": { "language": "javascript", "code": "text1 = \"abc\"\ntext2 = \"def\"", "wordWrap": "true" }, "children": [] }, { "id": "2362b4a0-2015-4d06-869b-0fe0afbab14a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Output", "styles": { "bold": true } } ], "children": [] }, { "id": "3a127823-18c8-4dd3-940e-3717a4410027", "type": "codeBlock", "props": { "language": "javascript", "code": "0", "wordWrap": "true" }, "children": [] }, { "id": "1f766892-ebda-41f0-a346-b6ffc7491272", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [], "children": [] } ] } ] }, { "id": "d382d7e8-574d-4d8b-9d7a-de27310b0298", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Approach", "styles": {} } ], "children": [] }, { "id": "2786bac1-4bc2-4352-9682-ae63f0759414", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Use ", "styles": {} }, { "type": "text", "text": "Dynamic Programming", "styles": { "bold": true } }, { "type": "text", "text": ".", "styles": {} } ], "children": [] }, { "id": "d243d927-d436-4fce-a3d0-a509a44f6301", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Let:", "styles": {} } ], "children": [] }, { "id": "821183c9-a0c6-42c6-9590-10d96a8bfb7f", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][j]\n\nLength of the longest common subsequence\nbetween\n\ntext1[0...i-1]\ntext2[0...j-1]", "wordWrap": "true" }, "children": [] }, { "id": "6444782d-dc95-40fd-be75-62bbb339c84e", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 3, "isToggleable": false }, "content": [ { "type": "text", "text": "Transition", "styles": {} } ], "children": [] }, { "id": "be21cd9e-e5e8-4987-bb8c-b3e5f35a64b4", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "If the characters match:", "styles": {} } ], "children": [] }, { "id": "5d4dedf0-10d8-45ef-b716-35d1680f2035", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][j] = 1 + dp[i-1][j-1]", "wordWrap": "true" }, "children": [] }, { "id": "7b638704-4119-416b-bbb1-737f5753d839", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Otherwise:", "styles": {} } ], "children": [] }, { "id": "4ab3ecc8-d55d-4196-923e-8ad76b228247", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[i][j] = max(\n\ndp[i-1][j],\n\ndp[i][j-1]\n\n)", "wordWrap": "true" }, "children": [] }, { "id": "a0a0c164-6568-4935-80fd-441498af53fe", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "The answer is stored in:", "styles": {} } ], "children": [] }, { "id": "420f79e0-d08e-42de-9655-53368061d0ff", "type": "codeBlock", "props": { "language": "javascript", "code": "dp[m][n]", "wordWrap": "true" }, "children": [] }, { "id": "7b2794f2-2841-4273-9781-936d219b1c3c", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "where", "styles": {} } ], "children": [] }, { "id": "a3b5c15d-70a7-40e1-90ca-991d9a821b76", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "m = text1.length()", "styles": {} } ], "children": [] }, { "id": "067a3537-f6e8-47e9-9dd7-7db59fb91ad2", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n = text2.length()", "styles": {} } ], "children": [] }, { "id": "a4a56e43-5902-4d5b-9940-f3ec8f27e23d", "type": "divider", "props": {}, "children": [] }, { "id": "56aab12a-3c73-4f63-97a1-3050b81ebb62", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Dry Run", "styles": {} } ], "children": [] }, { "id": "a9cdafda-19c8-4746-9e04-a88fff44867a", "type": "codeBlock", "props": { "language": "javascript", "code": "text1 = \"abcde\"\n\ntext2 = \"ace\"\n\nMatch a\n\nLCS = 1\n\nMatch c\n\nLCS = 2\n\nMatch e\n\nLCS = 3\n\nAnswer = 3", "wordWrap": "true" }, "children": [] }, { "id": "fdd045b8-9b95-48a5-ab3c-4f8132ed5d8d", "type": "divider", "props": {}, "children": [] }, { "id": "eea7800d-cc55-4ae6-8f6f-4bc573177739", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Java Solution", "styles": {} } ], "children": [] }, { "id": "62ab5e00-61ee-4100-b4b4-924f0270c5c0", "type": "addDoc", "props": { "fileId": "49c57e4f-b49a-435b-aae5-db215d7197a1", "title": "Longest Common Subsequence Java", "fileType": "code-editor", "embedMode": "embed" }, "children": [] }, { "id": "8a890f85-48a2-422f-ac28-8ec411a3410b", "type": "divider", "props": {}, "children": [] }, { "id": "67d02f46-3d84-4cc6-8102-35e9552dbc3c", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Complexity Analysis", "styles": {} } ], "children": [] }, { "id": "27fd3f33-1e6f-4935-9033-ec1c3923969a", "type": "table", "props": { "textColor": "default" }, "content": { "type": "tableContent", "columnWidths": [ null, null ], "headerRows": 1, "rows": [ { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Complexity", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "Value", "styles": {} } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Time Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(m × n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] }, { "cells": [ { "type": "tableCell", "content": [ { "type": "text", "text": "Space Complexity", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } }, { "type": "tableCell", "content": [ { "type": "text", "text": "O(m × n)", "styles": { "bold": true } } ], "props": { "colspan": 1, "rowspan": 1, "backgroundColor": "default", "textColor": "default", "textAlignment": "left" } } ] } ] }, "children": [] }, { "id": "608362b9-0e3c-4674-b72e-b5f1a21fb52a", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Where:", "styles": {} } ], "children": [] }, { "id": "e56233b5-3ad6-45bd-be9d-5bebd511a09b", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "m", "styles": { "bold": true } }, { "type": "text", "text": " = Length of text1", "styles": {} } ], "children": [] }, { "id": "a72582b7-5c6b-46ce-85da-462865ce24f9", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "n", "styles": { "bold": true } }, { "type": "text", "text": " = Length of text2", "styles": {} } ], "children": [] }, { "id": "4e18c887-7a35-4492-9d7d-ff2cda68ad7b", "type": "divider", "props": {}, "children": [] }, { "id": "c22450bd-7491-47ea-aa46-413fea242a3a", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Interview Tip", "styles": {} } ], "children": [] }, { "id": "55042f48-18da-451f-8f9a-ac2956312781", "type": "paragraph", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "LCS is one of the most important Dynamic Programming problems and forms the basis for problems like Edit Distance, Diff Utilities, and Longest Palindromic Subsequence.", "styles": {} } ], "children": [] }, { "id": "449a5a86-5db1-43d3-9c89-6c42828f004b", "type": "divider", "props": {}, "children": [] }, { "id": "a4cd4ae7-c8d1-4941-9529-55d34df5adf2", "type": "heading", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left", "level": 2, "isToggleable": false }, "content": [ { "type": "text", "text": "Key Takeaways", "styles": {} } ], "children": [] }, { "id": "605b4bae-83ce-4c2a-a83c-b8f241c94385", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Dynamic Programming problem.", "styles": {} } ], "children": [] }, { "id": "83ca4e54-87d5-4ceb-a809-000ced061078", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Compare characters one by one.", "styles": {} } ], "children": [] }, { "id": "76d0c788-0fe9-431e-9027-1ff7fd01ce88", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Build the DP table bottom-up.", "styles": {} } ], "children": [] }, { "id": "e7d24365-56e7-4891-aade-f5bb3b26373a", "type": "bulletListItem", "props": { "backgroundColor": "default", "textColor": "default", "textAlignment": "left" }, "content": [ { "type": "text", "text": "Runs in ", "styles": {} }, { "type": "text", "text": "O(m × n)", "styles": { "bold": true } }, { "type": "text", "text": " time.", "styles": {} } ], "children": [] } ] } } ] }, { "_id": "49c57e4f-b49a-435b-aae5-db215d7197a1", "title": "Longest Common Subsequence Java", "folderId": "96e41475-540e-4ea4-84eb-35885d12c7a7", "time": 1785383694002, "createdAt": 1785383645519, "updatedAt": 1785385669906, "fileType": "code-editor", "github_sha": "5a4ae8de64139bbcd7e6e1134f67dafdff6ca994", "github_last_sync": 1785385669906, "document": [ { "type": "code-editor", "data": { "code": "class Solution {\n\n public int longestCommonSubsequence(String text1, String text2) {\n\n int m = text1.length();\n int n = text2.length();\n\n int[][] dp = new int[m + 1][n + 1];\n\n for (int i = 1; i <= m; i++) {\n\n for (int j = 1; j <= n; j++) {\n\n if (text1.charAt(i - 1) == text2.charAt(j - 1)) {\n\n dp[i][j] = 1 + dp[i - 1][j - 1];\n\n } else {\n\n dp[i][j] = Math.max(\n dp[i - 1][j],\n dp[i][j - 1]\n );\n }\n }\n }\n\n return dp[m][n];\n }\n\n public static void main(String[] args) {\n\n String text1 = Input.getString(\"text1\");\n String text2 = Input.getString(\"text2\");\n int output = Input.getInt(\"output\");\n\n Solution sol = new Solution();\n\n System.out.println(\"Expected: \" + output);\n System.out.println(\"Actual: \" + sol.longestCommonSubsequence(text1, text2));\n }\n}", "language": "java", "output": "Expected: 3\nActual: 3\n", "panelOpen": true, "wordWrap": false, "testCases": [ { "id": "tc_1785383689201_0", "name": "Test Case 1", "inputs": [ { "id": "in_1785383689201_0_0", "key": "text1", "value": "abcde" }, { "id": "in_1785383689201_0_1", "key": "text2", "value": "ace" }, { "id": "in_1785383689201_0_2", "key": "output", "value": "3" } ] }, { "id": "tc_1785383689201_1", "name": "Test Case 2", "inputs": [ { "id": "in_1785383689201_1_0", "key": "text1", "value": "abc" }, { "id": "in_1785383689201_1_1", "key": "text2", "value": "abc" }, { "id": "in_1785383689201_1_2", "key": "output", "value": "3" } ] }, { "id": "tc_1785383689201_2", "name": "Test Case 3", "inputs": [ { "id": "in_1785383689201_2_0", "key": "text1", "value": "abc" }, { "id": "in_1785383689201_2_1", "key": "text2", "value": "def" }, { "id": "in_1785383689201_2_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785383689201_3", "name": "Test Case 4", "inputs": [ { "id": "in_1785383689201_3_0", "key": "text1", "value": "AGGTAB" }, { "id": "in_1785383689201_3_1", "key": "text2", "value": "GXTXAYB" }, { "id": "in_1785383689201_3_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785383689201_4", "name": "Test Case 5", "inputs": [ { "id": "in_1785383689201_4_0", "key": "text1", "value": "aaaa" }, { "id": "in_1785383689201_4_1", "key": "text2", "value": "aa" }, { "id": "in_1785383689201_4_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785383689201_5", "name": "Test Case 6", "inputs": [ { "id": "in_1785383689201_5_0", "key": "text1", "value": "abcdef" }, { "id": "in_1785383689201_5_1", "key": "text2", "value": "fbdamn" }, { "id": "in_1785383689201_5_2", "key": "output", "value": "2" } ] }, { "id": "tc_1785383689201_6", "name": "Test Case 7", "inputs": [ { "id": "in_1785383689201_6_0", "key": "text1", "value": "stone" }, { "id": "in_1785383689201_6_1", "key": "text2", "value": "longest" }, { "id": "in_1785383689201_6_2", "key": "output", "value": "3" } ] }, { "id": "tc_1785383689201_7", "name": "Test Case 8", "inputs": [ { "id": "in_1785383689201_7_0", "key": "text1", "value": "leetcode" }, { "id": "in_1785383689201_7_1", "key": "text2", "value": "etco" }, { "id": "in_1785383689201_7_2", "key": "output", "value": "4" } ] }, { "id": "tc_1785383689201_8", "name": "Edge Case 1", "inputs": [ { "id": "in_1785383689201_8_0", "key": "text1", "value": "" }, { "id": "in_1785383689201_8_1", "key": "text2", "value": "" }, { "id": "in_1785383689201_8_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785383689202_9", "name": "Edge Case 2", "inputs": [ { "id": "in_1785383689202_9_0", "key": "text1", "value": "" }, { "id": "in_1785383689202_9_1", "key": "text2", "value": "abc" }, { "id": "in_1785383689202_9_2", "key": "output", "value": "0" } ] }, { "id": "tc_1785383689202_10", "name": "Edge Case 3", "inputs": [ { "id": "in_1785383689202_10_0", "key": "text1", "value": "abc" }, { "id": "in_1785383689202_10_1", "key": "text2", "value": "" }, { "id": "in_1785383689202_10_2", "key": "output", "value": "0" } ] } ], "selectedTestCaseId": "tc_1785383689201_0" } } ] } ] } ], "files": [] } ], "files": [] } }