Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

You just need to write a recursive method to print the ArrayList

void Print(ArrayList list)
{
    if(list==null || list.Count==0) return;

    for(int i=0;i<list.Count;i++)
    {
        if(list[i] is ArrayList)
            Print((ArrayList)list[i]);
        else
            Output(list[i]);
    }
}

Method Output() is used to print element to screen.

- g233007 September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about an ArrayList [1,2,3] : 2 is also having an Array List of 2 elements while 3 is having 3 more elements..Can you print those out?

- Anie September 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can write an extension method for arraylists, like ToStringX() [unfortunately you cannot override its ToString - at least I think so!]
But this one should work as cl.ToStringX()

public static string ToStringX(this ArrayList array)
        {
            string s = "[";
            foreach (object obj in array)
            {
                ArrayList inner = obj as ArrayList;
                if (inner != null)
                {
                    s += inner.ToStringX() + " ";
                }
                else
                {
                    s += obj.ToString() + " ";
                }
            }
            return s+"]";
        }

- Selmeczy, Péter September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

foreach (ArrayList i in al)
{
for (int k = 0; k < i.Count ;k++ )
{
Console.Write(i[k]);
}
}

- Annonymous November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

ArrayList al = new ArrayList();
ArrayList bl = new ArrayList();
ArrayList cl = new ArrayList();
al.Add(2);
al.Add(1);
bl.Add(3);
bl.Add(4);

cl.Add(al);
cl.Add(bl);

foreach (ArrayList a in cl) {
foreach (int k in a) {
Console.WriteLine(k);
}

}

- einstein September 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Definitely the interviewers intention is to create a circular loop and test how interviewee overcome this problem.

Regular loop breaking tactics can be used here in the above program

- R September 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

add(2)

- Anonymous September 17, 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