Microsoft Bangalore Engineering Manager Interview [23/Mar/2022] [Rejected]

Interview Date: 23/2/2022.

Please upvote if you like to make Article reach wider audience

QUESTION-1. ListofList in C++

Interviewer does not gave any inputs, Just asked to implement. When I tried asking how does ListOfList internally look like, He does not answered and told its written in question(You should not implement it, or speculate about its implementation). Very vague question.

QUESTION:

  • Implement methods inside class ListIterator
/**
 * // This is the interface that allows for creating list of lists.
 * // You should not implement it, or speculate about its implementation
 * class ListOfList {
 *   public:
 *     // Return true if this ListOfList holds a single integer, rather than a list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this ListOfList holds, if it holds a single integer
 *     // The result is undefined if this ListOfList holds a list
 *     int getInteger() const;
 *
 *     // Return the list that this ListOfList holds, if it holds a list
 *     // The result is undefined if this ListOfList holds a single integer
 *     const vector<ListOfList> &getList() const;
 * };
 */

class ListIterator {
public:
    ListIterator(vector<ListOfList> &listOfList) {
    }
    
    int next() {
    }
    
    bool hasNext() {
    }
};


/**
 * Your ListIterator object will be instantiated and called as such:
 * ListIterator i(listOfList);
 * while (i.hasNext()) cout << i.next();
 */


 Example (1)
Input: listOfList = [1,[4,[6]]]
Output: [1,4,6]

 Example (2)
Input: [1, [2, 3, [4, 5, []], 6], 7]
Output: [1, 2, 3, 4, 5, 6, 7]


My Answer

class ListIterator {
    vector<ListOfList> ll;
    vector<ListOfList>::iterator it = ll.begin();
public:
    //Input: [1, [2, 3, [4, 5, []], 6], 7]
    ListIterator(vector<ListOfList> &listOfList) {
        ll = listOfList;
    }
    
    int next() {
        if (hasNext()){
            for (auto i=it.begin(); i !=it.end(); ++i){
                //Check the single ll
                list<int>::iterator single& = *i;
                for (auto j=single.begin(); j!=single.end(); ++j){
                    return *j;
                }
            }
            //it = i;
        }
        return 0;
    }
    
    bool hasNext() {
        if it != ll.end()
            return true;
        return false
    }
};


Queries

  • 1. Is this a linked list inside linked list. Is this the data structure for it?
struct node {
  int a;
  struct node* another_list;
  struct node* next;
};
  • 2. What does {1,{2,{3,4,5,{6}}}}; represents?
                                4->5
             1 -> 2 -> 3,/\->6
  • 3. What's the correct answer.
  • 4. Looks n-way nested list creation problem.

QUESTION-2. Arrange/Sort Cities in proper order by Population

Interviewer was extremely friendly and helping whenever I was stuck, Kudos to him!!

Question:

  • Sort list of cities in right order. Give list of tuples.
  • Tuple represents (population_of_present_city, Number of cities on its left whose population >= this city's population)
  • Examples:
Example-1
        a          b        c         d         e
Input: [8000,1], [5000,3], [8000,0], [6000,2], [6000,0]
    city          a       b     c     d       e
    population:   8000    5000  8000  6000   6000
    //There should be:
      1 cities to left of city=a, whose population >= 8000.
      3 cities to left of city=b, whose population >= 5000
      0 city to left of city=c, whose population >= 8000
      2 city to left of city=d, whose population >= 6000      
      0 cities to left of city=e, whose population >= 6000

Output: [6000,0] [8000,0] [6000, 2] [5000, 3] [8000, 1]

Example-2
        a         b       c         d
Input: [800,0], [700,0], [300,1], [600,2]
  0 cities to left of city=a whose population >= 800
  0 cities to left of city=b whose population >= 700
  1 city to left of city=c whose population   >= 300
  2 cities to left of city=d whose population >= 600

Output: [700,0][300,1][800,0][600,2]

Approach=Sorting using comparator

Logic

  • 1. Sort in decending order by population
input = [8000,1], [5000,3], [8000,0], [6000,2], [6000,0]

1a. sorted in desc order by population:
        [8000,1] [8000,0] [6000,2] [6000,0] [5000,3]
        
1b. if population is same, sort by second element
        [8000,0] [8000,1] [6000,0] [6000,2] [5000,3]
  • 2. Arrange tuples as per indexes obtained in step-1b
    [8000,0] [8000,1] [6000,0] [6000,2] [5000,3]
    
    Index of:
      [6000,0] should be 0.   [6000,0] [8000,0] [8000,1] [6000,2] [5000,3]
      [6000,2] should be 2.   [6000,0] [8000,0] [6000,2] [8000,1] [5000,3]
      [5000,3] should be 3.   [6000,0] [8000,0] [6000,2] [5000,3] [8000,1]
      
Ans: [6000,0] [8000,0] [6000,2] [5000,3] [8000,1]

Code

CPP

#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
using mpair = pair<int,int>;

bool comparator(pair<int,int> p1, pair<int,int> p2){
    if(p1.first == p2.first)
      return p1.second < p2.second;

    return p1.first > p2.first;
}

//i = {8000,1}, {5000,3}, {8000,0}, {6000,2}, {6000,0}
vector<mpair> fun(vector<mpair> input) {
    vector<mpair> o;

    sort(input.begin(), input.end(), comparator);

    //After sort
    //i = {8000,0}, {8000,1}, {6000,0}, {6000,2}, {5000,3}

    for (int i=0; i<input.size(); ++i)
        o.insert(o.begin()+input[i].second, input[i]);

    return o;
}

int main(){
    vector<pair<int,int>> i = {{8000,1}, {5000,3}, {8000,0}, {6000,2}, {6000,0}};
    vector<mpair> o = fun(i);
    for (auto j:o){
        cout << j.first <<"," << j.second;
        cout <<"\n";
    }
}
# ./a.out
6000,0
8000,0
6000,2
5000,3
8000,1

Article here also: https://github.com/amitkumar50/Code-examples/blob/master/Companies_Interviews/microsoft/Software_Development_Manager.md

Comments (0)