Uber Round 1 DSA Problem

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2025-08-27Added By Anonymous
Source: Link

Company: Uber
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Find available parking space on a given day. Parking regions are consecutive from 0 to n.
Given an array where the ith element shows the parking region for the ith day.
Return an array of parking space for the ith day region which is unoccupied and can be used for parking.
park_region[] = {{3,5},{6,7},{8,9},{1,12}}
available_region[]={3,2,2,5}

vector<int> find_available_parking_space(vector<vector<int>>& park)
{
   map<int,int> mp;      //store the occupied region
   vector<int> ans;
   int n=park_region.size();
   if(n==0)
   {
      return {};
   }
   
   for(auto &p:park_region)
   {
      int total_region = (p[1]-p[0]+1);  //total available region
      int taken = 0;    //total occupied space in the available region
      int next = p[0];
      auto pt = mp.lower_bound(next);
      if(pt!=mp.begin())
      { 
        pt = prev(pt);
        if(pt->first>=next && next<=pt->second)
        {
            taken+=min(pt->second,p[1])-next + 1;
            next=min(pt->second,p[1])+1;
            mp[pt->first]=min(pt->second,p[1]);
        }
      }
      while(next<=p[1])
      {
        auto pt = mp.lower_bound(next);
        if(next>=pt->first && next<=pt->second)
        {
            taken+=min(pt->second,p[1])-next+1;
            next = min(pt->second,p[1])+1;
            auto it = mp.lower_bound(next-1);
            if(it!=mp.end() && it->first>=(next-1) && 
it->second<=(next-1))
            {
                mp[it->first]=min(pt->second,p[1]);
            }
        }
        else
        {
            break;
        }
      }
      ans.push_back((total_region-taken));
   }
   return ans;
}

I was able to come up with the approach but could not able to code it. Messed up too much at the edge cases.
I hope this solution covers all the edge cases.

Comments