iPowerFour Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

bool is_sum_possible(int num_items)
{
bool * temp;
int i;

if(num_items<18)
{
if((num_items==6)||(num_items==9)||(num_items==17)||(num_items==12)||
(num_items==15))
		return true;
}

temp=(bool *)malloc(sizeof(bool)*(num_items+1));
for(i=0;i<num_items;i++)
temp[i]=false;

temp[6]=temp[9]= temp[17]=temp[12]=temp[15]=true;

for(i=18;i<num_items;i++)
{
		if(temp[i-6]|temp[i-9]|temp[i-17])
				temp[i]=true;
}

return temp[num_items];

}

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

small correction:

if(num_items<18)
{
if((num_items==6)||(num_items==9)||(num_items==17)||(num_items==12)||
(num_items==15))
return true;

return false;
}

- Anonymous February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for(i=18;i<num_items;i++)
should be
for(i=18;i<=num_items;i++)

- HD February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah..thanks :) my point was to show how to solve it using dynamic programming aided by memoization.

- Anonymous February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

1 #include <stdio.h>
  2 int f(int input) {
  3         if(!input) return 1;
  4         if(input < 6) return 0;
  5         return f(input-6) || f(input-9) ||f(input-17);
  6 }
  7 
  8 int main() {
  9         int iNum;
 10         while(scanf("%d\n",&iNum) > 0) {
 11                 if(f(iNum)) {
 12                         printf("true!\n");
 13                 } else {
 14                         printf("false!\n");
 15                 }
 16         }
 17         return 0;
 18 }

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

+1 for the simplicity of the code.

I guess this works for all the cases;

- Shekar Gurram February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

take LCM of three number if the N is divisible by LCM (6,9,17) then it is possible otherwise not...

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

Wrong. If input is 12, that is not the multiple of LCM, but it can be given as 2 X 6. We need to search some other solution.

- Rajat Rastogi February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use the knapsack technique to do it.

- whizz February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wt abt 36 ??? it wont work..

- arunkumar February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Didn't get you ? Was that comment for the below post ?

- whizz February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

//Function to calculate posssibility
bool IsPossible(int numItems)
{
     int tempItems;
     if (numItems < 6) return false;
     if (numItems < 9) return false;
     if (numItems < 17 && (numItems != 9 || numItems != 15)) return false;
        
     if ((tempItems = numItems % 17) > 0)
        {           
            if ((tempItems = ((tempItems != 9 && tempItems != 6) ? tempItems + 17 :tempItems ) % 9) > 0 )
            {    
                 if((tempItems=(tempItems != 6 ? tempItems + 9:tempItems) % 6) > 0)
                 {
                     return false;
                 }
                 return true;
            }
            return true;
        }  
     return true;                
}

//Driver Function
int main()
{
    int amount;
    do{
    cout<<"Enter amount here: ";
    cin>>amount;  
    bool result =IsPossible(amount);
    if(result)
        cout<< "Yes, Can be "<<endl;
    else
        cout<< "No, Cant be "<<endl;  
    }while(amount != -1);           
    
    return 0;    
}

- Jagamohan February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Re-check your code buddy. It doesn't satisfy all the test cases :)

- whizz February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code will return false for numItems = 6

- Anonymous February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

great solution... :))

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

bool IsSalePossible(int number)
{
if(number % 6 == 0)
return true;
if(number % 9 == 0)
return true;
if(number % 17 == 0)
return true;
if(number %3 == 0 && number % 6 == 3 && number > 6)
return true;
number = number - 17;
while(number > 0)
{
if(number % 6 == 0)
return true;
if(number % 9 == 0)
return true;
number = number - 17;
}
return false;
}

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

your code will return false for numItems = 32

- Anonymous February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

6+9+17=32..using this code input 32 gives false..

- Anonymous February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It may work like: (only algorithm, not coding)
[input:x]
if((x%6 and x%9 and x%17)==0) return true;
k=x/17;
kr = x%17;
if(k>=kr) return true;
if(k>=1 && (kr==10 || kr==7 || kr==13)) return true;
if(k%2==0 && (kr==2 || kr==4 || kr==11) return true;

// Many more cases can be there...

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

The problem is the same as finding if there exists a non-negative solution to a linear diophantine equation in 3 variables.

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

The problem is the same as finding if there exists a non-negative solution to a linear diophantine equation in 3 variables.

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

/* Here a = 6, b = 8 & c = 17 as per the question */
	public static boolean canGivePacks(int noOfItems, int a, int b, int c) {
		int[] combinations = {a, b, c, a + b, a + c, b + c, a + b + c};
		
		for (int comb: combinations) {
			if(noOfItems % comb == 0) 
				return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		for( int i = 0; i < 100; i++) {
		System.out.println("No. of Apples (" + i + ") supports packing?:" + ApplePacks.canGivePacks(i, 6, 9, 17));;
		}
	}

- gnr February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a+a+b etc cases?

- ac February 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple DP will do.

s[0] = true
s[i] = true if either s[i-6], s[i-9] or s[i-17] is true

- syeedibnfaiz February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N = 9a + 6b => N = 3q => q = 3a + 2b, given N, find a and b: q mod 3 = 2 => b=1,a=q/3, q mod 3 = 1 => b=2,a=q/3-1, else a=q/3, this means that for every N(3) > 5 there exists pair of a and b to satisfy requirements.

Let number of apple to be writtable as M = 17q + t, and M mod 3 != 0 and t mod 3 != 0 - as it has been checked in previous para, moving forward: q - t = d > 0 =>
M could be written as 17(q-t) + t + (18-1)t=17d + 18t.
So it's simple! Any objections?

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

You guys are making this way too complicated.

public boolean canPurchase(int num)
{
    num %= 17;
    num %= 9;
    num %= 6;
    return num == 0;

}

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

You guys are making this way too complicated.

public static boolean canPurchase(int num)
{
    num %= 17;
    num %= 9;
    num %= 6;
    return num == 0;

}

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

Very simple answer:

public static boolean canPurchase(int num)
{
    num %= 17;
    num %= 9;
    num %= 6;
    return num == 0;

}

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

this doesn't work. For example, 35. this can be broken into 9,9,17.

However, with your algorithm.
35 % 17 -> 1.
1 % 9 -> 1
1 % 6 -> 1
return false. this is wrong.

- sam February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this doesn't work. For example, 35. this can be broken into 9,9,17.

However, with your algorithm.
35 % 17 -> 1.
1 % 9 -> 1
1 % 6 -> 1
return false. this is wrong.

- sam February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

see if the number is divisible by 17,9, 6 or not indivisually.
if not check remainder of 17 is divisible by 6,9 or not
if not check remainder of ( remainder of 17 ) 9 is divisible by 6 or not .

if none of case give 0 then false else true

- V February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

number = gets.to_i

p (number % 6 == 0  || number % 9 == 0 || number % 17 == 0)

- Kranthi ( Ruby Language) November 14, 2014 | 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