You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -36,112 +36,105 @@ Leave 10–15 minutes to reflect, share feedback, and then switch roles.
36
36
37
37
Best of luck, and enjoy the practice! 🚀
38
38
39
-
## Problem: Sort Colors (Dutch Flag)
39
+
## Problem: Find First Occurrence (Binary Search Variant)
40
40
41
-
Given an array with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
41
+
Given a sorted array of integers that may contain duplicates and a target value, return the index of the first occurrence of the target if it exists in the array. If the target doesn't exist, return -1.
42
42
43
-
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
44
-
45
-
You must solve this problem without using the library's sort function and in one pass (O(n) time).
43
+
You must write an algorithm with O(log n) runtime complexity.
46
44
47
45
### Follow-up Questions:
48
46
- What is the time complexity of your solution?
49
47
- What is the space complexity?
50
-
- How would this extend to k colors instead of 3?`,
48
+
- How would you modify this to find the last occurrence instead?`,
51
49
exercise: {
52
50
starterCode:`/*
53
-
Problem: Sort Colors (Dutch Flag)
54
-
55
-
Given an array with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
51
+
Problem: Find First Occurrence (Binary Search Variant)
56
52
57
-
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
53
+
Given a sorted array of integers that may contain duplicates and a target value, return the index of the first occurrence of the target if it exists in the array. If the target doesn't exist, return -1.
58
54
59
-
You must solve this problem without using the library's sort function and in one pass (O(n) time).
55
+
You must write an algorithm with O(log n) runtime complexity.
60
56
61
57
Examples:
62
-
Input: [2, 0, 2, 1, 1, 0]
63
-
Output: [0, 0, 1, 1, 2, 2]
58
+
Input: nums = [5, 7, 7, 8, 8, 10], target = 8
59
+
Output: 3
60
+
61
+
Input: nums = [5, 7, 7, 8, 8, 10], target = 6
62
+
Output: -1
64
63
65
-
Input: [2, 0, 1]
66
-
Output: [0, 1, 2]
64
+
Input: nums = [1, 1, 1, 1, 1], target = 1
65
+
Output: 0
67
66
68
67
Follow-up Questions:
69
68
- What is the time complexity of your solution?
70
69
- What is the space complexity?
71
-
- How would this extend to k colors instead of 3?
70
+
- How would you modify this to find the last occurrence instead?
72
71
*/
73
72
74
-
function sortColors(nums) {
75
-
// Approach: Use three pointers (Dutch Flag algorithm)
76
-
// left: boundary for 0s (red)
77
-
// right: boundary for 2s (blue)
78
-
// current: current element being processed
79
-
// Goal: [0s][1s][2s]
73
+
function searchFirst(nums, target) {
74
+
// Approach: Modified binary search
75
+
// When we find the target, continue searching left to find first occurrence
76
+
// Keep track of the leftmost found index
80
77
81
78
// Your code here
82
79
}`,
83
80
solution:`/*
84
-
Problem: Sort Colors (Dutch Flag)
81
+
Problem: Find First Occurrence (Binary Search Variant)
85
82
86
-
Given an array with n objects colored red, white, or blue, sort them in-place.
87
-
Use integers 0, 1, and 2 to represent red, white, and blue respectively.
83
+
Given a sorted array that may contain duplicates, find the first occurrence of target.
88
84
*/
89
85
90
-
function sortColors(nums) {
91
-
// Initialize three pointers
92
-
let left = 0; // Boundary for 0s (red)
93
-
let right = nums.length - 1; // Boundary for 2s (blue)
94
-
let current = 0; // Current element being processed
86
+
function searchFirst(nums, target) {
87
+
// Initialize left and right pointers
88
+
let left = 0;
89
+
let right = nums.length - 1;
90
+
let result = -1; // Store the first occurrence index
95
91
96
-
// Process elements until current passes right boundary
97
-
while (current <= right) {
98
-
if (nums[current] === 0) {
99
-
// Found red (0): swap with left boundary and move both pointers
message: `Edge cases failed. Single 0: ${JSON.stringify(test1)}, Single 1: ${JSON.stringify(test2)}, Single 2: ${JSON.stringify(test3)}, Empty: ${JSON.stringify(test4)}`
message: `Edge cases failed. Empty: ${searchFirst([],1)}, Single found: ${searchFirst([1],1)}, Single not found: ${searchFirst([1],2)}, Two identical: ${searchFirst([1,1],1)}, Two different: ${searchFirst([1,2],2)}`
223
196
});
224
197
}
225
198
}catch(error){
@@ -229,31 +202,25 @@ function sortColors(nums) {
229
202
});
230
203
}
231
204
},
232
-
message: "Function should handle already sorted arrays correctly."
205
+
message: "Function should handle edge cases correctly."
message: `Same elements cases failed. All 1s: ${searchFirst([1,1,1,1,1],1)}, All 5s: ${searchFirst([5,5,5],5)}, All 2s: ${searchFirst([2,2,2,2],2)}, Not found: ${searchFirst([1,1,1,1,1],2)}`
257
224
});
258
225
}
259
226
}catch(error){
@@ -263,36 +230,31 @@ function sortColors(nums) {
263
230
});
264
231
}
265
232
},
266
-
message: "Function should handle reverse sorted arrays correctly."
233
+
message: "Function should handle arrays with all same elements correctly."
0 commit comments