Facebook Interview Question for Software Engineer / Developers






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

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i don't understand the puzzle

- nos December 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

chutiye

- coder March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

chutiye

- coder March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void parseJSON()
	{
		byte[] input = "{\"a1\":\"abc\",\"t\":true,\"e\":123}".getBytes();
		ArrayList<UniType> array = new ArrayList<UniType>();
		int type = 0;// 0 for array, 1 for object
		int status = 0;
		byte[] trues = "true".getBytes();
		byte[] falses = "false".getBytes();
		UniType temp = new UniType();
		
		for(int i = 0; status!=-1 && i < input.length  ; i++)
		{
			char c = (char) input[i];
			switch(status)
			{
				case 0:
					if(c == '{')
						status = 1;
					else
						status = -1;
					break;
				case 1:
					switch(c)
					{
						case ' ':
							break;
						case '"':
							status = 2;
							temp.type = 1;
							temp.value.append('"');
							break;
						case '+':
						case '-':
							temp.type = 0;
							temp.value.append(c);
							status = 4;
							break;
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							temp.type = 0;
							temp.value.append(c);
							status = 5;
							break;
						case 'f':
							temp.type = 2;
							temp.value.append(c);
							status = 10;
							break;
						case 't':
							temp.type = 2;
							temp.value.append(c);
							status = 6;
							break;
						default:
							status = -1;
					}
					break;
				case 2:
					if(c == '"')
					{
						temp.value.append('"');
						array.add(temp);
						status = 3;
						temp = new UniType();
					}else
					{
						temp.value.append(c);
					}
					break;
				case 3:
					switch(c)
					{
					case ':':
						type = 1;
					case ',':
						status = 1;
						break;
					case '}':
						status = -1;
					default:
						status = -1;
					}
					break;
				case 4:
					if( c >= '0' && c <= '9' )
					{
						status = 5;
						temp.value.append(c);
					}else
					{
						status = -1;
					}
					break;
				case 5:
					if( c >= '0' && c <= '9' )
					{
						temp.value.append(c);
					}else if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 6:
				case 7:
				case 8:
					if(c == trues[status - 5])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 9:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
					if(c == falses[status - 9])
					{
						temp.value.append(c);
						status++;
					}else
					{
						status = -1;
					}
					break;
				case 14:
					if(c == ',')
					{
						array.add(temp);
						temp = new UniType();
						status = 1;
					}else if(c == '}')
					{
						array.add(temp);
						temp = new UniType();
						status = -1;
					}else
						status = -1;
					break;
				default:
					status = -1;
			}
		}
		System.out.println(type == 1 ? "object" : "array");
		
		//print out the result
		status = 0;
		for(UniType un : array)
		{
			if(status < type)
				System.out.print( un.getValue() + ":");
			else
				System.out.println(un.value.toString());
			status = 1 - status;
		}
	}

- Ragnarok March 05, 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