Created
November 8, 2016 01:38
-
-
Save jakubtuchol/ab62aa73b231b13956c0d4896b416bad to your computer and use it in GitHub Desktop.
Pinterest Interview Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.jakubtuchol.algorithms.questions; | |
| /** | |
| * Created by jakub on 12/12/15. | |
| */ | |
| public class NameLister { | |
| public static String listNames(String[] names) { | |
| if (names.length == 1) | |
| return names[0]; | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = 0; i < names.length - 1; i++) { | |
| if (i == names.length - 2) { | |
| sb.append(names[i]); | |
| sb.append(" and "); | |
| sb.append(names[i + 1]); | |
| } else { | |
| sb.append(names[i]); | |
| sb.append(", "); | |
| } | |
| } | |
| return sb.toString(); | |
| } | |
| public static String generateShortener(int num) { | |
| return String.format("%d more", num); | |
| } | |
| public static int shortenerLength(int numNames) { | |
| return Integer.toString(numNames).length() + " more".length(); | |
| } | |
| public static String listNamesNumLimited(String[] names, int maxNames) { | |
| if (names.length == 1) { | |
| if (maxNames == 0) | |
| return generateShortener(1); | |
| else | |
| return names[0]; | |
| } | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = 0; i < names.length - 1; i++) { | |
| // found next to last element | |
| if (i == names.length - 2) { | |
| if (maxNames == 0) { | |
| if (i != 0) | |
| sb.append(" and "); | |
| sb.append(generateShortener(2)); | |
| } else if (maxNames == 1) { | |
| sb.append(names[i]); | |
| sb.append(" and "); | |
| sb.append(generateShortener(1)); | |
| } else { | |
| sb.append(", "); | |
| sb.append(names[i]); | |
| sb.append(" and "); | |
| sb.append(names[i+1]); | |
| } | |
| } else { | |
| if (maxNames == 0) { | |
| sb.append(" and "); | |
| sb.append(generateShortener(names.length - i)); | |
| } else { | |
| if (i != 0) | |
| sb.append(", "); | |
| sb.append(names[i]); | |
| } | |
| } | |
| maxNames--; | |
| } | |
| return sb.toString(); | |
| } | |
| public static String listNamesCharLimited(String[] names, int numChars) { | |
| if (names.length == 1) { | |
| if (names[0].length() > numChars) { | |
| if (shortenerLength(1) > numChars) | |
| return ""; | |
| else | |
| return generateShortener(1); | |
| } else | |
| return names[0]; | |
| } | |
| for (int i = 0; i < names.length; i++) { | |
| } | |
| return ""; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.jakubtuchol.algorithms.questions; | |
| import junit.framework.TestCase; | |
| import org.junit.Test; | |
| /** | |
| * Created by jakub on 12/12/15. | |
| */ | |
| public class NameListerTest extends TestCase { | |
| @Test | |
| public void testListNamesSimple() { | |
| String[] nameList = {"Alice", "Bob", "Carlos", "Diana"}; | |
| assertEquals("Alice, Bob, Carlos and Diana", NameLister.listNames(nameList)); | |
| } | |
| @Test | |
| public void testListNamesDegenerate() { | |
| String[] nameList = {"Alice"}; | |
| assertEquals("Alice", NameLister.listNames(nameList)); | |
| } | |
| @Test | |
| public void testListNamesTwoNames() { | |
| String[] nameList = {"Alice", "Bob"}; | |
| assertEquals("Alice and Bob", NameLister.listNames(nameList)); | |
| } | |
| @Test | |
| public void testGenerateShortener() { | |
| assertEquals("0 more", NameLister.generateShortener(0)); | |
| assertEquals("4 more", NameLister.generateShortener(4)); | |
| } | |
| @Test | |
| public void testListNamesLimited() { | |
| String[] nameList = {"Alice", "Bob", "Carlos", "Diana"}; | |
| assertEquals("Alice, Bob and 2 more", NameLister.listNamesNumLimited(nameList, 2)); | |
| } | |
| @Test | |
| public void testListNamesLargeLimit() { | |
| String[] nameList = {"Alice", "Bob", "Carlos", "Diana"}; | |
| assertEquals("Alice, Bob, Carlos and Diana", NameLister.listNamesNumLimited(nameList, 4)); | |
| } | |
| @Test | |
| public void testListTwoLimited() { | |
| String[] nameList = {"Alice", "Bob"}; | |
| assertEquals("Alice and 1 more", NameLister.listNamesNumLimited(nameList, 1)); | |
| } | |
| @Test | |
| public void testListTwoCompletelyLimited() { | |
| String[] nameList = {"Alice", "Bob"}; | |
| assertEquals("2 more", NameLister.listNamesNumLimited(nameList, 0)); | |
| } | |
| @Test | |
| public void testShortenerLength() { | |
| assertEquals(7, NameLister.shortenerLength(11)); | |
| assertEquals(6, NameLister.shortenerLength(6)); | |
| } | |
| @Test | |
| public void testCharLimitedLessThanOneWord() { | |
| String[] nameList = {"Alice"}; | |
| assertEquals("", NameLister.listNamesCharLimited(nameList, 2)); | |
| } | |
| @Test | |
| public void testSingleWordCharLimited() { | |
| String[] nameList = {"Batholemeus"}; | |
| assertEquals("1 more", NameLister.listNamesCharLimited(nameList, 8)); | |
| } | |
| @Test | |
| public void testCharLimited() { | |
| String[] nameList = {"Alice", "Bob", "Carlos", "Diana"}; | |
| assertEquals("Alice, Bob, Carlos and 1 more", NameLister.listNamesCharLimited(nameList, 30)); | |
| } | |
| @Test | |
| public void testSecondCharLimited() { | |
| /* | |
| String[] nameList = {"Alice", "Bob", "Carlos", "Diana"}; | |
| assertEquals("Alice, Bob and 2 more", NameLister.listNamesCharLimited(nameList, 20)); | |
| */ | |
| assertTrue(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment