Microsoft | Phone | Bangalore | Sr. Software Engineer

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-04-05Added By Anonymous
Source: Link

Microsoft Technical Interview

Interview started with basic introduction round. Interviewer had shared codelity to do online coding.

Before writing code of given proble statement interviewer was more eager to know the approach of solution.

Few questions are important before solving any quesitons. They expect to reply properly:

•Explain me the approach for the solution of this problem?
•What will be the time and space complexity?
•Why list or array or hashmap or disctionary?
•Do you have any alternative approach to solve this problem?


Below were the questions:

53. Maximum Subarray

This is a pretty simple question. You can solve by using Kadane's algorithm technique.

Another questions..


Policemen catch thieves

Given an array of size n that has the following specifications:

Each element in the array contains either a policeman or a thief.

Each policeman can catch only one thief.

A policeman cannot catch a thief who is more than K units away from the policeman.

We need to find the maximum number of thieves that can be caught.

Example:

Input : arr[] = {'P', 'T', 'T', 'P', 'T'},

k = 1.

Output : 2.

Here maximum 2 thieves can be caught, first

policeman catches first thief and second police-

man can catch either second or third thief.

This problem can be solved by using greedy technique.

Always be yourself. Push hard and always explain approach before writing code. Keep talking during interview. Don't be silent for a while too.

Solution

53. Maximum Subarray

1class Solution(object): 2 def maxSubArray(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 8 # Kadane's Algorithm 9 max_so_far = float('-inf') 10 max_ending_here = 0 11 size = len(nums) 12 13 for i in range(size): 14 max_ending_here = max_ending_here + nums[i] 15 if (max_so_far < max_ending_here): 16 max_so_far = max_ending_here 17 18 if max_ending_here < 0: 19 max_ending_here = 0 20 21 return max_so_far 22 23

Policemen catch thieves

1def policeThief(arr, n, k): 2 i = 0 3 l = 0 4 r = 0 5 res = 0 6 thi = [] 7 pol = [] 8 9 # store indices in list 10 while i < n: 11 if arr[i] == 'P': 12 pol.append(i) 13 elif arr[i] == 'T': 14 thi.append(i) 15 i += 1 16 17 # track lowest current indices of 18 # thief: thi[l], police: pol[r] 19 while l < len(thi) and r < len(pol): 20 21 # can be caught 22 if (abs( thi[l] - pol[r] ) <= k): 23 res += 1 24 l += 1 25 r += 1 26 27 # increment the minimum index 28 elif thi[l] < pol[r]: 29 l += 1 30 else: 31 r += 1 32 33 return res


Comments