Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
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
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();
}
}
}
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);
}
}
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);
}
}
#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;
}
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);
}
}
Recursive call with swapping elements before and after.
- artak.a.petrosyan November 24, 2015