Interview Question for Front-end Software Engineers






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

hi,
Shouldn't the fourth line be
111221
instead of 111212

- Anonymous February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static void main(String[] args)
	{
		int number = 1;
		for(int i =0;i<7;++i)
		{			
			System.out.println(number);
			number = caculateNextNumber(number);			
		}
		
	}
	public static int caculateNextNumber(int number)
	{
		int digit=0;		
		int newNumber=0;
		int multiplier = 1;
		while(number> 0) 
		{	
			int count=0;
			do{
				digit = number%10;
				number = number/10;
				count++;
			}while(digit==number%10);
			newNumber=newNumber+(count*10+digit)*multiplier;
			multiplier*=100;
		}
		return newNumber;
	}

- Radix May 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here you go

1 
 11 
 21 
 1211 
 111221 
 312211 
 13112221 
 1113213211 

#include <stdio.h>

int main(void)
{

    int num = 1, i;
    unsigned long new_num;

    int  j, count, temp;

    for (i= 0 ;i < 8 ; i++)
    {
        new_num =  0;
        j = 1;

        printf("\n %u \t", num);

        while (num)
        {
            temp = num % 10;

            num /=10;

            count = 1;

            while (temp == (num%10))
            {
                count++;
                num /=10;
            }

            // printf("count %d, temp %d, j %d, new_num %d \n", count, temp, j, new_num);

            new_num = (((10*count) + temp) *j) + new_num;

            j *= 100;
        }

        num = new_num;
      }
    return 0;
}

- Mike February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Program:


package com.javaconcepts.datastructure;

import java.util.ArrayList;
import java.util.List;

public class PrinitProblem {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
print(10);
}

public static void print(int n ){
List<String> list = null;
if(n <=0){
System.out.println("Please provide a valid number");
return;
}else{
list = new ArrayList<String>();
list.add("1"); //intialize
}
for(int i = 0; i < n; i++){
String newInt = counter(list.get(i));
list.add(newInt);
}
System.out.println(list);
}

private static String counter(String integer) {
if(integer == null){
return null;
}
String str = integer.toString();
String lastValue=null;
int counter = 1;
String newstr = "";
for(int i = 0; i < str.length(); i ++){
lastValue = str.substring(i,i+1);
if(i+1 < str.length()){
String nextValue = str.substring(i+1,i+2);
if(lastValue.equalsIgnoreCase(nextValue)){
counter++;
}else{
newstr = newstr + counter+""+lastValue;
counter = 1;
}
}
else{
newstr = newstr+"" + counter+""+lastValue;
}
}
return newstr;
}

}

- Sanjay Kumar (Inventwheel.com) February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo:
Maintain 2 queues. At the start of each line-print loop, one queue will always be empty, the other will always contain the line to be printed.
Dequeue and print from the non-empty queue 1 element at a time. While dequeue-ing, in addition to printing, also check if the element has changed. If no, increment counter. If yes, push both the counter and the element to the other queue. Reset the counter and continue till the queue being read (and printed) is empty.
Repeat(now the empty and non-empty queues would have flipped but the logic remains the same)

Here's a working C code:

void printRoutine(int firstArray[], int secondArray[])
{
int i = 0, j = 0;
int count = 0;

int currentElement = firstArray[0];
while(firstArray[i] != 0) //Loops till queue is empty
{
count = 0;
while(currentElement == firstArray[i]) //Loops till element changes
{
printf("%d\t", firstArray[i]);
count++;
firstArray[i] = 0; //dequeue
i++;
}

secondArray[j++] = count;
secondArray[j++] = currentElement;
currentElement = firstArray[i];
}
printf("\n\n");
}

void countNumbersAndPrint()
{
int array1[50] = {0}, array2[50] = {0};
int numLinesToPrint = 0;
int linesPrinted = 0;
int whichArray = 1;
int i=0;

//initialize
array1[0] = 1;

printf("Enter the number of lines to print\n");
scanf("%d", &numLinesToPrint);

while(linesPrinted < numLinesToPrint)
{
if(1 == whichArray)
{
printRoutine(array1, array2);
whichArray = 2;
}
else
{
printRoutine(array2, array1);
whichArray = 1;
}
linesPrinted++;
}
}

void main()
{
printf("Printing the count...\n");
countNumbersAndPrint();
}

This can be further optimized to use just 1 queue actually. But implementing queues with arrays is a PITA!

- code-monkey May 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(function line(depth, number) {
  var accumulate = 0, nthDigit = 0;
  while(number) {
    var count = 0, digit = number % 10;
    while(digit === number % 10) {
      number = Math.floor(number/10);
      count++;
    }
    accumulate = accumulate + (count * 10 + digit) * Math.pow(10, nthDigit);
    nthDigit+=2;
  }
  console.log(accumulate);
  if (--depth) return line(depth, accumulate);
  else return;
})(8, 1)

one caveat is javascript put your big number into scientific notation if number close to 1e21

- D.L. February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

nt main(){
	vector<vector<int> > ans ;
	vector<int> tmp ;
	tmp.PB(1) ;
	tmp.PB(-1) ;		// Adding extra number to help compare the last number
	ans.PB(tmp) ;
	int n ;
	cin>>n ;
	for(int i=1; i<n ;++i){
		tmp.clear() ;
		int s = ans[i-1].size() ;
		int count = 1, j=0 ;
		while(j<s-1){
			if(ans[i-1][j]!=ans[i-1][j+1]){ 	// the last number will be tried to match with Dummy value j+1  
				tmp.PB(count);
				tmp.PB(ans[i-1][j]);
				count = 1 ;
			}else{
				++count ;
			}
			++j ;
		}
		tmp.PB(-1) ; 		// maintaining the dummy val
		ans.PB(tmp) ;
	}
	for(int i=0; i<ans.size() ; ++i){
		for(int j=0; j<ans[i].size()-1 ; ++j){		//not printing dummy number
			cout<<ans[i][j] ;
		}
		cout<<endl ;
	}
	return 0;
}

- wrathtoliar November 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Pattern(int n)
{
int count=1,temp,i=1;
struct queue *q1=createqueue();
struct queue *q2=createqueue();
struct queue *q3=createqueue();
enqueue(q1,1);
while(!IsEmptyQueue(q1))
{
temp=dequeue(q1);
while(temp==Front(q1))
{
temp=dequeue(q1);
count++;

}
printf("%d%d",count,temp);
enqueue(q2,count);
enqueue(q2,temp);
count=1;
if(IsEmptyQueue(q1))
{
q3=q1;
q1=q2;
q2=q3;
i++;
printf("\n\n");
}
if(i==n)
{
return;
}
}
}

- dolly October 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
for (int i = 1; i <= 7; i++) {
int j;
if (i==1){ list.add(1); System.out.println(i); continue;}

Object arr[] = list.toArray();
list.clear();
int continuity = 1;
for (j=0; j<arr.length-1 ; j++){
if((Integer)arr[j+1] == (Integer)arr[j])
{
continuity++;
}
else
{
System.out.print(continuity);
list.add(continuity);
System.out.print(arr[j]);
list.add((Integer)arr[j]);
continuity=1;
}
}
if(j==arr.length-1)
{
System.out.print(continuity);
list.add(continuity);
System.out.print(arr[j]);
list.add((Integer)arr[j]);
}
System.out.println("");
}

}

Output

1
21
1211
111221
312211
13112221
1113213211

- Anonymous February 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		for (int i = 1; i <= 7; i++) {
			int j;
			if (i==1){ list.add(1); 	System.out.println(i); continue;}
			
			Object arr[] = list.toArray();
			list.clear();
			int continuity = 1;
			for (j=0; j<arr.length-1 ; j++){
				if((Integer)arr[j+1] == (Integer)arr[j])
				{
					continuity++;
				}
				else
				{
					System.out.print(continuity);
					list.add(continuity);
					System.out.print(arr[j]);
					list.add((Integer)arr[j]);
					continuity=1;
				}
			}
			if(j==arr.length-1)
			{
				System.out.print(continuity);
				list.add(continuity);
				System.out.print(arr[j]);
				list.add((Integer)arr[j]);
			}
			System.out.println("");
		}

	}

1
21
1211
111221
312211
13112221
1113213211

- Anonymous February 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function repetation(n){
	n = ''+n;	
	var result = ''		;
	var temp = n.charAt(0);

	for(var i = 1 ; i < n.length ; i++){
		if(!temp.includes(n.charAt(i))){
			result += temp.length + temp.charAt(0);
			temp = n.charAt(i);
		}	

		else{
			temp += n.charAt(i);	
		}
	}
	result += temp.length + temp.charAt(0);
	return repetation(result);

}

- Nirav Modi November 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pattern: 1
11
121
1221

Code Please?

- Ravi Thukral March 04, 2018 | 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