Road Patch - Microsoft

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-10-24Added By Anonymous
Source: Link

I recently took one interview at microsoft where I was asked with this question.

There is 2 lane road, each lane is represented by a String, l1 represents the first lane and l2 represents the second lane. A given lane is made up of N segments. A given segment can be either smooth or can have pothholes. If we consider these 2 strings, l1 = "..XX.X" and l2 = "X.X.X..". Here "." represents smooth road and "X" represents the pathhole.

A car can be driven through pathhole segment but it is just uncomfortable. Now the task is to patch maximum no of the pothholes while keeping the road open for travel.

Example 1 : l1 = "..XX.X" and l2 = "X.X.X..", output : 4

Road visualization === Path A car can take while patching potholes (* represents the segment selected) === (X) Pothholes that can be patched.

| . | . | X | X | . | X | ======== | * | * | - | - | - | - | ========== | . | . | (X) | (X) | . | (X) |

| X | . | X | . | X | . | ======== | - | * | * | * | * | * | ========== | (X) | . | X | . | X | . |


If you people are finding difficulty above example to digest , I can prepare and share visually appealing diagrams representing the examples.


I used a recursive-backtracking approach to solve the problem. (Total no of pothhole) - (min. no of pothholes in an optimal path) gives the answer.

Comments