# θ³‡θ¨Šη§‘ζŠ€η”’ζ₯­ε°ˆζ‘ˆθ¨­θ¨ˆ 作ζ₯­ε›› ## Mock Interview πŸ¦Ήβ€β™‚οΈ : Richard (Interviewer) πŸ‘¨β€πŸ’» : Mason (Interviewee) ### 34. Find First and Last Position of Element in Sorted Array **Description** Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. example: nums = [5,7,7,8,8,10], target = 8 **Interview** πŸ¦Ήβ€β™‚οΈ : Hello Mason, we're glad to have you here in our interview, would you like to briefly introduce yourself and your previous job? πŸ‘¨β€πŸ’» : πŸ¦Ήβ€ : It sounds impressive, I'm sure you'll do well in the following process. I've just sent you a document and there's a small exercise on it, can you see it? πŸ‘¨β€πŸ’» : Yes, (Mason answer or question) πŸ¦Ήβ€ : Let me briefly introduce the question for you, now we'll give you an sorted array of integer `nums`, and a target number `target`. We want you to give us the leftmost position of `target` and the rightmost position of `target`. πŸ¦Ήβ€ : If there's no problem, you can tell me your approach and maybe start to implement your solution πŸ‘¨β€πŸ’» : (Mason answer correctly!) πŸ¦Ήβ€ : Can you calculate the time complexity of your algorithm? πŸ‘¨β€πŸ’» : (Mason answer) πŸ¦Ήβ€ : Can you lower the time complexity to $O(log(n))$ πŸ‘¨β€πŸ’» : (Mason answer) ε¦‚ζžœεœ¨ι€™ι‚Šδ½ δ½Ώη”¨ιžθΏ΄θ§£ζ³•ζˆ–θ€…helper function, ζˆ‘ζœƒε•εœ¨function callηš„ζˆζœ¬θ·Ÿζ™‚ι–“θ€‡ι›œεΊ¦δΉ‹ι–“, δ½ ζ€ŽιΊΌεˆ†ζžδ½•θ€…ηš„ζˆζœ¬ζ―”θΌƒδ½Ž πŸ¦Ήβ€ : I think your solution is pretty well, but you use (recursive or helper function) more function call in this solution. I wonder how do you make the tradeoff between complexity and the cost of function call? ```cpp! /** * Note: The returned array must be malloced, assume caller calls free(). */ int findLeftIndex(int* nums, int numsSize, int target) { int low = 0, high = numsSize - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return low; } int findRightIndex(int* nums, int numsSize, int target) { int low = 0, high = numsSize - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] <= target) { low = mid + 1; } else { high = mid - 1; } } return high; } int* searchRange(int* nums, int numsSize, int target, int* returnSize) { *returnSize = 2; int* result = (int*)malloc(*returnSize * sizeof(int)); result[0] = -1; // Initialize with [-1, -1] result[1] = -1; int left = findLeftIndex(nums, numsSize, target); int right = findRightIndex(nums, numsSize, target); // Check if the target is within the range found if (left <= right && right < numsSize && nums[left] == target && nums[right] == target) { result[0] = left; result[1] = right; } return result; } ``` ```diff -if (nums[mid] <= target) { - low = mid + 1; -} else { - high = mid - 1; -} +nums[mid] <= target ? low = mid + 1 : high = mid - 1; ``` ### 287. Find the Duplicate Number **Description** Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. **Interview** πŸ¦Ήβ€β™‚οΈ : Hello Mason, we're glad to have you here in our interview, would you like to briefly introduce yourself and your previous job? πŸ‘¨β€πŸ’» : (Mason answer) πŸ¦Ήβ€ : It sounds impressive, I'm sure you'll do well in the following process. I've just sent you a document and there's a small exercise on it, can you see it? πŸ‘¨β€πŸ’» : Yes, (Mason answer or question) ```c int findDupclicate(int *nums, int numsSize) { int *table = calloc(sizeof(int), numsSize); for (int i=0; i<numsSize; i++) { if (table[nums[i]]) return nums[i]; table[nums[i]]++; } return -1; // error case } ``` πŸ¦Ήβ€ : We'll give you an array of integer `nums`, the length of `nums` is `numsSize`, the elements in this array are in the range between `1`~`numsSize`. There'll be exactly 2 elements with the same value, please tell us which value is repeated. πŸ¦Ήβ€ : If there's no problem, you can tell me your approach and maybe start to implement your solution πŸ‘¨β€πŸ’» : (Mason answer correctly!) πŸ¦Ήβ€ : Can you calculate the time complexity and space complexity of your algorithm? πŸ‘¨β€πŸ’» : (Mason answer) πŸ¦Ήβ€ : Can you make the time complexity to $O(n)$ and space complexity to $O(1)$ ? ```cpp! int findDuplicate(int* nums, int numsSize) { // Initialize the tortoise and hare pointers int tortoise = nums[0]; int hare = nums[0]; // Find the meeting point of the tortoise and hare do { tortoise = nums[tortoise]; hare = nums[nums[hare]]; } while (tortoise != hare); // Find the entrance to the cycle tortoise = nums[0]; while (tortoise != hare) { tortoise = nums[tortoise]; hare = nums[hare]; } return hare; } ```