Interview Question


Country: United States




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

use dynamic array(malloc and free)
if the array size does not increase then
1. do inplace addition of the values from array[last_element]
if the array size increases then
1.copy the array values in temp variable {9,9,9}---> temp = 999.
2.free the array
3.dynamically allocate the array with size length+1
4.add 1 to 999 and copy it to the new array as {1,0,0,0}

- aravind mano May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we are interested in the final result, then the we can achieve the result without conversion. The code is given below

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int a[] = {7,3,5,3,9};
	int i;
	
	// Instead of adding 1 separately, set carry initially 
	int carry = 1; 
	
	int size = sizeof(a)/sizeof(a[0]);
	int* result = (int*)malloc((size+1)*sizeof(int));
	for(i=size-1;i>=0;i--)
	{
		result[i+1] = (a[i]+carry)%10;
		carry = (a[i]+carry)/10;
	}
	result[0] = carry;
	for(i=0;i<size+1;i++)
		printf("%d",result[i]);
	printf("\n");
	return 0;
}

- dadakhalandhar May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can i get the same for PHP

- Denny May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't know php.

- dadakhalandhar May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can i get a php program for the above question

- denny May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

am i missing anything


<?php
$ss = array(2,3,4);
$count = count($ss);
$sum = 0;
foreach($ss as $key => $item){
$sum += $item * (10 to the power ($count-$key));
}

$final = $sum +1;

$count_integer = lenght($final);

for ($i = 0; $i < $count_integer; $i++){
$new_array[] = $final % 10;
$final = ceil($final/10);
}
?>

- D May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;

public class ArrayToNumber {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int [] inputArray = {9,9,9};
		int [] inputArray = {7,3,5,3,9};
		System.out.println( "out put:"+ Arrays.toString(inputNumber(inputArray)));

	}
	/**
	 * 
		For example input = {7,3,5,3,9} convert this to number 73539, 
		add 1 so it becomes 73540 and convert to array {7,3,5,4,0}. 
		Array can be of any length, so you can't always represent array in form 
		of in-built number format. So you have to do this summation in-place. 
		Also, how would you increase array size in-case input = {9,9,9} 
		so output = (1,0,0,0} 
		Assume, all elements of arrays are between 0 and 9.
	 */
	public static int [] inputNumber(int [] inputArray){
		int integerValue=0;
		int [] outputArray = new int[inputArray.length];
		int [] overFlowOutputArray = null;		
		//Converting array of elements into integer number
		for(int i=0; i<inputArray.length;i++){
			integerValue=integerValue*10+inputArray[i];
		}
		//Adding one to the integer number which got from previous step
		integerValue = integerValue+1;		
		//Converting integer number back array
		for(int j=inputArray.length-1; integerValue>0;j--){ 
			//Check the array need to be increased and return the array
			if(j==-1){
				overFlowOutputArray = new int[inputArray.length+1];
				System.arraycopy(outputArray, 0, overFlowOutputArray, 1, inputArray.length);
				overFlowOutputArray[0] = integerValue%10;
				return overFlowOutputArray;
			}
			outputArray[j] = integerValue%10;
			integerValue = integerValue/10;	
		}
		return outputArray;
	}
}

- Anonymous May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

php code for the above problem

<?php
$input = array(9,9,9);
$number ='';
foreach($input as $val ) {
 $number .=$val;
}
$result = str_split($number+1);

print_r ($result);
?>

- Dhinesh Ramalingam May 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class ArrayElementToNumber {

	public static void main(String[] args) {
		System.out.println("Enter the length of array");
		int[] arr = { 7, 5, 3, 2, 9,5,2,3
				};
		Stack<Integer> stack = new Stack<Integer>();

		int i = 0;
		int n = 1;
		while (i <= arr.length - 1) {
			stack.push(arr[arr.length - 1 - i] * n);
			n = n * 10;
			i++;
		}
		int x = 0;
		int result = 0;
		while (x < stack.capacity()) {
			int number = stack.pop();
			result = number + result;
			System.out.println(result);
		}
	
	}
}

- Anonymous May 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Solution

- abcd May 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main(){
int n,a[20],b[20],i,count=0;
long num=0,num1;
clrscr();
printf("Enter number of elements in the array: ");
scanf(" %d",&n);
for(i=0;i<n;i++){
printf("Enter the %d element of array: ",i+1);
scanf(" %d",&a[i]);
num=num*10+a[i];
}
printf("\nThe elements inside the array is: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
num=num+1;
num1=num;
while(num/10!=0){
count=count+1;
num=num/10;
}count++;
n=count;
for(i=count-1;i>=0;i--){
b[i]=num1%10;
num1=num1/10;
}
printf("\nThe new array is: ");
for(i=0;i<n;i++)
printf(" %d",b[i]);


getch();
return 0;
}

- MUKESH KUMAR May 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Language: JavaScript
Assumptions: arr is an integer array, num is an integer

function addOneToArray( arr, num ) {
	var tmp = arr.slice(0);
	( (+(tmp.join("")) + num ) + "").split("").forEach( function(el, i) { tmp[i] = +el; } );
	return tmp;
}

- sid.dibenedetto May 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no need to convert the array to numeric
simply scan the array from the end , until the first letter that is not '9' ( say *cp)is reached
increment *cp, then set elements after cp to '0'
if (cp<array[0]) free and allocate a new array

- EthOmus May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n%d",a[i]);
return 0;
}
int shift(int a[],int n)
{
int i;
for(i=n;i>0;i--)
a[i]=a[i-1];
a[0]=1;
return n+1;
}
int main()
{
int i,a[10],n;
printf("enter the number of elements to be inserted in the array:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=-1;i--)
{
if(i<0){
n=shift(a,n);break;}
if(((a[i]+1)/10)<1){
a[i]=a[i]+1;
break;}
else
a[i]=(a[i]+1)%10;
}
display(a,n);
return 0;
}

- ram rs May 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple perl program for this

#! /usr/local/bin/perl -w
use strict;

my @input_array = (8,7,9);
my $number;

foreach my $value (@input_array){
$number = $number.$value;
}

$number = $number + 1;
my @final_array = split(//,$number);

foreach my $val (@final_array){
print "$val\n";
}

- Nakul May 22, 2013 | 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