Microsoft Interview | Set 20 (On-campus for Internship)

Hello everyone, I recently got selected for the internship at Microsoft IDC. GeeksForGeeks helped me a lot in preparing for the coding tests as well as for interviews. The positions that they were interviewing for were SDE and SDET(Testing).

1st Round. – Online – 30 mins
The first round was an online round which was hosted on cocubes.com . There were 20 MCQ questions. Questions were from a variety of topics like Algorithms, Data structures, C, C++, OOPs, Memory Management, Computer Architecture etc.

2nd Round. – Online – 1 hour
This was a coding round in which two problems were given.

Problem – 1: Given a word and a text, return the count of the occurences of anagrams of the word in the text.
For eg. word is “for” and the text is “forxxorfxdofr”, anagrams of “for” will be “ofr”, “orf”,”fro”, etc. So the answer would be 3 for this particular example.

Problem – 2: Given a binary tree with parent pointers, find the right sibling of a given node(pointer to the node will be given), if it doesn’t exist return null. Do it in O(1) space and O(n) time.

3rd Round – Group Test – Code on paper – 45 mins
First we had to discuss the solution with the assigned mentor and then we had to code it on paper(no pseudo code)

Problem 1: Given a BST, one of the nodes violates the BST property( left-child < parent < right child ), return the pointer to that node. Problem 2: Two no.s having equal no. of digits were given in the form of singly linklist. (For eg.- 102 will be given as 1 -> 0 -> 2 ). Add these no.s and return the answer in the form of a singly linkllist. Do it in O(n) time, given n digit numbers

4th Round – Interview
I had to implement a T9 dictionary . insertWord function and populateWords were the two functions which I had to implement on paper(again no pseudo code).

First I have to dicuss the approach with the interviewer and then he asked me to code.

5th Round – Interview
He dicussed the work that I did in my last summer internship and after that he asked me to code the maximum subarray problem in one pass.

I and my friends who got internships at Adobe Research, Amazon, De-Shaw, Samsung would like to thanks GeeksForGeeks for mainting a high quality problem set for coding tests and interviews.

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.





  • the_c0der

    Can anyone tell how to solve this :
    Given a binary tree with parent pointers, find the right sibling of a given node(pointer to the node will be given), if it doesn’t exist return null. Do it in O(1) space and O(n) time.

  • http://techiegenie.blogspot.in/ barath vutukuri

    This is code for permutations…

    #include
    void swap(char *x,char*y)
    {
    char temp;
    temp=*x;
    *x=*y;
    *y=temp;
    }

    void permute(char *a,int i,int n)
    {
    int j;
    if(i==n)
    printf(“%s n”,a);
    else
    {
    for(j=i;j<=n;j++)
    {
    swap((a+i),(a+j));
    permute(a,i+1,n);
    swap((a+i),(a+j));
    }
    }
    }

    main()
    {
    char a[]= "for";
    permute(a,0,2);
    getchar();
    return 0;
    }

    In the permutations code,i will be checking if any permutation is substring of original string.If there is any,i will print that permutation.that string is obviously anagram…

    #include
    #include
    char originalstring[]=”forxxorfxdofr”;
    char *substring;
    void swap(char *x,char*y)
    {
    char temp;
    temp=*x;
    *x=*y;
    *y=temp;
    }

    void permute(char *a,int i,int n)
    {
    int j;
    if(i==n)
    {
    substring=strstr(originalstring,a);
    if(substring)
    {
    printf(“%sn”,a);
    }
    }

    else
    {
    for(j=i;j<=n;j++)
    {
    swap((a+i),(a+j));
    permute(a,i+1,n);
    swap((a+i),(a+j));
    }
    }
    }

    main()
    {
    char a[]= "for";
    permute(a,0,2);
    getchar();
    return 0;
    }

    • Thomas

      easy code for anagrams i have seen ever….

  • Shreevardhan

    int find_anagrams(char *word, char *text) {
    int bin[256] = {0}, mismatch = 0, found = 0, len = 0, c, i;
    for(i = 0; word[i] != ”; i++) {
    c = word[i];
    if(bin == 0) mismatch++;
    bin–;
    len++;
    }
    for(i = 0; text[i] != ”; i++) {
    c = text[i];
    if(bin == 0) mismatch++;
    if(bin == -1) mismatch–;
    bin++;
    if(i >= len) {
    c = text[i – len];
    if(bin == 0) mismatch++;
    if(bin == 1) mismatch–;
    bin–;
    }
    if(mismatch == 0) found++;
    }
    return found;
    }

    • Voldi

      Can you please explain the code above?
      Not able to dry run it properly.

  • Sergio Arturo

    I got confused in the second problem of the second round mainly because of English (so silly :P). By right sibling, does it mean, the correct sibling (by this, if the passed node was the left child , return right or if it was right, return left) or does it mean the sibling from the right (so if passed node was the left child, return right and if it is already right, I sould return null, am I correct?).

  • Django

    can u explain with an example, what are we suppose to do in ‘siblings’ problem?

  • Jitendra khushwaha

    was there any bound upon choosing the language for 2nd and 3rd round. I’m if someone like to write program in other language than C, like java,python or C# etc.. was it allowed?

  • Arpit

    could someone explain what input is given in Coding problem-2? Do we have left and right child pointers?

  • Nishchay Kala

    Code for ‘Counting Anagram’ Question in C language.

    #include
    #include
    int count_anagrams(char* str, char* text) {
    int count[26]= {0}, countT[26]={0}, i = 0, len = 0, lenT = 0, ans = 0, front = -1, rear = -1;
    while(*(str+len))
    len++;
    while(*(text+lenT))
    lenT++;
    for(i=0; i<len; i++)
    count[*(str+i) - 'a']++;
    for(i=0; i<lenT; i++) {
    front++;
    countT[*(text+front) - 'a']++;
    if(count[*(text+front) - 'a'] == countT[*(text+front) - 'a']) {
    if(front-rear == len) {
    ans++;
    rear++;
    countT[*(text+rear) - 'a']--;
    }
    }
    else {
    while(*(text+rear+1) != *(text+front)) {
    rear++;
    countT[*(text+rear) - 'a']--;
    }
    rear++;
    countT[*(text+rear) - 'a']--;
    }
    }
    return ans;
    }
    //driver function
    int main(void) {
    printf("%d n", count_anagrams("for", "forofro"));
    return 0;
    }

  • Kuldeep Kumar

    Complexity O(m*n) where m is length of string n is length of pattern

    #include
    #include
    #include
    using namespace std;
    int check_anagram(string str1,string str2,int i){
    int j=0;
    map M;
    while(str1[i]){
    M[str1[i]]++;
    i++;
    }

    while(str2[j]){
    M[str2[j]]--;
    if(M[str2[j]]<0)
    return 0;
    j++;
    }
    return 1;
    }
    int countAnagram(string str,string str1){
    string temp;
    int i=0,count=0,flag=0;
    int len1 = str.size();
    int len2 = str1.size();

    while ( str [ i ] ) {
    temp=str.substr(i,len2);
    flag=check_anagram(temp,str1,0);
    if(flag==1){
    count++;
    break;
    }
    else
    i++;
    }
    int k=1;
    while(i+len2<len1){
    flag=0;
    temp.insert (temp.end(),1,str[i+len2]);
    flag=check_anagram(temp,str1,k);
    if(flag==1)
    count++;
    i++;
    }
    return count;
    }

    int main(){

    string str = "rmarmrarm" , str1 = "ramr";
    cout << countAnagram ( str , str1 ) << "n" ;
    return 0;

    }

  • Guest

    Sample Java Program

    public class AddLists {
    public AddLists() {
    super();
    }

    public static void main(String[] args) {
    AddLists mergeList = new AddLists();
    AddLists.CustomLinkedList list1 = mergeList.new CustomLinkedList();
    for (int i = 4; i <= 7; i++) {
    list1.insert(9);
    }
    AddLists.CustomLinkedList list2 = mergeList.new CustomLinkedList();
    for (int i = 7; i <= 9; i++) {
    list2.insert(9);
    }
    list1 = mergeList.add(list1, list2);
    list1.traverse();

    }

    public CustomLinkedList add(CustomLinkedList list1, CustomLinkedList list2) {
    int len1 = list1.getLength();
    int len2 = list2.getLength();
    LinkedNode head1 = list1.getHead();
    LinkedNode head2 = list2.getHead();
    int d = Math.abs(len1 - len2);
    if (len1 > len2) {
    for (int i = 0; i < d; i++) {
    head1 = head1.getNext();
    }
    while (head1 != null) {
    head1.setData(list1.add(head1, head2));
    head1 = head1.getNext();
    head2 = head2.getNext();
    }
    //list1.traverse();
    } else {
    for (int i = 0; i len2 ? list1 : list2;
    list1 = list1.reverse(list1);
    list1.traverse();
    int c = 0;
    int i = 0;
    head1 = list1.getHead();
    while (head1 != null && head1.getNext()!=null) {
    i = head1.getData();
    head1.setData((i + c)% 10);
    c = (i + c)/10;
    head1 = head1.getNext();
    }
    head1.setData(head1.getData()+c);
    //list1.traverse();
    list1 = list1.reverse(list1);
    return list1;
    }

    class CustomLinkedList {
    private LinkedNode head;
    private LinkedNode tail;

    public Integer add(LinkedNode item1, LinkedNode item2) {
    return item1.value(item1.getData()) + item2.value(item2.getData());
    }

    public void insert(T data) {
    LinkedNode item = new LinkedNode(data);
    if (this.getHead() == null) {
    this.setHead(item);
    this.setTail(item);
    return;
    }
    this.getTail().setNext(item);
    this.setTail(item);
    }

    public void traverse() {
    LinkedNode head = this.getHead();
    while (head != null) {
    System.out.print(head.getData() + "t");
    head = head.getNext();
    }
    System.out.println("n");
    }

    public CustomLinkedList reverse(CustomLinkedList list) {
    LinkedNode head = list.getHead();
    LinkedNode prev = null;
    LinkedNode temp = null;
    while (head != null) {
    temp = head;
    head = head.getNext();
    temp.setNext(prev);
    prev = temp;
    }
    list.setHead(prev);
    return list;
    }

    public int getLength() {
    int i = 0;
    LinkedNode head = this.getHead();
    while (head != null) {
    i++;
    head = head.getNext();
    }
    return i;
    }

    public void setHead(LinkedNode head) {
    this.head = head;
    }

    public LinkedNode getHead() {
    return head;
    }

    public void setTail(LinkedNode tail) {
    this.tail = tail;
    }

    public LinkedNode getTail() {
    return tail;
    }
    }

    class LinkedNode {
    private LinkedNode next;
    private T data;

    public LinkedNode(T data) {
    this.setData(data);
    }

    public int value(T data) {
    return data instanceof Integer ? Integer.parseInt(data.toString()) : 0;
    }

    public void setNext(LinkedNode next) {
    this.next = next;
    }

    public LinkedNode getNext() {
    return next;
    }

    public void setData(T data) {
    this.data = data;
    }

    public T getData() {
    return data;
    }
    }
    }

  • Guest


    import java.util.HashMap;

    public class MicrosoftAnagrams {
    public MicrosoftAnagrams() {
    super();
    }

    public static void main(String[] args) {
    MicrosoftAnagrams microsoftAnagrams = new MicrosoftAnagrams();
    String sourceStr = "for";
    String destStr = "foraaarbbrofggrxfyoxnnorfmmorfmrxorppzfor";
    System.out.println(microsoftAnagrams.getASize(sourceStr.toCharArray(), destStr.toCharArray()));
    }

    public int getASize(char[] source, char[] destination) {
    HashMap map = new HashMap();
    for (int i = 0; i < source.length; i++) {
    map.put(source[i], 0);
    }
    int count = 0;
    int j = 0;
    for (int i = 0; i <= destination.length - source.length; i++) {
    if (map.get(destination[i]) != null) {
    count++;
    for (j = i; j 0)
    count--;
    break;
    }
    }
    i = j;
    }
    }
    return count;
    }
    }

  • Guest

    code for question1 2nd round

    #include

    int equal(int a[],int b[]);
    int find(char *s,char *w)

    {

    int t[26]={0};

    int k[26]={0};

    int sum=0;

    int i=0;

    int wlen=strlen(w);

    while(w[i])

    {

    t[w[i++]-‘a’]++;

    }

    i=0;

    while(i<wlen)

    {

    k[s[i++]-'a']++;

    }

    if(equal(t,k))

    printf("n%dn",0);

    for(i=1;i<=strlen(s)-wlen;i++)

    {

    int j=0;

    k[s[i-1]-'a']–;

    k[s[i+wlen-1]-'a']++;

    if(equal(k,t))

    {

    printf("n%dn",i);

    }

    }

    }

    int equal(int a[],int b[])

    {

    int flag=1;

    int i=0;

    for(;i<26;i++)

    {

    if(a[i]!=b[i])

    {

    return 0;

    }

    }

    return 1;

    }

    int main()

    {

    char *a="abcbac";

    char *w="abc";

    find(a,w);

    }

  • guest

    There was some posting the code in the previous comment so i’ve posted it at codepad…Improvements to the code are invited

    http://codepad.org/32f8QnOJ

  • Silent_hacker

    I also wrote a code working in O(N*K) time where K is constant time taken for array comparison….http://ideone.com/lmuOAs

  • guest

    Bellow is my code for the anagram problem, but somehow i’m not convinced with it.It seems unoptimal.Any suggestions to modify…..

    #include
    #include

    void permutation()__attribute__((constructor));

    void swap(char *a,char *b)
    {
    char temp;
    temp=*a;
    *a=*b;
    *b=temp;
    }
    int ctr=0;
    char s[100][100];

    string STR=”forxxorfxdofr”;

    void func(char *str,int n2)
    {
    char s[100];int flag=0;
    int n=STR.size();
    for(int i=0;i<n;i++)
    {
    int k=0;
    memcpy(s,(STR.substr(i,n2)).c_str(),sizeof((STR.substr(i,n2)).c_str()));

    for(k=0,flag=0;k<n2;k++)
    if(s[k]!=str[k])
    {
    flag=1;break;
    }
    if(flag==0)
    {
    ctr++;
    cout<<(STR.substr(i,n2))<<"t"<<str<<"t"<<ctr<<"n";
    }

    }
    }

    void permute(char *str,int i,int n)
    {
    if(i==n)
    {

    func(str,n+1);
    }
    else
    {
    for(int j=i;j<=n;j++)
    {
    swap((str+i),(str+j));
    permute(str,i+1,n);
    swap((str+i),(str+j));
    }
    }
    }

    void permutation()
    {
    char str[]="for";
    int n=strlen(str);
    permute(str,0,n-1);
    }

    int main(){cout<<ctr;}

    • guest

      #include
      #include

      void permutation()__attribute__((constructor));

      void swap(char *a,char *b)
      {
      char temp;
      temp=*a;
      *a=*b;
      *b=temp;
      }
      int ctr=0;
      char s[100][100];

      string STR=”forxxorfxdofr”;
      int n1=sizeof(STR)/sizeof(char);
      void func(char *str,int n2)
      {
      char s[100];int flag=0;
      int n=STR.size();
      for(int i=0;i<n;i++)
      {
      int k=0;
      memcpy(s,(STR.substr(i,n2)).c_str(),sizeof((STR.substr(i,n2)).c_str()));
      //cout<<"s : "<<s<<"n";
      for(k=0,flag=0;k<n2;k++)
      if(s[k]!=str[k])
      {
      flag=1;break;
      }
      if(flag==0)
      {
      ctr++;
      cout<<(STR.substr(i,n2))<<"t"<<str<<"t"<<ctr<<"n";
      }

      }
      }

      void permute(char *str,int i,int n)
      {
      if(i==n)
      {
      //cout<<"p : "<<str<<"n";
      func(str,n+1);
      }
      else
      {
      for(int j=i;j<=n;j++)
      {
      swap((str+i),(str+j));
      permute(str,i+1,n);
      swap((str+i),(str+j));
      }
      }
      }

      void permutation()
      {
      char str[]="for";
      int n=strlen(str);
      permute(str,0,n-1);

      cout<<ctr;

      }

      int main(){}

  • guest

    hey what was there in ROUND 3 :Group test????what to discuss with the project mentor..???

  • rahul23

    @3fe6a3f39c07ce276924aff5dcb639cd:disqus

    Please tell how u implemented the t9 dictionary..my approach to create a trie aith digits where each node will have 0-9 digits.then if we want to insert any word then we will match the corresponding digit for each character and insert into trie ..and to search we can track till the digits and then we can maintain a linked list at every leaf to store top words to be displayed in case of multiple words matching same digits combinations….

    please let me know ur approach..

    thnx

  • Harshil Lodhi

    The interview was held 2 weeks ago and the online rounds were held 3 weeks ago.

    • eric

      hi…
      can you plz tell me the logic of 2nd problem of 2nd round(right child of the node).
      I mean what we have to do in DFS (in-order or preorder or post order)

  • shilpa

    hey can some one tell me how to hit that anagram problem !!! plz!! thanks in advance !! :)

    • Priyank

      Here is a self explanatory code for it..

      #include

      void permutation(char* a,char* b){

      char *ap,*bp,*temp;

      int s1[26]={0},s2[26]={0},i,index=0,lengthb=0,pg=1,len;

      ap=a;

      bp=b;

      while(*bp!=”){

      s2[*bp-‘a’]++;

      bp++;

      lengthb++;

      }

      bp=a;

      len=lengthb;

      while(len–){

      s1[*bp-‘a’]++;

      bp++;

      }

      while(pg){

      for(i=0;i<26;i++){

      if(s1[i]!=s2[i]){

      i=30;

      break;

      }

      }

      if(i<30){

      printf("%d ",index);

      }

      s1[*ap-'a']–;

      s1[*bp-'a']++;

      ap++;

      bp++;

      index++;

      if(*bp=='') pg=0;

      }

      }

      int main(){

      char a[500],b[500];

      scanf("%s %s",a,b);

      permutation(a,b);

      return 0;

      }

  • Guest

    Hey , When was this interview ?

  • Anonymus

    Hey , When was this interview?

  • Priyank

    hi, I recently got intern at De-Shaw, may i know which college campus are you talking about ?

    • Harshil Lodhi

      ^ IIT Guwahati. Would you like to get in touch with the DE- Shaw interns from our campus?

      • Priyank

        yup.. surely !!! 😀

        • Silent_hacker

          Hii Harshil & Priyank..Can you please give a linear time algo for the first anagrams problem

          • Sourav

            Let’s say you are searching for anagrams of “for”-k characters. and looking for it in a string A of length n.

            First make a hash table and insert all the characters of “for”, this is O(k).

            Maintain a queue of k characters in the string, containing characters from A[i] to A[i+k-1].also maintain a count of no. of characters in queue, which are the same as “for”.

            Each iteration: O(1)
            1)increase i,
            2) delete last char from queue, add one more character.
            3) find if new character present in hash table.
            change count variable accordingly.

            No, of times that (count==k) during the iteration is no. of anagrams

      • pratyay

        hey dude, can you please tell me whether both the mcq round and coding round were on cocubes.com……thanks in advance

        • Priyank

          yup..both rounds for MS were on cocubes.com