Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Recursive call with swapping elements before and after.

public static int length(int[] s, int n) {
		int result = 0;
		if (s[n] != n) {
			int k = s[n];
			s[n] = s[k];
			s[k] = k;
			result = length(s, n) + 1;
			s[k] = s[n];
			s[n] = k;
		}
		return result;
	}

- artak.a.petrosyan November 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution helped me understand the questions..thanks mate

- somecup December 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont get this !
how are you passing the array to the function ?
int[] is not a valid argument type !

- ansh93 February 05, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

task is written not clear. Could you improve the description?

- zr.roman November 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

modified - can you let me know which part you have confusion - goal is to find number of steps to find given value in array - for example say find me value 4 in array which is described - you start with a[4] which gives you 2 now you look for a[2] which equals 0 then a[0] which equals 5 then a[5] which equals 3 and a[3] which eventually equals 4 - which took 5 steps

- idforbc November 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

c# implementation.

using System;

namespace NumberOfSteps {

    class Program {

        private static int process(int[] arr, int value, ref int i, int initVal) {

            if ( arr[ value ] != initVal) {
                i++;
                process( arr, arr[value], ref i, initVal );
            }
            return i;
        }

        private static int length(int[] arr, int val) {
            int i = 1;
            return process( arr, val, ref i, val );
        }      

        static void Main(string[] args) {

            var numOfSteps = length(new int[] {5, 1, 0, 4, 2, 3}, 4);

            Console.WriteLine( numOfSteps );

            Console.ReadLine();
        }
    }
}

- zr.roman November 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think it satisfies the conditions/restrictions

- idforbc November 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what conditions/restrictions are violated?

- zr.roman November 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh, you updated the initial restrictions! ok.

- zr.roman November 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

private static int length(int[] arr, int value)        {

            int count = 1;
            int index = 0;
            m1:
            index = count == 1 ? value : arr[index];
            if ( arr[index] != value ) {
                count++;
                goto m1;
            }
            return count;
        }

or add restriction - not allowed to use goto.

- zr.roman November 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void run()
{
int[] s = { 5, 1, 0, 4, 2, 3 };

int steps = length(s, 3);

}

static int length(int[] s, int k)
{
int pointer = (int) System.Math.Floor(System.Math.Log10(k));
int kval = (int)(k / (System.Math.Pow(10, pointer)));

if (s[pointer] == kval)
{
return pointer;
}
else
{
return length(s, k * 10);
}

}

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void run()
        {
            int[] s = { 5, 1, 0, 4, 2, 3 };

            int steps = length(s, 3);

        }

        static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }
            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }
            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int
length(int *data, int value)
{
    if (data[0] == value)
        return 1;

    return 1 + length(data+1,value);
}

- introspec November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)

{

int pointer = (int) System.Math.Floor(System.Math.Log10(k));

int kval = (int)(k / (System.Math.Pow(10, pointer)));

if (s[pointer] == kval)

{

return pointer;

}

else

{

return length(s, k * 10);

}

}

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cassert>

using namespace std;

int s[] = {5,1,0,4,2,3};

int
length2(int *data, int value)
{
    if (data[0] == value) 
	return 1;

    return 1 + length2(data+1, value);
}


int 
length(int *data, int value)
{
    int max_idx = value;
    int steps = 0;

    for (int i = 0; i <= max_idx; ++i) {
	max_idx = max(data[i], max_idx);
	++steps;
	if (data[i] == value)
	    return steps;
    }

    assert(0);
    return 0;
}





int
main(int argc, char **argv)
{
    cout << "Result: " << length(s, 5) << endl;
    cout << "Result: " << length2(s, 5) << endl;
    return 0;
}

- introspec November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

CareerCup

Questions


Forum


Salaries


Resume Tips


RSS


Sign In







Writing code on white board/paper.

Forum Post 1 Answers Writing code on white board/paper.

I am looking for a job switch and hence preparing for my interviews. Now working in an organization, I am very much used to IDE's. I have given a few online interviews and I find it difficult to write the code without using any IDE. Even writing down a simple program of linked list. Also usually the interviewers doesn't allow you to write the code using an IDE first and then paste it online.

How do you guys practice for such interviews? Do you practice the code on paper first?
- ACE CA about a year ago | Flag











Email me when people comment.

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }            
        }

- junpark November 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int length(int[] s, int k)
        {
            int pointer = (int) System.Math.Floor(System.Math.Log10(k));
            int kval = (int)(k / (System.Math.Pow(10, pointer)));

            if (s[pointer] == kval)
            {
                return pointer;
            }
            else
            {
                return length(s, k * 10);
            }            
        }

- junpark November 27, 2015 | 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