Microsoft Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
/*
Now, for the health concious, who eats only one item.
Surprize, same code, almost!
*/
_options_ = [ 'bread-butter' , 'pizza' , 'burger' ]
// is the combination valid
def is_valid_combination( item , history ){
if ( empty(history) ) return true
if ( 'pizza' == item && 'pizza' == history[-1] ) return false
if ( 'burger' == item ){
if ( 'burger' == history[-1] ) return false
if ( size(history) > 1 && 'burger' == history[-2] ) return false
}
return true
}
// the recursive definition
def _recurse_(days, history){
if ( size(history) == days ){
printf('%s\n', history )
return
}
for ( c : _options_ ){
if ( is_valid_combination( c, history ) ){
h = list(history)
h.add( c )
_recurse_(days,h)
}
}
}
// call it
def gen_combo( days ){
_recurse_( days,[])
}
gen_combo(3)
the recursion.
Rec(n, f1, f2):
if n = 0: return 0;
c = 1 + rec(n-1, bread butter, f1)
If f1 != pizza: c += 1 + rec(n-1, pizza, f1)
If f2 != Burger and f1 != Burger c += 1 + rec(n-1, burger, f1)
Return c
then the solution is:
Rec(n, no, no)
this is O(3^n), can be O(n) if implemented with memoization or
as an iterative, array based solution
if you are fit with combinatorics, you might create a closed form
formula. Maybe someone takes the time for that, I found it a bit
complicated, given I don't like pizza or burger for breakfast...
No = 0
BreadButter = 1
Pizza = 2
Burger = 3
def numberOfWays4Breakfast(n):
memo = {}
def recursion(n, f1, f2):
if n == 0: return 0
key = n * 16 + f2 * 4 + f1
c = memo.get(key)
if c != None: return c
c = 1 + recursion(n - 1, BreadButter, f1)
if f1 != Pizza: c += 1 + recursion(n - 1, Pizza, f1)
if f2 != Burger: c += 1 + recursion(n - 1, Burger, f1)
memo[key] = c
return c
return recursion(n, No, No)
print(numberOfWays4Breakfast(3))
@guilhebi
With this formula you have the number of bread-butter, pizzas and hamburgers you can eat but not the number of ways you can eat them.
If the number of days is 3 for example you could have: bread, bread, bread; bread, bread, pizza; ...; pizza, bread, bread.
Indeed you could have 3 bread-butters at maximum or 2 pizzas or 1 hamburger (the +1 only applies if the number is greater than 3).
In order to compute the number of ways you have what you have to do is to compute the permutations of those values (permutations with repetitions in this case as you have repeated elements).
Using your formula the number of ways would be:
answerPn / (bread-butter * pizza * burguer)
This is not the complete answer as here we are counting incorrect solutions (for example (pizza, pizza, bread)). In order to give the correct answer you have to compute the number of extra incorrect solutions and subtract it to previous the number. For the case of the three days it is 5. Here are all the possibilities for three days:
{('bread', 'bread', 'bread'),
('bread', 'bread', 'hamburguer'),
('bread', 'bread', 'pizza'),
('bread', 'hamburguer', 'bread'),
('bread', 'hamburguer', 'pizza'),
('bread', 'pizza', 'bread'),
('bread', 'pizza', 'hamburguer'),
('hamburguer', 'bread', 'bread'),
('hamburguer', 'bread', 'pizza'),
('hamburguer', 'pizza', 'bread'),
('pizza', 'bread', 'bread'),
('pizza', 'bread', 'hamburguer'),
('pizza', 'bread', 'pizza'),
('pizza', 'hamburguer', 'bread'),
('pizza', 'hamburguer', 'pizza'),
import java.util.Scanner;
public class CaluculateENhancement {
static int length;
static int poss;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
length = sc.nextInt();
StringBuffer inp=new StringBuffer();
generateString(inp, length, 0,1);
System.out.println("done bro..."+poss);
}
private static StringBuffer generateString(StringBuffer str, int len, int index, int pos){
String strChar[] ={"a","b","c"};
if(str.length()==length){
if(validate(str))poss++;
return str;
}
for(int k=0; k<strChar.length; k++){
str.append(strChar[k]);
generateString(str, len, index, pos+1);
str.deleteCharAt(str.length()-1);
}
return str;
}
private static boolean validate(StringBuffer str) {
char ch[] = str.toString().toCharArray();
for(int i=0;i<str.length();i++){
if(ch[i]=='b'){
if(i-1>-1){
if(ch[i-1]=='b'){
return false;
}
}
}else if(ch[i]=='c'){
if(i-1>-1){
if(ch[i-1]=='c'){
return false;
}
}
if(i-2>-1){
if(ch[i-2]=='c'){
return false;
}
}
}
}
return true;
}
}
import java.util.Scanner;
public class CaluculateENhancement {
static int length;
static int poss;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
length = sc.nextInt();
StringBuffer inp=new StringBuffer();
generateString(inp, length, 0,1);
System.out.println("done bro..."+poss);
}
private static StringBuffer generateString(StringBuffer str, int len, int index, int pos){
String strChar[] ={"a","b","c"};
if(str.length()==length){
if(validate(str))poss++;
return str;
}
for(int k=0; k<strChar.length; k++){
str.append(strChar[k]);
generateString(str, len, index, pos+1);
str.deleteCharAt(str.length()-1);
}
return str;
}
private static boolean validate(StringBuffer str) {
char ch[] = str.toString().toCharArray();
for(int i=0;i<str.length();i++){
if(ch[i]=='b'){
if(i-1>-1){
if(ch[i-1]=='b'){
return false;
}
}
}else if(ch[i]=='c'){
if(i-1>-1){
if(ch[i-1]=='c'){
return false;
}
}
if(i-2>-1){
if(ch[i-2]=='c'){
return false;
}
}
}
}
return true;
}
}
import java.util.Scanner;
public class CaluculateENhancement {
static int length;
static int poss;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
length = sc.nextInt();
StringBuffer inp=new StringBuffer();
generateString(inp, length, 0,1);
System.out.println("done bro..."+poss);
}
private static StringBuffer generateString(StringBuffer str, int len, int index, int pos){
String strChar[] ={"a","b","c"};
if(str.length()==length){
if(validate(str))poss++;
return str;
}
for(int k=0; k<strChar.length; k++){
str.append(strChar[k]);
generateString(str, len, index, pos+1);
str.deleteCharAt(str.length()-1);
}
return str;
}
private static boolean validate(StringBuffer str) {
char ch[] = str.toString().toCharArray();
for(int i=0;i<str.length();i++){
if(ch[i]=='b'){
if(i-1>-1){
if(ch[i-1]=='b'){
return false;
}
}
}else if(ch[i]=='c'){
if(i-1>-1){
if(ch[i-1]=='c'){
return false;
}
}
if(i-2>-1){
if(ch[i-2]=='c'){
return false;
}
}
}
}
return true;
}
}
public class MenuHandler {
private static boolean isValidMenuForTheDay(ArrayList<MenuEnum> bfMenu, int day, MenuEnum todayMenu) {
int prevDay=day-1;
int prev2Day=day-2;
if(prevDay > -1 && bfMenu.get(prevDay)==MenuEnum.Pizza && todayMenu==MenuEnum.Pizza){
return false;
}
if(prev2Day > -1 && bfMenu.get(prev2Day)==MenuEnum.Burger && todayMenu==MenuEnum.Burger){
return false;
}
return true;
}
public static void getPermutationsBreakfastMenuForDays(int startDay,int noOfDays,ArrayList<MenuEnum> list){
if(list.size()==4){
System.out.println("Final List: "+list);
}
if(startDay < 4 && noOfDays > -1){
for(MenuEnum menu:MenuEnum.values()){
if(isValidMenuForTheDay(list,startDay,menu)){
list.add(menu);
getPermutationsBreakfastMenuForDays(startDay+1,noOfDays-1,list);
list.remove(startDay);
}
}
}
}
public enum MenuEnum {
BreadAndButter,Pizza,Burger
}
struct ent_b {
int tp;
int when;
int current
};
void increament_entb(vector<ent_b> &S, int S) {
for(int i = 0; i < S; i++) {
if (S[i].current < when) {
S[i].current++;
}
}
}
void breakfast(int n, vector<ent_b> items, int days) {
if (days > n) {
return;
}
for(int i = 0; i < N; i++) {
if (b[i].when < b[i].current) {
continue;
}
vector<ent_b> temp = items;
temp[i].current = 0;
//picks b[i]
increament_entb(temp, items.size());
breakfast(n, temp, days++);
items = temp;
}
}
/*
* Assuming that a breakfast MUST consists of three items
* which makes Bread-Butter, Bread-Butter, Bread-Butter a valid breakfast = 3A
* let A = Bread-Butter, B = Pizza, C = Burger
* having enough days all reachable combinations (permutations) would be:
*
* AAA, BBB, CCC, perm (A, B,C), arrange: A in [BB], A in [CC], B in [AA], B in [CC], C in [AA], C in [BB]
* 1 + 1 + 1 + 3! + 3 + 3 + 3 + 3 + 3 + 3 = 27
*
* if letting ABB == BAB == BBA then total = 15
*
*/
- NoOne June 19, 2017