Google foobar Challenge Level 1 The Cake is not a lie

Coding Challenge Coding Live

//a random non-symmetric string, should return 1 as one piece

Click to share on Facebook (Opens in new window)

To provide a Java solution, editsolution.java

Google foo.bar Challenge level 2: Dont get volunteered!

Commander Lambda has had an incredibly successful week: she completed the first test run of her LAMBCHOP doomsday device, she captured six key members of the Bunny Rebellion, and she beat her personal high score in Tetris. To celebrate, shes ordered cake for everyone even the lowliest of minions! But competition among minions is fierce, and if you dont cut exactly equal slices of cake for everyone, youll get in big trouble.

Write a function called answer(s) that, given a non-empty string less than 200 characters in length describing the sequence of M&Ms, returns the maximum number of equal parts that can be cut from the cake without leaving any leftovers.

//a string of just same characters, should return the length of the string

The cake is round, and decorated with M&Ms in a circle around the edge. But while the rest of the cake is uniform, the M&Ms are not: there are multiple colors, and every minion must get exactly the same sequence of M&Ms. Commander Lambda hates waste and will not tolerate any leftovers, so you also want to make sure you can serve the entire cake.

To provide a Python solution, editsolution.py

// if we found a valid equal repeating substring then valid is the number of repeating substrings

I got a Google foo.bar coding challenge while searching for Dependency Injections on google.

To help you best cut the cake, you have turned the sequence of colors of the M&Ms on the cake into a string: each possible letter (between a and z) corresponds to a unique color, and the sequence of M&Ms is given clockwise (the decorations form a circle around the outer edge of the cake).

// init the result to be -1 as default in case we get an empty string or null

//create a temp part substring of initial string to test for repetition

Decided to solve the first challenge (with some help) and post it here.

//if each iterative substring does not equal part then its not valid

HackerRank Cracking the Coding Interview 2: Strings: Making Anagrams

//passed the valid size multiple test

//NOTE: this is inefficient, need to change. will utilize Suffix tree

import org.junit.Assert; /** * Created by Arvind on 3/4/2017. */ public class AnswerTest @org.junit.Test public void answer() throws Exception //Given Test Case 1 Assert.assertEquals(Answer.answer(abccbaabccba),2); //Given Test Case 2 Assert.assertEquals(Answer.answer(abcabcabcabc),4); //a string of just same characters, should return the length of the string Assert.assertEquals(Answer.answer(aaaaaaaaaaaa),12); //a random non-symmetric string, should return 1 as one piece Assert.assertEquals(Answer.answer(falsestring),1); //empty string, should return -1 Assert.assertEquals(Answer.answer(),-1);

Click to share on Google+ (Opens in new window)

public class Answer public static int answer(String s) // init the result to be -1 as default in case we get an empty string or null int result = -1; //length of whole string int length = s.length(); //NOTE: this is inefficient, need to change. will utilize Suffix tree for(int i = length; i 0; i–) int n = length/i; //if n is a multiple of length (ie. total string of size length could consist of whole multiples of substrings of size n) if(n * i == length) //passed the valid size multiple test Boolean valid = true; //create a temp part substring of initial string to test for repetition String part = s.substring(0,n); //iterate over the number of equal substrings i for (int j = 1; j i; j++) //if each iterative substring does not equal part then its not valid if (!s.substring(j*n,j*n+n).equals(part)) valid = false; break; // if we found a valid equal repeating substring then valid is the number of repeating substrings if (valid) result = i; break; return result;

Click to share on Twitter (Opens in new window)

//if n is a multiple of length (ie. total string of size length could consist of whole multiples of substrings of size n)

//iterate over the number of equal substrings i

This entry was posted inUncategorizedand taggedchallenge,foobar,google,java,the cake is not a lieon

Leave a Reply