Microsoft Interview Question for Software Engineer in Tests






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

<pre lang="" line="1" title="CodeMonkey11544" class="run-this">public class CommentStripper {

public static void main(String[] args) {
final int CODE = 0; // parsing normal code
final int SLASH = 1; // found a leading '/'
final int BLOCK = 2; // in a block c-style comment
final int LINE = 3; // in a line comment
final int STAR = 4; // found a trailing * in a block comment
final int QUOTE = 5; // in a quote
final int ESCAPE = 6; // found the escape character (i.e., \)
int state = CODE; // current state
final char EOL = System.getProperty("line.separator").charAt(0); // End-of-line
// constant
final int EOF = -1; // End-of-file constant
int whatever; // current read char
while ((whatever = StdInput.readChar()) != EOF) {
char c = (char) whatever;
switch (state) {
case CODE:
if (c == '"') {
state = QUOTE;
System.out.print(c);
} else if (c == '/') {
state = SLASH;
} else {
System.out.print(c);
}
break;
case SLASH:
if (c == '*') {
state = BLOCK;
} else if (c == '/') {
state = LINE;
} else {
state = CODE;
System.out.print("/" + c);
}
break;
case BLOCK:
if (c == '*') {
state = STAR;
}
break;
case STAR:
if (c == '/') {
state = CODE;
System.out.print(" ");
} else if (c == '*') {
state = STAR;
} else {
state = BLOCK;
}
break;
case LINE:
if (c == EOL) {
state = CODE;
System.out.println();
}
break;
case QUOTE:
if (c == '"') {
state = CODE;
System.out.print(c);
} else if (c == '\\') {
state = ESCAPE;
System.out.print(c);
} else {
System.out.print(c);
}
break;
case ESCAPE: {
state = QUOTE;
System.out.print(c);
}
}
}
}
}

class StdInput {
public static int readChar() {
try {
return System.in.read();
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
</pre><pre title="CodeMonkey11544" input="yes">
</pre>

- Anonymous August 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This Java code implements a finite-state machine.
Dharma

- x August 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you must use file input-output. currently it will keep reprinting whatever you write.

- thinker August 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey50468" class="run-this">Just basic /**/ and // operations. Didn't handle other operations such as comments inside string or printf

void LexicalAnalyzer(string path)
{
string line;
char* code_file = (char*)malloc(1000*sizeof(char));
int len = 0;
int j = 0;
int comment_index = 0;
std::ifstream myfile (path.c_str());
while(myfile.good())
{
getline(myfile, line);
for(int i = 0; i < line.size(); i++)
{
code_file[j] = line[i];
j++;
len++;
}
}
int len_variable = len;
for(j = 0; j < len_variable; j++)
{
if(code_file[j] == '/' && code_file[j + 1] == '*')
{
comment_index = j;
while(!(code_file[j] == '*' && code_file[j + 1] == '/'))
{
j++;
}
memcpy((char*)code_file + comment_index, (char*)code_file + j + 2, len - j);
len_variable = len_variable -(j + 2 - comment_index);
j = 0;
}
if(code_file[j] == '/' && code_file[j + 1] == '/')
{
comment_index = j;
while(code_file[j] != '\r')
{
j++;
}
memcpy((char*)code_file + comment_index, (char*)code_file + j + 2, len - j);
len_variable = len_variable -(j + 2 - comment_index);
j = 0;
}
}
code_file[len_variable] = NULL;
}</pre><pre title="CodeMonkey50468" input="yes">
</pre>

- Anonymous October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The approach seems simple. Read 2 chars at a time and write them on to the output buffer. If they turn out to be "//" then dont copy the rest of the chars until the end of line or newline char. If you find a "/" read the next char to determine what kind of comment it is if its really a comment. If they turn out to be "/*" discard all the chars until you see a */

- Anonymous December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dont join AdCenter, they are a bunch of jokers. Mostly maintanence work, no coding at all. A friend works there and left within 6 months to another company

- Anonymous February 26, 2012 | 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