Microsoft Interview Question for Front-end Software Engineers


Country: United States
Interview Type: In-Person




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

The empty python script does the trick.

- Anonymous October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are self-replicating programs, self-reproducing programs, and self-copying programs. ( Wiki: en.wikipedia.org/wiki/Quine_(computing) )

Watch out for Quines here:
1. madore.org/~david/computers/quine.html
2. nyx.net/~gthompso/quine.htm

A complete Quine:

#include <stdio.h>

int
main (void)
{
  char *s1="#include <stdio.h>%c%cint%cmain (void)%c{%c";
  char *s2="  char *s%c=%c%s%c;%c  char *s%c=%c%s%c;%c";
  char *s3="  char n='%cn', q='%c', b='%c%c';%c";
  char *sp="  printf(";
  char *s4="%ss1,n,n,n,n,n);%c";
  char *s5="%ss2,'1',q,s1,q,n,'2',q,s2,q,n);%ss2,'3',q,s3,q,n,'p',q,sp,q,n);%c";
  char *s6="%ss2,'4',q,s4,q,n,'5',q,s5,q,n);%ss2,'6',q,s6,q,n,'7',q,s7,q,n);%c";
  char *s7="%ss2,'8',q,s8,q,n,'9',q,s9,q,n);%ss2,'0',q,s0,q,n,'x',q,sx,q,n);%c";
  char *s8="%ss3,b,q,b,b,n);%ss4,sp,n);%ss5,sp,sp,n);%c";
  char *s9="%ss6,sp,sp,n);%ss7,sp,sp,n);%ss8,sp,sp,sp,n);%c";
  char *s0="%ss9,sp,sp,sp,n);%ss0,sp,sp,n,n,n);%c  return 0;%c}%c";
  char *sx="--- This is an intron. ---";
  char n='\n', q='"', b='\\';
  printf(s1,n,n,n,n,n);
  printf(s2,'1',q,s1,q,n,'2',q,s2,q,n);  printf(s2,'3',q,s3,q,n,'p',q,sp,q,n);
  printf(s2,'4',q,s4,q,n,'5',q,s5,q,n);  printf(s2,'6',q,s6,q,n,'7',q,s7,q,n);
  printf(s2,'8',q,s8,q,n,'9',q,s9,q,n);  printf(s2,'0',q,s0,q,n,'x',q,sx,q,n);
  printf(s3,b,q,b,b,n);  printf(s4,sp,n);  printf(s5,sp,sp,n);
  printf(s6,sp,sp,n);  printf(s7,sp,sp,n);  printf(s8,sp,sp,sp,n);
  printf(s9,sp,sp,sp,n);  printf(s0,sp,sp,n,n,n);
  return 0;
}

- nimitagr October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note that this is not necessarily a quine question, because:
"Quines, per definition, cannot receive any form of input, including reading a file."
However, there is no such requirement in this question.

- ychen April 20, 2019 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

see "quine" in google

#include <stdio.h>
char str[] = "#include <stdio.h>%cchar str[] = %c%s%c;%cint main() { printf(str, 10, 34, str, 34, 10); return 0; }";
int main() { printf(str, 10, 34, str, 34, 10); return 0; }

- dk October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use

__FILE__

macro to open the file in read mode and print the contents of the file.

__FILE__ : This macro expands to the name of the current input file, in the form of a C string constant.

- Psycho October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <fstream>
#include <string.h>
//#include <stdio.h>

int main()
{
        std::ifstream fp(__FILE__, std::ifstream::in);
        std::string line;

        //FILE* fp = fopen(__FILE__, "r");

        if(fp == NULL)
        {
                std::cout << "fp = NULL" << std::endl;
                return 0;
        }
        else
        {
                //std::cout << "fp != NULL " << std::endl;
                while(!fp.eof())
                {
                        getline(fp, line, '\n');
                        for(int i=0; i<line.size(); i++)
                        {
                                char c = line[i];
                                std::cout << c;
                        }
                        std::cout << "\n";
                }
        }

        return 0;
}

- Bibhas Deb October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

/This is a mirror program which prints its source code ;-)
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int main()
{
// to read one line from file
string line;
// this is our source file which is going to be the input file
ifstream f (__FILE__);
// read until the last line and print each line
while (getline(f, line))
cout << line << endl;
return 0;
}

- cdreamer October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

NOTE: give the full path of the file to the compiler

- cdreamer October 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

key points:
1, use "printf" to keep the format
2, use ASCII code to replace "\n","\t" and quotation mark.

public class Quine
{
	public static void main(String [] argus)
	{
		String input = "public class Quine %c{%c%c public static void main(String [] argus) %c{%c%c String s = %c%s%c; %c System.out.printf(input, 10, 10, 9, 10, 10, 9, 34, input, 34, 10, 10, 10); %c } %c}";
		System.out.printf(input, 10, 10, 9, 10, 10, 9, 34, input, 34, 10, 10, 10);
	}
}

- Eugene Zhao October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

#define ENIUQ(TEMPLATE) cout << TEMPLATE << "(" << #TEMPLATE << ");}";

void main()
{
	ENIUQ("#include <iostream.h>\n#define ENIUQ(TEMPLATE) cout << TEMPLATE << \"(\" << #TEMPLATE << \");}\";\n\nvoid main()\n{ENIUQ");
    system("pause");
}

- kh.m.umerjavaid October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All are stupid logic

- All are stupid logic December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include <stdlib.h>
int main()
{
system("cat test.c");
}

- few lines code. December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def self.produceSourceCode
		file = File.open(__FILE__,"r")
		puts file.read
		file.close
	end

- rahul February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its pretty easy with Javascript. Just use .toString():

function printSourceCode(input){
    return input.toString();
}

// test it out!
console.log(printSourceCode(function incrementer(num){ num++; }));

- rahuldesai209 July 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A program that reads and prints its source.

- Noobie October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A program that reads its own source file?

- Noobie October 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This actually works but it can be considered cheating. :)
Imagine the case where the program does not know the location to its own source code (file).

- Ehsan October 15, 2013 | Flag


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