Microsoft Interview Question for Software Engineer in Tests


Country: United States




Comment hidden because of low score. Click to expand.
2
of 2 vote

functionality for operator << can be written as in C++

class Stringlib {
    string str;

    public:

    Stringlib(string s) {
        str = s;
    }

    friend const char* operator<< (Stringlib& data, int num);

};

const char* operator<< (Stringlib& d, int num) {

    num = num % d.str.size();
    string str =  d.str.substr(d.str.size()-num).append(d.str.substr(0,d.str.size()-num));
    cout << str << endl;
    return str.c_str();
}
int main() {
    Stringlib data("Microsoft");
    //data << 3;
    printf("%s\n", data << 3);
    return 0;
}

rest three operators follow in similar fashion

- jitendra.theta April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

For <<, >>, it works like the following:
<<X
1.Reverse the whole string first.
2.Reverse the first X substr.
3.Reverse the last length-X substr.
For example:
Microsoft << 2:
1. tfosorciM
2. First 2 chars, ftosorciM
3. Rest substr, ftMicroso;

The >>X operation works the same way except the reverse the Length-X chars first and than X chars.

- Mem March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Oper
{
    ...
    public string Value { get; private set; }
}

public static string operator <<(Oper lh, int rh)
{
    ...
    int len = lh.Value.Length - rh;
    return lh.Value.Substring(len) + lh.Value.Substring(0, len);
}

Oper x = new Oper("Microsoft");
string t = x << 2;
output = ftMicroso

- Anonymous March 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you try sending an empty string ?

- Sunil March 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Being a fairly easy question overall, in this case, I think the examiner would focus more on technical aspects. E.g. did you cover corner case, or NULL string, or syntax for overloading an operator

<< or >> can be implemented using any of the two approaches mentioned above:
a) Reverse entire string and then reverse parts of it
b) Get a relevant substring at starting pos and append remaning string. Would require extra space

= can be implemented using a simple strncpy or memcpy
== is strncmp

- puneet.sohi March 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *begin, char *end)
{
  char temp;
  while (begin < end)
  {
    temp = *begin;
    *begin++ = *end;
    *end-- = temp;
  }
}
void CircularRotate(char*str, int k)
{
 char*end=str+strlen(str)-1;
 reverse(str,end);
 char*kth=str+k-1;
 reverse(str,kth);
 str=str+k;
 reverse(str,end);
}

- mitesh April 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A sample for << using C#

public static string operator <<(Operator op, int num)
{
    int len;
    if (string.IsNullOrWhiteSpace(op.Str) || num >= op.Str.Length)
    {
        len = 0;
    }
    else
    {
        len = op.Str.Length - num;
    }

    return op.Str.Substring(len) + op.Str.Substring(0, len);
}

public class Operator
{
    public string Str { get; set; }
}

- Eric Lei April 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String StrShift(String str, int num)
{
    String newStr = str + str;
    return newStr.substring(num, num + str.length());
}

- Maddy May 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python solution

import sys

class String(object):
  def __init__(self, string):
    if string is not None:
      self.strlen = len(string)
      self.string = list(string)
      self.count = 0
    else:
      self.string = None

  def __lshift__(self, expected):
    if (expected < 0 or
        expected > sys.maxint or
        type(expected) != int):
      return 'Invalid expected value passed'
    elif expected == 0:
      return self.string
    elif self.string is None:
      return None
    elif self.strlen in [1,0]:
      return ''.join(self.string)

    while self.count != expected:
      prev = self.string[self.strlen-1]
      for i in range(self.strlen-2, -1, -1):
        next = self.string[i]
        self.string[i] = prev
        prev = next
      else:
        self.string[self.strlen-1] = prev
      self.count += 1
    return ''.join(self.string)

  def __rshift__(self, expected):
    if (expected < 0 or 
        expected > sys.maxint or 
        type(expected) != int):
      return 'Invalid expected value passed'
    elif expected == 0:
      return self.string
    elif self.string is None:
      return None
    elif self.strlen in [1,0]:
      return ''.join(self.string)

    while self.count != expected:
      prev = self.string[0]
      for i in range(1, self.strlen):
        next = self.string[i]
        self.string[i] = prev
        prev = next
      else:
        self.string[0] = prev
      self.count += 1
    return ''.join(self.string)

print String('abcb') >> 1

- Galileo December 02, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More