Recently, our PROGRAMHELP team assisted several candidates with their Google onsite interview for graduate/internship software engineer positions, and most of them received their dream offer from Google. and most of them received their dream offer from Google. This series will analyze some of the onsite questions that our team met during the Google onsite interviews. This series will analyze some of the onsite questions that our team met during the Google onsite interviews.
Recently, our PROGRAMHELP team has provided coaching and vicarious interview assistance to several candidates for their Google onsite interviews, and the vast majority of them have been successful in getting offers from Google with the help of our team members.In this series, let's analyze the algorithmic questions that were encountered in the last few Google onsite interviews for you to learn from.
Onsite Algorithm Questions
Given an Array A, find the minimum amplitude you can get after changing up to 3 elements. Amplitude is the range of the array (basically the difference between the largest and smallest element). between the largest and smallest element).
Example 1
Input: [-1, 3, -1, 8, 5 4]
Output: 2
Explanation: we can change -1, -1, 8 to 3, 4 or 5
Example 2
Input: [10, 10, 3, 4, 10]
Output: 0
Explanation: change 3 and 4 to 10
Analysis
At first glance of the question, our team offers an initial solution for the candidate, i.e. using sort and greedy. The time complexity is O(n * logn). The solution is listed below: (in Typescript)
At the first sight of the topic, our team members directly provided the candidate with an initial solution using sorting and greedy algorithm. The time complexity of this algorithm is O(n * logn). Below is the Typescript source code of the solution:
function minDifference(nums: number[]): number {
if (!nums || nums.length === 0) return undefined;
if (nums.length <= 4) return 0;
nums.sort((a: number, b: number) => a - b);
let min = Number.MAX_SAFE_INTEGER;
for (
let left = 0, right = nums.length - 4;
left < 4 && right < nums.length;
left++, right++
) {
min = Math.min(min, nums[right] - nums[left]);
}
return min;
}
Of course, the capability of our team members is not limited to an O(n * logn) solution. we further offer a Partial Sort solution (To learn more about Partial Sort, please refer to the leetcode question. 215. Kth Largest Element in an Array), which has the time complexity of O(n). As we only sort the min 4 and the max 4 numbers in an array. As there is no priority queue in Typescript, we use Java for the solution.
Of course, the capabilities of our team members do not stop at solutions with O(n * logn) time complexity. We went further and proposed a solution with O(n) time complexity based on partial ordering (for partial ordering, see leetcode). 215. Kth Largest Element in an Array). In this method, we only need to get the smallest, and the largest 4 numbers in an array. Since Typescript does not provide a PriorityQueue, we provide a Java-based solution:
public class Solution {
public int minDifference(int[] nums) {
int numsSize = nums.length.
if (numsSize <= 4) {
return 0; }
}
// Find the four smallest elements using a fixed-size max heap
PriorityQueue maxHeap = new PriorityQueue(
Collections.reverseOrder()
); for (int num : nums)
for (int num : nums) {
maxHeap.offer(num); if (maxHeap.size())
if (maxHeap.size() > 4) {
maxHeap.poll();
}
}
List smallestFour = new ArrayList(maxHeap);
Collections.sort(smallestFour); }
// Find the four largest elements using a fixed-size min heap
PriorityQueue minHeap = new PriorityQueue(); // Find the four largest elements using a fixed-size min heap.
for (int num : nums) {
minHeap.offer(num); if (minHeap.size())
if (minHeap.size() > 4) {
minHeap.poll();
}
}
List largestFour = new ArrayList(minHeap);
Collections.sort(largestFour); }
int minDiff = Integer.MAX_VALUE; // Four scenarios to compute the minimum difference.
// Four scenarios to compute the minimum difference
for (int i = 0; i < 4; i++) {
minDiff = Math.min(
minDiff, maxFour.get(i)
largestFour.get(i) - smallestFour.get(i)
);
}
return minDiff; }
}
}
The interviewer was, surely, very content when we offered an O(n) solution. The interviewer was also been able to see the way our team members explained the thinking process clearly and neatly. The interviewer was also been able to see the way our team members explained the thinking process clearly and neatly. These certainly help the candidate pass this round and finally get the offer from Google.
The interviewer was very pleased with the O(n) solution provided by our team. The interviewer was also able to see the clarity and concise explanation of the problem solving ideas that our team members helped the candidate provide. All of these elements helped the candidate pass the round of interviews and ultimately get an offer from Google.
Contact Us
After our strong interview assistance, OA ghostwriting, Onsite interview assistance, candidates through the analysis and communication of these interview questions, the interviewer not only understands the candidate's programming ability, but also sees my clear thinking and effective communication skills in the problem solving process. These not only help to cope with Google's interview, but also enhance our ability to solve practical programming problems. I wish you all good luck in your interviews!
The Candidate observed my problem-solving and thinking process clearly through our interview support service, which is applicable to Google interviews and can also strengthen programming abilities. The Candidate observed my problem-solving and thinking process clearly through our interview support service which is applicable to Google interviews and can also strengthen programming abilities. Wishing everyone luck with their interview!
If you also need our interview assistance services, please contact us immediately.