Interview Question
Front-end Software Engineerspublic 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;
}
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;
}
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;
}
}
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!
(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
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;
}
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;
}
}
}
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
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
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);
}
hi,
- Anonymous February 25, 2011Shouldn't the fourth line be
111221
instead of 111212