Microsoft Interview Question for Applications Developers


Country: India
Interview Type: In-Person




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

My indentation did not appear in the question.
Every inner brace should increase one indentation to the following lines.
Every close brace should decrease one indentation to the same line and the following lines.

- Naveen Reddy Mandadi August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
ideone.com/4371r
*/
import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String a = "{A:'B',C:{D:'E',F:{G:'H',I:'J'

";
int level = 0;
String p;
for(int i=0; i<a.length();i++)
{
char c = a.charAt(i);
String s = Character.toString(c);
if( s.equals("{"))
{
level++;
int count = 0;
System.out.print(s+"\n");
while(count<level)
{
System.out.print(" ");
count++;
}
}
else if(s.equals(","))
{
int count = 0;
System.out.print(s+"\n");
while(count<level)
{
System.out.print(" ");
count++;
}
}

else if( s.equals("}"))
{level--;
int count = 0;
System.out.print("\n");
while(count<level)
{
System.out.print(" ");
count++;
}
System.out.print(s);

}
else
{
System.out.print(s);
}
}
}
}

- am15851 August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printJSON(String input, StringBuffer spaces, boolean useSpaces)
    {
       char printChar= input.charAt(0);
       System.out.print(useSpaces?spaces.toString()+printChar:printChar);
       
       if(input.length()<=1)
           return;
       
       useSpaces= true;
       
       switch(printChar)
       {
           case ',':
               System.out.print("\b\n");
               break;
           case ':' :
               useSpaces= input.charAt(1)=='{';
               if(useSpaces)
                   System.out.println();
               break;
           case '{':
               System.out.println();
               spaces= spaces.append(" ");
               break;
           case '}':
               System.out.println();
               spaces= spaces.length()>0?spaces.deleteCharAt(0):spaces;
               break;
           default:
               useSpaces=  input.charAt(1)=='}';
               if(useSpaces)
                   System.out.println();             
       }
       printJSON(input.substring(1, input.length()), spaces, useSpaces);
    }

- nj August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string t="";

void indent(int indent_counter)
{
	while(indent_counter--)
		t+="\t";
}

int main()
{
	string s;
	cin >> s;
	int len=s.length();
	int indent_counter=0;
	s+='p';
	for(int i=0;i<len;i++)
	{
		if(s[i]=='{')
		{
			indent(indent_counter);
			if(s[i+1]!='{' || s[i+1]!='}')
			{
				t+="\n";
				indent(indent_counter);
				t+="{\n";
			}
			else
			{
				t+="\n";
				indent(indent_counter);
				t+="{";
			}
			indent(++indent_counter);
		}
		else if(s[i]=='}')
		{
			
			if(s[i+1]!='}' || s[i+1]!='{')
			{
				t+="\n";
				indent(--indent_counter);
				t+="}\n";
			}
			else
			{
				t+="\n";
				indent(--indent_counter);
				t+="}";
			}
			indent(indent_counter);
		}
		else if(s[i]==',')
		{
			t+="\n";
			indent(indent_counter);
		}
		else
		{
			t+=s[i];
			//indent(indent_counter);
		}
		//cout <<i<<"\t"<< s[i]<<endl;
	}
	cout << t<<endl;
	return 0;  
}

- maverick August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void indent(char[] A)
	{
		int tab=0;
		int count=0;
		for(int i=0;i<A.length;i++)
		{
			
			if(A[i]=='{')
			{
				
				count=0;
				System.out.print("\n");
				for(int j=0;j<tab;j++)
					System.out.print("\t");
				System.out.print("{");
				System.out.print("\n");
				tab++;
			}
			else if(A[i]==',')
			{
				count=0;
				System.out.print("\n");
			}
			else if(A[i]=='}')
			{
				
				count=0;
				tab--;
				System.out.print("\n");
				for(int j=0;j<tab;j++)
					System.out.print("\t");
				System.out.print("}");
				System.out.print("\n");
			if(tab<0)
				System.out.print("error");
			}
			else
			{
				if(count==0)
				{
				for(int j=0;j<tab;j++)
					System.out.print("\t");
				}
				System.out.print(A[i]);
				count=1;
			}	
				public static void main(String[] pp)
	{
		
		
		char[] A ="{A:\"B\",C:{D:\"E\",F:{G:\"H\",I:\"J\"

".toCharArray() ;

indent(A);

}
}
}

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

naveen can u pls tell me evrything about the interview,whose question ur posting?ma email is anuragmishra.manit@gmail.com

- rockstar August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code :

#include <stdio.h>

#define MAX_LENGTH 100

int is_special(char ch)
{
	if(ch == ',' || ch == '{' || ch == '}')
		return 1;
	return 0;
}

main()
{
	char S[MAX_LENGTH];
	int i, alph = 0;

	scanf("%s", S);

	for(i = 0; S[i] != '\0'; i++)
	{
		if(is_special(S[i]))
		{
			if(alph)
				printf("\n");
			
			if(S[i] != ',')
				printf("%c\n", S[i]);
			
			alph = 0;

		}
		else
		{
			printf("%c", S[i]);
			alph = 1;
		}
	}

	return 0;
}

Please give me your suggestions.

- srikantaggarwal August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

good one!!!

- Anonymous September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.


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