Byjus – whitehat junior : Online Technical Coding Interview Questions

I got called from instahyre for php developer.

Round 1 :

1.) Implement Explode functionality of php

<?php
                    
   $string = "I am |~| I am ....|~||~||~||~||~||~||~|[email protected]@#@#|~|@#@# fdsds |~|dfdf~ |~|";
   $deli = "|~|";  
  
    print_r(explode($deli , $string));
    print_r(myExplode($deli , $string ));

   function myExplode( $deli , $string ){
       
       $ans = array();
       $deliLen = strlen($deli);
       $index = 0;
       $temp ="";
       $subStr = "";
       $deliIndex = 0;
       for( $i = 0 ; $i < strlen($string) ; $i++){
             if( $string[$i] == $deli[0] ){
                 while($index < $deliLen && $i < strlen($string) &&  $string[$i] == $deli[$index]){
                     $temp .= $string[$i];
                     $index++;
                     $i++;
                 }
                 if($index == $deliLen){
                   $ans[] = $subStr;
                   $subStr = "";
                 }else{
                     $subStr .= $temp;
                 }
                 $temp = "";
                 $index = 0;   
             }
             else{
                 $subStr .= $string[$i];
             }
       }
         if(strlen($subStr) > 0){
                 $ans[] = $subStr;
             }     
       return $ans;
   }
  
?>

2.) Mysql Query to find
a.) ranks of given roll number based on marks . Same marks should be at same rank.
b.) kth Largest numner in the column , duplcate & non duplicate case both

  

select rolln from englis_test where marks = (select distinct marks from englis_test order by marks desc LIMIT 1,1  ); 

Round 2 : system Desgin

Design a scalable Cab finding system based on user and cab location. Given a list of cabs with their location, and user's current location, return a set of nearby cabs which lie within r km radius of the user.

Input:
A list of cabs with their location.
cabs = [[id1, x1, y1], [id2, x2, y2], [id3, x3, y3]...[idn, xn, yn]]
where xi, yi are x and y coordinates of the ith cab and idi is unique id of ith cab
User's current location
        userLocation = [xu, yu]
        where xu, yu are x and y coordinates of the user. 
Radius r denoting the radius of circle within which cab is considered as nearby


Design following methods:

// takes above defined inputs and returns a set of nearby cabs
Set<cabs> findNearbyCabs ( List<cabs>, Tuple userLocation, int r)

Requirements:
Above method should have minimum latency. The list of cabs is huge. Iterating all of them is not an option. 


// Takes in a cab and it's new location and updates the location
void updateCabLocation ( Cab cab, Tuple newCabLocation)

Requirements:
This method will be called a lot of times, hence latency should be minimum. 


// optional method
Void preprocessing ()


Test Cases:

Input:
cabs = [1, 0, 0], [2, 5, 10], [3, 5, 1000], [4, 1000, 2], [5, 3, 4], [6, -4, -5], [7, -2, 3]
userLocation = [0,0]
r = 5



Method calls:

findNearbyCabs(cabs, userLocation, r)

Output: [ 1, 5, 7]

updateCabLocation([3,5,1000], [1,1])
updateCabLocation([7,-2, 3], [1,100])

findNearbyCabs(cabs, userLocation, r)

Output: [ 1, 3, 5]



Class Cabs(){

   vector<vector<int>> cabX(n , vector<int>(2 , 0));
   vector<vector<int>> cabY(n , vector<int>(2 , 0));
     unorderd_map<int , vector<int>> m;

Void preprocessing (){

     Int index = 0;
foreach(vector<int> cab :  cabs){
      cabX[index][1][0] = cab[1];
      cabY[index][1][0] = cab[2];
      
vector<int> coordinates{cab[1] , cab[2]};
M[cab[0]] = coordinates;

cabX[index][1][1] = cabY[index][1][1] = cab[0];
 
index++;
}

sort(CabX);
sort(CabY);
}

  


vector<int> findNearbyCabs ( vector<int> &cabs, vector<int> userLocation, int r){
              
Int xUser = userLocation[0];
Int yUser = userLocation[1];
vector<int> ans ;
              Int xStartIndex = getStartIndex(cabX ,xUser-r  );
              Int xEndIndex = getIndex(cabX ,xUser+r);

          Int yStartIndex = getStartIndex(cabY ,yUser-r  );
              Int yEndIndex = getIndex(cabY ,yUser+r);

        for(int i =xStartIndex ; i <= xEndIndex ; i++  ){
                 
             Int cabId = cabX[i][1];
                        
           Int yIndex = m[cabId][1];
             if( yIndex <= (yUser+r)  && yIndex >= (yUser-r) ){
               ans.push_back(cabId);
}
        

}

Return ans;
}

void updateCabLocation ( int cabId, vector<int> newCabLocation){

       m[cabId] = newCabLocation;
       preprocessing();
}






// Online C++ compiler to run C++ program online
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
 class Cabs{
 public :
   vector<vector<int>> cabX;
   vector<vector<int>> cabY;
     
    map<int , vector<int>> m;
    vector<vector<int>> cabs ;
   
 void setCabs(vector<vector<int>> c){
   cabs = c;
   preprocessing();
   }

void preprocessing(){

     int index = 0;
for(vector<int> cab :  cabs){
      
      vector<int>x{cab[1] ,cab[0]  };
      vector<int>y{cab[2] , cab[0] };
      cabX.push_back(x);
      cabY.push_back(y);
     
      vector<int>  coordinates{cab[1]  , cab[2]  };
m[cab[0]] = coordinates;

cabX[index][1] = cabY[index][1] = cab[0];
 
index++;
}

sort(cabX.begin() , cabX.end() , [](vector<int> a, vector<int> b ){
    return a[1] <= b[1];
});

/*
sort(cabY.begin() , cabY.end() , [](vector<int> a, vector<int> b ){
    return a[2] <= b[2];
});
*/
}

  
vector<int> findNearbyCabs (  vector<int> userLocation, int r){
   return findCab(userLocation , r);
}


vector<int> findCab ( vector<int> userLocation, int r){
              
            int xUser = userLocation[0];
            int yUser = userLocation[1];
vector<int> ans ;
              int xStartIndex = 0 ;//getStartIndex(cabX ,xUser-r  );
              int xEndIndex = cabs.size();//getIndex(cabX ,xUser+r);

        for(int i =xStartIndex ; i < xEndIndex ; i++  ){
                 
            //     cout<<"1.."<<endl;
             int cabId = cabX[i][1];
              //          cout<<"2.."<<cabId<<endl;
           int yIndex = m[cabId][1];
           int xIndex = m[cabId][0];
           //cout<<"3.."<<yIndex<<endl;
           
           
           
             if( insideCircle(xUser , yUser , xIndex ,yIndex , r) ){
               ans.push_back(cabId);
}
        

}

for(int i : ans){
cout<<i<<"--";
}

return ans;
}

bool insideCircle(int a , int b , int c , int d , int r){
    return r*r >= ( (a -c)*(a -c) + (b-d)*(b-d));
}

void updateCabLocation ( int cabId, vector<int> newCabLocation){

       m[cabId] = newCabLocation;
       preprocessing();
}
};

int main() {
    vector<vector<int>> cabs{
        vector<int>{1, 0, 0},
        vector<int>{2, 5, 10}, 
        vector<int>{3, 5, 1000}, 
        vector<int>{4, 1000, 2},
        vector<int>{5, 3, 4},
        vector<int>{6, -4, -5}, 
        vector<int>{7, -2, 3}
    };
    Cabs c ;//
    c.setCabs(cabs);
    vector<int> userLocation{0,0};
    int r = 5;
    c.findCab(userLocation , r);
    return 0;
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error

Enjoy this blog? Please spread the word :)

0
Would love your thoughts, please comment.x
()
x