# θ³θ¨η§ζη’ζ₯ε°ζ‘θ¨θ¨ δ½ζ₯ε
## 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;
}
```