├── #000 - #099 ├── #001 - Google │ ├── #001.pdf │ └── Solution.java ├── #002 - Uber │ ├── #002.pdf │ └── Solution.java ├── #003 - Google │ ├── #003.pdf │ └── Solution.java ├── #004 - Stripe │ ├── #004.pdf │ └── Solution.java ├── #005 - Jane Street │ ├── #005.pdf │ └── Solution.java ├── #006 - Google │ └── #006.pdf ├── #007 - Facebook │ ├── #007.pdf │ └── Solution.java ├── #008 - Google │ ├── #008.pdf │ └── Solution.java ├── #009 - Airbnb │ ├── #009.pdf │ └── Solution.java ├── #010 - Apple │ ├── #010.pdf │ └── Solution.java ├── #011 - Twitter │ ├── #011.pdf │ └── Solution.java ├── #012 - Amazon │ ├── #012.pdf │ └── Solution.java ├── #013 - Amazon │ ├── #013.pdf │ └── Solution.java ├── #014 - Google │ └── #014.pdf ├── #015 - Facebook │ └── #015.pdf ├── #016 - Twitter │ ├── #016.pdf │ └── Solution.java ├── #017 - Google │ ├── #017.pdf │ └── Solution.java ├── #018 - Google │ ├── #018.pdf │ └── Solution.java ├── #019 - Facebook │ ├── #019.pdf │ └── Solution.java ├── #020 - Google │ ├── #020.pdf │ └── Solution.java ├── #021 - Snapchat │ ├── #021.pdf │ └── Solution.java ├── #022 - Microsoft │ ├── #022.pdf │ └── Solution.java ├── #023 - Google │ ├── #023.pdf │ └── Solution.java ├── #024 - Google │ ├── #024.pdf │ └── Solution.java ├── #025 - Facebook │ ├── #025.pdf │ └── Solution.java ├── #026 - Google │ ├── #026.pdf │ └── Solution.java ├── #027 - Facebook │ ├── #027.pdf │ └── Solution.java ├── #028 - Palantir │ ├── #028.pdf │ └── Solution.java ├── #029 - Amazon │ ├── #029.pdf │ └── Solution.java ├── #030 - Facebook │ └── #030.pdf ├── #031 - Google │ ├── #031.pdf │ └── Solution.java ├── #032 - Jane Street │ └── #032.pdf ├── #033 - Microsoft │ └── #033.pdf ├── #034 - Quora │ └── #034.pdf ├── #035 - Google │ ├── #035.pdf │ └── Solution.java ├── #036 - Dropbox │ └── #036.pdf ├── #037 - Google │ ├── #037.pdf │ └── Solution.java ├── #038 - Microsoft │ └── #038.pdf ├── #039 - Dropbox │ └── #039.pdf ├── #040 - Google │ └── #040.pdf ├── #041 - Facebook │ └── #041.pdf ├── #042 - Google │ ├── #042.pdf │ └── Solution.java ├── #043 - Amazon │ ├── #043.pdf │ └── Solution.java ├── #044 - Google │ └── #044.pdf ├── #045 - Two Sigma │ └── #045.pdf ├── #046 - Amazon │ ├── #046.pdf │ └── Solution.java ├── #047 - Facebook │ ├── #047.pdf │ └── Solution.java ├── #048 - Google │ └── #048.pdf ├── #049 - Amazon │ ├── #049.pdf │ └── Solution.java ├── #050 - Microsoft │ ├── #050.pdf │ └── Solution.java ├── #051 - Facebook │ └── #051.pdf ├── #052 - Google │ ├── #052.pdf │ └── Solution.java ├── #053 - Apple │ ├── #053.pdf │ └── Solution.java ├── #054 - Dropbox │ └── #054.pdf ├── #055 - Microsoft │ ├── #055.pdf │ └── Solution.java ├── #056 - Google │ └── #056.pdf ├── #057 - Amazon │ ├── #057.pdf │ └── Solution.java ├── #058 - Amazon │ ├── #058.pdf │ └── Solution.java ├── #059 - Google │ └── #059.pdf ├── #060 - Facebook │ └── #060.pdf ├── #061 - Google │ └── #061.pdf ├── #062 - Facebook │ ├── #062.pdf │ └── Solution.java ├── #063 - Microsoft │ ├── #063.pdf │ └── Solution.java ├── #064 - Google │ └── #064.pdf ├── #065 - Amazon │ ├── #065.pdf │ └── Solution.java ├── #066 - Square │ └── #066.pdf ├── #067 - Google │ ├── #067.pdf │ └── Solution.java ├── #068 - Google │ └── #068.pdf ├── #069 - Facebook │ ├── #069.pdf │ └── Solution.java ├── #070 - Microsoft │ └── #070.pdf ├── #071 - Two Sigma │ └── #071.pdf ├── #072 - Google │ └── #072.pdf ├── #073 - Google │ ├── #073.pdf │ └── Solution.java ├── #074 - Apple │ ├── #074.pdf │ └── Solution.java ├── #075 - Microsoft │ ├── #075.pdf │ └── Solution.java ├── #076 - Google │ └── #076.pdf ├── #077 - Snapchat │ ├── #077.pdf │ └── Solution.java ├── #078 - Google │ ├── #078.pdf │ └── Solution.java ├── #079 - Facebook │ ├── #079.pdf │ └── Solution.java ├── #080 - Google │ ├── #080.pdf │ └── Solution.java ├── #081 - Yelp │ ├── #081.pdf │ └── Solution.java ├── #082 - Microsoft │ └── #082.pdf ├── #083 - Google │ ├── #083.pdf │ └── Solution.java ├── #084 - Amazon │ ├── #084.pdf │ └── Solution.java ├── #085 - Facebook │ ├── #085.pdf │ └── Solution.java ├── #086 - Google │ ├── #086.pdf │ └── Solution.java ├── #087 - Uber │ └── #087.pdf ├── #088 - Context Logic │ └── #088.pdf ├── #089 - LinkedIn │ ├── #089.pdf │ └── Solution.java ├── #090 - Google │ └── #090.pdf ├── #091 - Dropbox │ └── #091.pdf ├── #092 - Airbnb │ ├── #092.pdf │ └── Solution.java ├── #093 - Apple │ ├── #093.pdf │ └── Solution.java ├── #094 - Google │ └── #094.pdf ├── #095 - Palantir │ ├── #095.pdf │ └── Solution.java ├── #096 - Microsoft │ ├── #096.pdf │ └── Solution.java ├── #097 - Stripe │ ├── #097.pdf │ └── Solution.java ├── #098 - Coursera │ ├── #098.pdf │ └── Solution.java └── #099 - Microsoft │ ├── #099.pdf │ └── Solution.java ├── #100 - #199 ├── #100 - Google │ ├── #100.pdf │ └── Solution.java ├── #101 - Alibaba │ ├── #101.pdf │ └── Solution.java ├── #102 - Lyft │ ├── #102.pdf │ └── Solution.java ├── #103 - Square │ ├── #103.pdf │ └── Solution.java ├── #104 - Google │ ├── #104.pdf │ └── Solution.java ├── #105 - Facebook │ └── #105.pdf ├── #106 - Pinterest │ ├── #106.pdf │ └── Solution.java ├── #107 - Microsoft │ ├── #107.pdf │ └── Solution.java ├── #108 - Google │ ├── #108.pdf │ └── Solution.java ├── #109 - Cisco │ ├── #109.pdf │ └── Solution.java ├── #110 - Facebook │ ├── #110.pdf │ └── Solution.java ├── #111 - Google │ ├── #111.pdf │ └── Solution.java ├── #112 - Twitter │ ├── #112.pdf │ └── Solution.java ├── #113 - Google │ ├── #113.pdf │ └── Solution.java ├── #114 - Facebook │ └── #114.pdf ├── #115 - Google │ ├── #115.pdf │ └── Solution.java ├── #116 - Jane Street │ ├── #116.pdf │ └── Solution.java ├── #117 - Facebook │ ├── #117.pdf │ └── Solution.java ├── #118 - Google │ ├── #118.pdf │ └── Solution.java ├── #119 - Google │ ├── #119.pdf │ └── Solution.java ├── #120 - Microsoft │ ├── #120.pdf │ └── Solution.java ├── #121 - Google │ └── #121.pdf ├── #122 - Zillow │ ├── #122.pdf │ └── Solution.java ├── #123 - LinkedIn │ ├── #123.pdf │ └── Solution.java ├── #124 - Microsoft │ └── #124.pdf ├── #125 - Google │ ├── #125.pdf │ └── Solution.java ├── #126 - Facebook │ ├── #126.pdf │ └── Solution.java ├── #127 - Microsoft │ ├── #127.pdf │ └── Solution.java ├── #128 │ └── #128.pdf ├── #129 │ ├── #129.pdf │ └── Solution.java ├── #130 - Facebook │ ├── #130.pdf │ └── Solution.java ├── #131 - Snapchat │ ├── #131.pdf │ └── Solution.java ├── #132 - Riot Games │ ├── #132.pdf │ └── Solution.java ├── #133 - Amazon │ ├── #133.pdf │ └── Solution.java ├── #134 - Facebook │ ├── #134.pdf │ └── Solution.java ├── #135 - Apple │ ├── #135.pdf │ └── Solution.java ├── #136 - Google │ ├── #136.pdf │ └── Solution.java ├── #137 - Amazon │ ├── #137.pdf │ └── Solution.java ├── #138 - Google │ ├── #138.pdf │ └── Solution.java ├── #139 - Google │ ├── #139.pdf │ └── Solution.java ├── #140 - Facebook │ └── #140.pdf ├── #141 - Microsoft │ └── #141.pdf ├── #142 - Google │ ├── #142.pdf │ └── Solution.java ├── #143 - Amazon │ ├── #143.pdf │ └── Solution.java ├── #144 - Google │ ├── #144.pdf │ └── Solution.java ├── #145 - Google │ ├── #145.pdf │ └── Solution.java ├── #146 - BufferBox │ ├── #146.pdf │ └── Solution.java ├── #147 │ ├── #147.pdf │ └── Solution.java ├── #148 - Apple │ ├── #148.pdf │ └── Solution.java ├── #149 - Goldman Sachs │ ├── #149.pdf │ └── Solution.java ├── #150 - LinkedIn │ ├── #150.pdf │ └── Solution.java ├── #151 │ ├── #151.pdf │ └── Solution.java ├── #152 - Triplebyte │ └── #152.pdf ├── #153 │ ├── #153.pdf │ └── Solution.java ├── #154 - Amazon │ ├── #154.pdf │ └── Solution.java ├── #155 - MongoDB │ ├── #155.pdf │ └── Solution.java ├── #156 - Facebook │ ├── #156.pdf │ └── Solution.java ├── #157 - Amazon │ ├── #157.pdf │ └── Solution.java ├── #158 - Slack │ ├── #158.pdf │ └── Solution.java ├── #159 - Google │ ├── #159.pdf │ └── Solution.java ├── #160 - Uber │ ├── #160.pdf │ └── Solution.java ├── #161 - Facebook │ ├── #161.pdf │ └── Solution.java ├── #162 - Square │ ├── #162.pdf │ └── Solution.java ├── #163 - Jane Street │ ├── #163.pdf │ └── Solution.java ├── #164 - Google │ ├── #164.pdf │ └── Solution.java ├── #165 - Google │ ├── #165.pdf │ └── Solution.java ├── #166 - Uber │ ├── #166.pdf │ └── Solution.java ├── #167 - Airbnb │ ├── #167.pdf │ └── Solution.java ├── #168 - Facebook │ ├── #168.pdf │ └── Solution.java ├── #169 - Google │ └── #169.pdf ├── #170 - Facebook │ ├── #170.pdf │ └── Solution.java ├── #171 - Amazon │ ├── #171.pdf │ └── Solution.java ├── #172 - Dropbox │ └── #172.pdf ├── #173 - Stripe │ ├── #173.pdf │ └── Solution.java ├── #174 - Microsoft │ └── #174.pdf ├── #175 - Google │ └── #175.pdf ├── #176 - Bloomberg │ ├── #176.pdf │ └── Solution.java ├── #177 - Airbnb │ ├── #177.pdf │ └── Solution.java ├── #178 - Two Sigma │ └── #178.pdf ├── #179 - Google │ └── #179.pdf ├── #180 - Google │ ├── #180.pdf │ └── Solution.java ├── #181 - Google │ ├── #181.pdf │ └── Solution.java ├── #182 - Facebook │ ├── #182.pdf │ └── Solution.java ├── #183 - Twitch │ └── #183.pdf ├── #184 - Amazon │ ├── #184.pdf │ └── Solution.java ├── #185 - Google │ └── #185.pdf ├── #186 - Microsoft │ └── #186.pdf ├── #187 - Google │ └── #187.pdf ├── #188 - Google │ └── #188.pdf ├── #189 - Google │ ├── #189.pdf │ └── Solution.java ├── #190 - Facebook │ ├── #190.pdf │ └── Solution.java ├── #191 - Stripe │ ├── #191.pdf │ └── Solution.java ├── #192 - Google │ ├── #192.pdf │ └── Solution.java ├── #193 - Affirm │ ├── #193.pdf │ └── Solution.java ├── #194 - Facebook │ └── #194.pdf ├── #195 - Google │ ├── #195.pdf │ └── Solution.java ├── #196 - Apple │ ├── #196.pdf │ └── Solution.java ├── #197 - Amazon │ ├── #197.pdf │ └── Solution.java ├── #198 - Google │ ├── #198.pdf │ └── Solution.java └── #199 - Facebook │ ├── #199.pdf │ └── Solution.java ├── #200 - #299 ├── #200 - Microsoft │ ├── #200.pdf │ └── Solution.java ├── #201 - Google │ ├── #201.pdf │ └── Solution.java ├── #202 - Palantir │ ├── #202.pdf │ └── Solution.java ├── #203 - Uber │ ├── #203.pdf │ └── Solution.java ├── #204 - Amazon │ ├── #204.pdf │ └── Solution.java ├── #205 - IBM │ └── #205.pdf ├── #206 - Twitter │ └── #206.pdf ├── #207 - Dropbox │ └── #207.pdf ├── #208 - LinkedIn │ └── #208.pdf ├── #209 - YouTube │ └── #209.pdf ├── #210 - Apple │ └── #210.pdf ├── #211 - Microsoft │ └── #211.pdf ├── #212 - Dropbox │ ├── #212.pdf │ └── Solution.java ├── #213 - Snapchat │ └── #213.pdf ├── #214 - Stripe │ └── #214.pdf ├── #215 - Yelp │ └── #215.pdf ├── #216 - Facebook │ └── #216.pdf ├── #217 - Oracle │ └── #217.pdf ├── #218 - Yahoo │ ├── #218.pdf │ └── Solution.java ├── #219 - Salesforce │ └── #219.pdf ├── #220 - Square │ └── #220.pdf ├── #221 - Zillow │ └── #221.pdf ├── #222 - Quora │ └── #222.pdf ├── #223 - Palantir │ └── #223.pdf ├── #224 - Amazon │ ├── #224.pdf │ └── Solution.java ├── #225 - Bloomberg │ └── #225.pdf ├── #226 - Airbnb │ ├── #226.pdf │ └── Solution.java ├── #227 - Facebook │ └── #227.pdf ├── #228 - Twitter │ └── #228.pdf ├── #229 - Flipkart │ └── #229.pdf ├── #230 - Goldman Sachs │ └── #230.pdf ├── #231 - IBM │ ├── #231.pdf │ └── Solution.java ├── #232 - Google │ ├── #232.pdf │ └── Solution.java ├── #233 - Apple │ ├── #233.pdf │ └── Solution.java ├── #234 - Microsoft │ └── #234.pdf ├── #235 - Facebook │ ├── #235.pdf │ └── Solution.java ├── #236 - Nvidia │ └── #236.pdf ├── #237 - Amazon │ ├── #237.pdf │ └── Solution.java ├── #238 - Mit │ └── #238.pdf ├── #239 - Uber │ └── #239.pdf ├── #240 - Spotify │ └── #240.pdf ├── #241 - Palantir │ ├── #241.pdf │ └── Solution.java ├── #242 - Twitter │ └── #242.pdf ├── #243 - Etsy │ └── #243.pdf ├── #244 - Square │ ├── #244.pdf │ └── Solution.java ├── #245 - Yelp │ └── #245.pdf └── #246 - Dropbox │ ├── #246.pdf │ └── Solution.java ├── .gitignore ├── LICENSE ├── README.md └── dcp.jpg /#000 - #099/#001 - Google/#001.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#001 - Google/#001.pdf -------------------------------------------------------------------------------- /#000 - #099/#001 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 28.11.2018 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | int[] arr = { 10, 15, 3, 7 }; 12 | System.out.println(check(arr, 17)); 13 | } 14 | 15 | public static boolean check(int[] arr, int k) { 16 | if (arr == null || arr.length < 2) 17 | return false; 18 | 19 | Set tmp = new HashSet<>(); 20 | 21 | for (int val : arr) { 22 | if (tmp.contains(val)) 23 | return true; 24 | tmp.add(k - val); 25 | } 26 | 27 | return false; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#000 - #099/#002 - Uber/#002.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#002 - Uber/#002.pdf -------------------------------------------------------------------------------- /#000 - #099/#002 - Uber/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 28.11.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(product(new int[] { 1, 2, 3, 4, 5 }))); 11 | System.out.println(Arrays.toString(product(new int[] { 3, 2, 1 }))); 12 | } 13 | 14 | public static int[] product(int[] arr) { 15 | if (arr == null || arr.length < 2) 16 | return null; 17 | 18 | int[] res = new int[arr.length]; 19 | long product = 1; 20 | 21 | for (int val : arr) 22 | product *= val; 23 | 24 | for (int i = 0; i < arr.length; i++) 25 | res[i] = (int)(product / arr[i]); 26 | 27 | return res; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#000 - #099/#003 - Google/#003.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#003 - Google/#003.pdf -------------------------------------------------------------------------------- /#000 - #099/#004 - Stripe/#004.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#004 - Stripe/#004.pdf -------------------------------------------------------------------------------- /#000 - #099/#004 - Stripe/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 29.11.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findLowestMissingPositive1(new int[] { 3, 4, -1, 1 })); 9 | System.out.println(findLowestMissingPositive1(new int[] { 1, 2, 0 })); 10 | } 11 | 12 | public static int findLowestMissingPositive1(int[] arr) { 13 | if (arr == null || arr.length < 2) 14 | return -1; 15 | 16 | for (int i = 0; i < arr.length; ) { 17 | if (arr[i] <= 0 || arr[i] > arr.length || arr[i] - 1 == i) 18 | i++; 19 | else 20 | swap(arr, i, arr[i] - 1); 21 | } 22 | 23 | for (int i = 0; i < arr.length; i++) 24 | if (arr[i] - 1 != i) 25 | return i + 1; 26 | 27 | return -1; 28 | } 29 | 30 | private static void swap(int[] arr, int i, int j) { 31 | int tmp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = tmp; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /#000 - #099/#005 - Jane Street/#005.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#005 - Jane Street/#005.pdf -------------------------------------------------------------------------------- /#000 - #099/#005 - Jane Street/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.function.Function; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 29.11.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(car(cons(3, 4))); 11 | System.out.println(cdr(cons(3, 4))); 12 | } 13 | 14 | private static Function, Integer> cons(int a, int b) { 15 | return func -> func.apply(new Pair(a, b)); 16 | } 17 | 18 | private static int car(Function, Integer> cons) { 19 | return cons.apply(pair -> pair.a); 20 | } 21 | 22 | private static int cdr(Function, Integer> cons) { 23 | return cons.apply(pair -> pair.b); 24 | } 25 | 26 | private static class Pair { 27 | 28 | int a; 29 | int b; 30 | 31 | public Pair(int a, int b) { 32 | this.a = a; 33 | this.b = b; 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#000 - #099/#006 - Google/#006.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#006 - Google/#006.pdf -------------------------------------------------------------------------------- /#000 - #099/#007 - Facebook/#007.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#007 - Facebook/#007.pdf -------------------------------------------------------------------------------- /#000 - #099/#007 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 29.11.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(count("1")); // 1 9 | System.out.println(count("11")); // 2 10 | System.out.println(count("111")); // 3 11 | System.out.println(count("1111")); // 5 12 | } 13 | 14 | public static int count(String msg) { 15 | if (msg == null || msg.isEmpty()) 16 | return 0; 17 | if (msg.length() == 1) 18 | return 1; 19 | if (msg.length() == 2) 20 | return 2; 21 | return count(msg.substring(1)) + count(msg.substring(2)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /#000 - #099/#008 - Google/#008.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#008 - Google/#008.pdf -------------------------------------------------------------------------------- /#000 - #099/#008 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 07.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node node = createTree(); 9 | System.out.println(countUnivalSubtrees(node)); 10 | } 11 | 12 | private static Node createTree() { 13 | Node a = new Node(1); 14 | a.left = new Node(1); 15 | a.right = new Node(1); 16 | 17 | Node b = new Node(0); 18 | b.left = a; 19 | b.right = new Node(0); 20 | 21 | 22 | Node root = new Node(0); 23 | root.left = new Node(1); 24 | root.right = b; 25 | 26 | return root; 27 | } 28 | 29 | private static class Node { 30 | 31 | private final int val; 32 | private Node left; 33 | private Node right; 34 | 35 | public Node(int val) { 36 | this.val = val; 37 | } 38 | } 39 | 40 | public static int countUnivalSubtrees(Node node) { 41 | if (node == null) 42 | return 0; 43 | 44 | int count = countUnivalSubtrees(node.left); 45 | count += countUnivalSubtrees(node.right); 46 | 47 | return isUnivalTree(node) ? count + 1 : count; 48 | } 49 | 50 | private static boolean isUnivalTree(Node node) { 51 | if (node == null) 52 | return true; 53 | if (node.left != null && node.left.val != node.val) 54 | return false; 55 | if (node.right != null && node.right.val != node.val) 56 | return false; 57 | return isUnivalTree(node.left) && isUnivalTree(node.right); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /#000 - #099/#009 - Airbnb/#009.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#009 - Airbnb/#009.pdf -------------------------------------------------------------------------------- /#000 - #099/#009 - Airbnb/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 08.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 2, 4, 6, 2, 5 })); // 13 9 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 5, 1, 1, 5 })); // 10 10 | System.out.println(largesNonAdjacentNumbersSumb(new int[] { 5, 1, -1, -1, 5 })); // 10 11 | } 12 | 13 | public static int largesNonAdjacentNumbersSumb(int[] arr) { 14 | if (arr == null || arr.length == 0) 15 | return 0; 16 | if (arr.length == 1) 17 | return arr[0]; 18 | if (arr.length == 2) 19 | return Math.max(arr[0], arr[1]); 20 | 21 | for (int i = 2; i < arr.length; i++) { 22 | if (i > 2) 23 | arr[i] = Math.max(arr[i - 2], arr[i - 3]) + Math.max(0, arr[i]); 24 | else 25 | arr[i] = arr[i - 2] + Math.max(0, arr[i]); 26 | } 27 | 28 | return Math.max(arr[arr.length - 1], arr[arr.length - 2]); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /#000 - #099/#010 - Apple/#010.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#010 - Apple/#010.pdf -------------------------------------------------------------------------------- /#000 - #099/#010 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.TimeUnit; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 23.05.2020 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | long fromTIme = System.currentTimeMillis(); 11 | new JobScheduler().task(() -> System.out.println(System.currentTimeMillis() - fromTIme), TimeUnit.SECONDS.toMillis(5)); 12 | System.out.println("-- done --"); 13 | } 14 | 15 | public static final class JobScheduler { 16 | 17 | public void task(Runnable func, long delay) { 18 | new Thread(() -> { 19 | try { 20 | Thread.sleep(delay); 21 | func.run(); 22 | } catch(InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | }).start(); 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#000 - #099/#011 - Twitter/#011.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#011 - Twitter/#011.pdf -------------------------------------------------------------------------------- /#000 - #099/#012 - Amazon/#012.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#012 - Amazon/#012.pdf -------------------------------------------------------------------------------- /#000 - #099/#012 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 27.01.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(countUniqueWays(1, 1, 2)); // 1 11 | System.out.println(countUniqueWays(2, 1, 2)); // 2 12 | System.out.println(countUniqueWays(3, 1, 2)); // 3 13 | System.out.println(countUniqueWays(4, 1, 2)); // 5 14 | 15 | System.out.println(countUniqueWays(4, 1, 3, 5)); // 3 16 | } 17 | 18 | public static int countUniqueWays(int n, int... steps) { 19 | Arrays.sort(steps); 20 | return _countUniqueWays(n, 0, 0, steps); 21 | } 22 | 23 | private static int _countUniqueWays(int n, int cur, int res, int... steps) { 24 | if (cur == n) 25 | res++; 26 | 27 | if (cur < n) { 28 | for (int i = 0; i < steps.length; i++) 29 | res = _countUniqueWays(n, cur + steps[i], res, steps); 30 | } 31 | 32 | return res; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /#000 - #099/#013 - Amazon/#013.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#013 - Amazon/#013.pdf -------------------------------------------------------------------------------- /#000 - #099/#013 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.HashSet; 3 | import java.util.Map; 4 | import java.util.Set; 5 | 6 | /** 7 | * @author Oleg Cherednik 8 | * @since 27.01.2019 9 | */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | System.out.println(findLongestSubstring("abcba", 2)); // bcb 14 | } 15 | 16 | public static String findLongestSubstring(String str, int k) { 17 | Map> map = new HashMap<>(); 18 | String res = null; 19 | 20 | for (int i = 0, j = 0; j < str.length(); ) { 21 | if (map.containsKey(str.charAt(j))) 22 | map.get(str.charAt(j)).add(j++); 23 | else if (map.size() < k) { 24 | map.put(str.charAt(j), new HashSet<>()); 25 | map.get(str.charAt(j)).add(j++); 26 | } else { 27 | if (res == null || j - i + 1 > res.length()) 28 | res = str.substring(i, j); 29 | 30 | map.get(str.charAt(i)).remove(i); 31 | 32 | if (map.get(str.charAt(i)).isEmpty()) 33 | map.remove(str.charAt(i)); 34 | 35 | i++; 36 | } 37 | } 38 | 39 | return res; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /#000 - #099/#014 - Google/#014.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#014 - Google/#014.pdf -------------------------------------------------------------------------------- /#000 - #099/#015 - Facebook/#015.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#015 - Facebook/#015.pdf -------------------------------------------------------------------------------- /#000 - #099/#016 - Twitter/#016.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#016 - Twitter/#016.pdf -------------------------------------------------------------------------------- /#000 - #099/#017 - Google/#017.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#017 - Google/#017.pdf -------------------------------------------------------------------------------- /#000 - #099/#018 - Google/#018.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#018 - Google/#018.pdf -------------------------------------------------------------------------------- /#000 - #099/#018 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Comparator; 3 | import java.util.PriorityQueue; 4 | import java.util.Queue; 5 | 6 | /** 7 | * @author Oleg Cherednik 8 | * @since 24.05.2020 9 | */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | System.out.println(Arrays.toString(maxSubArr(new int[] { 10, 5, 2, 7, 8, 7 }, 3))); // [10, 7, 8, 8] 14 | } 15 | 16 | public static int[] maxSubArr(int[] arr, int k) { 17 | int[] res = new int[arr.length - k + 1]; 18 | Queue maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); 19 | 20 | for (int i = 0, j = 0, m = 0; j < arr.length; ) { 21 | maxHeap.add(arr[j++]); 22 | 23 | if (maxHeap.size() > k) 24 | maxHeap.remove(arr[i++]); 25 | if (maxHeap.size() == k) 26 | res[m++] = maxHeap.element(); 27 | } 28 | 29 | return res; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#000 - #099/#019 - Facebook/#019.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#019 - Facebook/#019.pdf -------------------------------------------------------------------------------- /#000 - #099/#019 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 30.09.2020 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(buildHouses(new int[][] { 11 | { 3, 4, 9, 10 }, 12 | { 2, 5, 8, 11 }, 13 | { 1, 6, 7, 12 } })); // 9 (1 + 5 + 3) 14 | } 15 | 16 | public static int buildHouses(int[][] matrix) { 17 | int totalHouses = matrix.length; 18 | int totalColors = matrix[0].length; 19 | int[] dp = new int[totalColors]; 20 | 21 | for (int house = 0; house < totalHouses; house++) 22 | dp = calc(dp, house, matrix, totalColors); 23 | 24 | return Arrays.stream(dp).min().orElse(-1); 25 | } 26 | 27 | private static int[] calc(int[] dp, int house, int[][] matrix, int totalColors) { 28 | int[] arr = new int[totalColors]; 29 | 30 | for (int color = 0; color < totalColors; color++) 31 | arr[color] = getMin(dp, color) + matrix[house][color]; 32 | 33 | return arr; 34 | } 35 | 36 | private static int getMin(int[] dp, int color) { 37 | int min = Integer.MAX_VALUE; 38 | 39 | for (int i = 0; i < dp.length; i++) 40 | if (i != color) 41 | min = Math.min(min, dp[i]); 42 | 43 | return min; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /#000 - #099/#020 - Google/#020.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#020 - Google/#020.pdf -------------------------------------------------------------------------------- /#000 - #099/#021 - Snapchat/#021.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#021 - Snapchat/#021.pdf -------------------------------------------------------------------------------- /#000 - #099/#021 - Snapchat/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.TreeMap; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 30.01.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(getRequiredRoomsAmount(new Interval[]{ 12 | new Interval(30, 75), 13 | new Interval(0, 50), 14 | new Interval(60, 150)})); 15 | } 16 | 17 | private static final class Interval { 18 | private final int start; 19 | private final int end; 20 | 21 | public Interval(int start, int end) { 22 | this.start = start; 23 | this.end = end; 24 | } 25 | } 26 | 27 | public static int getRequiredRoomsAmount(Interval[] intervals) { 28 | Map map = new TreeMap<>(); 29 | 30 | for (Interval interval : intervals) { 31 | map.put(interval.start, map.getOrDefault(interval.start, 0) + 1); 32 | map.put(interval.end, map.getOrDefault(interval.end, 0) - 1); 33 | } 34 | 35 | int res = 0; 36 | int count = 0; 37 | 38 | for (int delta : map.values()) 39 | res = Math.max(res, count += delta); 40 | 41 | return res; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /#000 - #099/#022 - Microsoft/#022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#022 - Microsoft/#022.pdf -------------------------------------------------------------------------------- /#000 - #099/#022 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.stream.Collectors; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 31.01.2019 7 | */ 8 | public class Solution { 9 | public static void main(String[] args) { 10 | System.out.println(splitWords(words("quick", "brown", "the", "fox"), "thequickbrownfox")); 11 | System.out.println(splitWords(words("bed", "bath", "bedbath", "and", "beyond"), "bedbathandbeyond")); 12 | System.out.println(splitWords(words("the", "theremin"), "theremin")); 13 | System.out.println(splitWords(words("a", "aa", "aaa", "aaaa", "aaaaa"), "aaaaab")); 14 | } 15 | 16 | private static Set words(String... words) { 17 | return Arrays.stream(words).collect(Collectors.toSet()); 18 | } 19 | 20 | public static List splitWords(Set words, String str) { 21 | Deque queue = new LinkedList<>(); 22 | splitWords(queue, words, str, 0); 23 | return queue.isEmpty() ? null : new ArrayList<>(queue); 24 | } 25 | 26 | private static boolean splitWords(Deque queue, Set words, String str, int offs) { 27 | if (offs >= str.length()) 28 | return true; 29 | 30 | for (String word : words) { 31 | if (!str.startsWith(word, offs)) 32 | continue; 33 | 34 | queue.addLast(word); 35 | 36 | if (splitWords(queue, words, str, offs + word.length())) 37 | return true; 38 | 39 | queue.removeLast(); 40 | } 41 | 42 | return false; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /#000 - #099/#023 - Google/#023.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#023 - Google/#023.pdf -------------------------------------------------------------------------------- /#000 - #099/#024 - Google/#024.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#024 - Google/#024.pdf -------------------------------------------------------------------------------- /#000 - #099/#025 - Facebook/#025.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#025 - Facebook/#025.pdf -------------------------------------------------------------------------------- /#000 - #099/#025 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 29.11.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(isMatches("abcdefg", "ab*ef.")); // true 9 | System.out.println(isMatches("abcdefg", ".***.e*")); // true 10 | System.out.println(isMatches("ray", "ra.")); // true 11 | System.out.println(isMatches("chats", "ra.")); // false 12 | System.out.println(isMatches("chats", "chat.")); // true 13 | } 14 | 15 | public static boolean isMatches(String str, String regexp) { 16 | return isMatches(str, 0, regexp, 0); 17 | } 18 | 19 | private static boolean isMatches(String str, int i, String regexp, int j) { 20 | if (j >= regexp.length()) 21 | return i >= str.length(); 22 | 23 | if (regexp.charAt(j) == '*') { 24 | if (j + 1 == regexp.length()) 25 | return true; 26 | if (isMatches(str, i, regexp, j + 1)) 27 | return true; 28 | 29 | for (int k = 1; k < str.length(); k++) 30 | if (isMatches(str, i + k, regexp, j + 1)) 31 | return true; 32 | } else if (str.charAt(i) == regexp.charAt(j) || regexp.charAt(j) == '.') 33 | return isMatches(str, i + 1, regexp, j + 1); 34 | 35 | return false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /#000 - #099/#026 - Google/#026.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#026 - Google/#026.pdf -------------------------------------------------------------------------------- /#000 - #099/#027 - Facebook/#027.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#027 - Facebook/#027.pdf -------------------------------------------------------------------------------- /#000 - #099/#027 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 01.02.2018 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(isBalanced("([])[]({})")); // true 12 | System.out.println(isBalanced("([)]")); // false 13 | System.out.println(isBalanced("((()")); // false 14 | } 15 | 16 | public static boolean isBalanced(String str) { 17 | Deque stack = new LinkedList<>(); 18 | 19 | for (int i = 0; i < str.length(); i++) { 20 | char ch = str.charAt(i); 21 | 22 | if (ch == '(' || ch == '[' || ch == '{') 23 | stack.push(ch); 24 | else { 25 | if (stack.isEmpty()) 26 | return false; 27 | 28 | char prv = stack.pop(); 29 | 30 | if (prv == '(' && ch != ')') 31 | return false; 32 | if (prv == '[' && ch != ']') 33 | return false; 34 | if (prv == '{' && ch != '}') 35 | return false; 36 | } 37 | } 38 | 39 | return stack.isEmpty(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /#000 - #099/#028 - Palantir/#028.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#028 - Palantir/#028.pdf -------------------------------------------------------------------------------- /#000 - #099/#029 - Amazon/#029.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#029 - Amazon/#029.pdf -------------------------------------------------------------------------------- /#000 - #099/#029 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 01.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(encode("AAAABBBCCDAA")); // 4A3B2C1D2A 9 | System.out.println(decode("4A3B2C1D2A")); // AAAABBBCCDAA 10 | } 11 | 12 | public static String encode(String str) { 13 | StringBuilder buf = new StringBuilder(str.length()); 14 | char ch = '\0'; 15 | int length = 0; 16 | 17 | 18 | for (int i = 0; i < str.length(); i++) { 19 | if (ch == '\0' || ch == str.charAt(i)) { 20 | ch = str.charAt(i); 21 | length++; 22 | } else { 23 | buf.append(length).append(ch); 24 | ch = str.charAt(i); 25 | length = 1; 26 | } 27 | } 28 | 29 | if (ch != '\0') 30 | buf.append(length).append(ch); 31 | 32 | return buf.toString(); 33 | } 34 | 35 | public static String decode(String str) { 36 | StringBuilder buf = new StringBuilder(); 37 | 38 | for (int i = 0; i < str.length(); ) { 39 | int length = str.charAt(i++) - '0'; 40 | char ch = str.charAt(i++); 41 | 42 | for (int j = 0; j < length; j++) 43 | buf.append(ch); 44 | } 45 | 46 | return buf.toString(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /#000 - #099/#030 - Facebook/#030.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#030 - Facebook/#030.pdf -------------------------------------------------------------------------------- /#000 - #099/#031 - Google/#031.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#031 - Google/#031.pdf -------------------------------------------------------------------------------- /#000 - #099/#031 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 01.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getEditDistance("kitten", "sitting")); 9 | System.out.println(getEditDistance("foo", "bar")); 10 | System.out.println(getEditDistance("money", "rooney")); 11 | } 12 | 13 | public static int getEditDistance(String one, String two) { 14 | int[] arr = new int[26]; 15 | 16 | for (int i = 0; i < one.length(); i++) 17 | arr[one.charAt(i) - 'a']++; 18 | 19 | for (int i = 0; i < two.length(); i++) 20 | arr[two.charAt(i) - 'a']--; 21 | 22 | int add = 0; 23 | int sub = 0; 24 | 25 | for (int delta : arr) { 26 | if (delta > 0) 27 | add += delta; 28 | else if (delta < 0) 29 | sub += Math.abs(delta); 30 | } 31 | 32 | return Math.max(add, sub); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /#000 - #099/#032 - Jane Street/#032.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#032 - Jane Street/#032.pdf -------------------------------------------------------------------------------- /#000 - #099/#033 - Microsoft/#033.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#033 - Microsoft/#033.pdf -------------------------------------------------------------------------------- /#000 - #099/#034 - Quora/#034.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#034 - Quora/#034.pdf -------------------------------------------------------------------------------- /#000 - #099/#035 - Google/#035.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#035 - Google/#035.pdf -------------------------------------------------------------------------------- /#000 - #099/#035 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 02.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | char[] arr = { 'G', 'B', 'R', 'R', 'B', 'R', 'G' }; 11 | System.out.println(Arrays.toString(arr)); 12 | sort(arr); 13 | System.out.println(Arrays.toString(arr)); 14 | } 15 | 16 | public static void sort(char... arr) { 17 | int r = 0; 18 | int b = arr.length - 1; 19 | int i = 0; 20 | 21 | while (i <= b) { 22 | if (arr[i] == 'R') 23 | swap(arr, i++, r++); 24 | else if (arr[i] == 'B') 25 | swap(arr, i, b--); 26 | else 27 | i++; 28 | } 29 | } 30 | 31 | private static void swap(char[] arr, int i, int j) { 32 | char ch = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = ch; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#000 - #099/#036 - Dropbox/#036.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#036 - Dropbox/#036.pdf -------------------------------------------------------------------------------- /#000 - #099/#037 - Google/#037.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#037 - Google/#037.pdf -------------------------------------------------------------------------------- /#000 - #099/#037 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 02.02.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | getPowerSet(new int[] { 1, 2, 3 }).forEach(arr -> System.out.println(Arrays.toString(arr))); 13 | } 14 | 15 | public static List getPowerSet(int[] arr) { 16 | List res = new ArrayList<>(); 17 | res.add(new int[0]); 18 | 19 | for (int length = 1; length <= arr.length; length++) { 20 | for (int i = 0; i <= arr.length - length; i++) { 21 | int[] tmp = new int[length]; 22 | System.arraycopy(arr, i, tmp, 0, length); 23 | res.add(tmp); 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#000 - #099/#038 - Microsoft/#038.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#038 - Microsoft/#038.pdf -------------------------------------------------------------------------------- /#000 - #099/#039 - Dropbox/#039.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#039 - Dropbox/#039.pdf -------------------------------------------------------------------------------- /#000 - #099/#040 - Google/#040.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#040 - Google/#040.pdf -------------------------------------------------------------------------------- /#000 - #099/#041 - Facebook/#041.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#041 - Facebook/#041.pdf -------------------------------------------------------------------------------- /#000 - #099/#042 - Google/#042.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#042 - Google/#042.pdf -------------------------------------------------------------------------------- /#000 - #099/#042 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Deque; 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 02.02.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(Arrays.toString(getSubset(new int[] { 12, 1, 61, 5, 9, 2 }, 24))); 13 | } 14 | 15 | public static int[] getSubset(int[] arr, int k) { 16 | Deque stack = new LinkedList<>(); 17 | find(arr, 0, k, stack); 18 | return stack.stream().mapToInt(Integer::intValue).toArray(); 19 | } 20 | 21 | private static boolean find(int[] arr, int i, int k, Deque stack) { 22 | if (k == 0) 23 | return true; 24 | if (k < 0 || i >= arr.length) 25 | return false; 26 | 27 | for (; i < arr.length; i++) { 28 | stack.push(arr[i]); 29 | 30 | if (find(arr, i + 1, k - arr[i], stack)) 31 | return true; 32 | 33 | stack.pop(); 34 | } 35 | 36 | return false; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /#000 - #099/#043 - Amazon/#043.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#043 - Amazon/#043.pdf -------------------------------------------------------------------------------- /#000 - #099/#044 - Google/#044.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#044 - Google/#044.pdf -------------------------------------------------------------------------------- /#000 - #099/#045 - Two Sigma/#045.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#045 - Two Sigma/#045.pdf -------------------------------------------------------------------------------- /#000 - #099/#046 - Amazon/#046.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#046 - Amazon/#046.pdf -------------------------------------------------------------------------------- /#000 - #099/#047 - Facebook/#047.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#047 - Facebook/#047.pdf -------------------------------------------------------------------------------- /#000 - #099/#047 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 28.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getMaxProfit(new int[] { 9, 11, 8, 5, 7, 10 })); // 5 9 | } 10 | 11 | public static int getMaxProfit(int[] stockPrices) { 12 | int maxProfit = 0; 13 | int min = stockPrices[0]; 14 | 15 | for (int day = 1; day < stockPrices.length; day++) { 16 | int curMaxProfit = stockPrices[day] - min; 17 | maxProfit = day == 1 ? curMaxProfit : Math.max(maxProfit, curMaxProfit); 18 | min = Math.min(min, stockPrices[day]); 19 | } 20 | 21 | return maxProfit; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /#000 - #099/#048 - Google/#048.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#048 - Google/#048.pdf -------------------------------------------------------------------------------- /#000 - #099/#049 - Amazon/#049.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#049 - Amazon/#049.pdf -------------------------------------------------------------------------------- /#000 - #099/#049 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 03.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findMaxSum(34, -50, 42, 14, -5, 86)); // 137 9 | System.out.println(findMaxSum(-5, -1, -8, -9)); // 0 10 | } 11 | 12 | public static int findMaxSum(int... arr) { 13 | int maxSum = 0; 14 | 15 | for (int i = 0, sum = 0; i < arr.length; i++) { 16 | maxSum = Math.max(maxSum, arr[i]); 17 | maxSum = Math.max(maxSum, sum = Math.max(sum + arr[i], 0)); 18 | } 19 | 20 | return maxSum; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /#000 - #099/#050 - Microsoft/#050.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#050 - Microsoft/#050.pdf -------------------------------------------------------------------------------- /#000 - #099/#051 - Facebook/#051.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#051 - Facebook/#051.pdf -------------------------------------------------------------------------------- /#000 - #099/#052 - Google/#052.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#052 - Google/#052.pdf -------------------------------------------------------------------------------- /#000 - #099/#052 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedHashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 03.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | LRUCache cache = new LRUCache<>(3); 12 | cache.set("one", "one1"); 13 | cache.set("two", "two1"); 14 | cache.set("three", "three1"); 15 | cache.set("four", "four1"); 16 | System.out.println(cache.get("two")); // two1 17 | System.out.println(cache.get("one")); // null 18 | 19 | cache.set("five", "five1"); 20 | System.out.println(cache.get("two")); // two1 21 | System.out.println(cache.get("three")); // null 22 | 23 | cache.set("four", "four2"); 24 | System.out.println(cache.get("four")); // four2 25 | } 26 | 27 | public static final class LRUCache { 28 | private final Map map = new LinkedHashMap<>(); 29 | private final int maxSize; 30 | 31 | public LRUCache(int maxSize) { 32 | this.maxSize = maxSize; 33 | } 34 | 35 | public void set(K key, V value) { 36 | if (map.containsKey(key)) 37 | map.remove(key); 38 | else if (map.size() == maxSize) 39 | map.remove(map.keySet().iterator().next()); 40 | 41 | map.put(key, value); 42 | } 43 | 44 | public V get(K key) { 45 | if (!map.containsKey(key)) 46 | return null; 47 | 48 | V value = map.remove(key); 49 | map.put(key, value); 50 | return value; 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /#000 - #099/#053 - Apple/#053.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#053 - Apple/#053.pdf -------------------------------------------------------------------------------- /#000 - #099/#053 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 03.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | Queue queue = new Queue<>(); 12 | 13 | queue.enqueue(1); 14 | queue.enqueue(2); 15 | queue.enqueue(3); 16 | queue.enqueue(4); 17 | System.out.println(queue.dequeue()); // 1 18 | System.out.println(queue.dequeue()); // 2 19 | 20 | queue.enqueue(5); 21 | queue.enqueue(6); 22 | queue.enqueue(7); 23 | queue.enqueue(8); 24 | 25 | System.out.println(queue.dequeue()); // 3 26 | System.out.println(queue.dequeue()); // 4 27 | System.out.println(queue.dequeue()); // 5 28 | System.out.println(queue.dequeue()); // 6 29 | System.out.println(queue.dequeue()); // 7 30 | System.out.println(queue.dequeue()); // 8 31 | } 32 | 33 | public static class Queue { 34 | private final Deque stackEnqueue = new LinkedList<>(); 35 | private final Deque stackDequeue = new LinkedList<>(); 36 | 37 | public void enqueue(V value) { 38 | stackEnqueue.push(value); 39 | } 40 | 41 | public V dequeue() { 42 | if (stackDequeue.isEmpty()) 43 | while (!stackEnqueue.isEmpty()) 44 | stackDequeue.push(stackEnqueue.remove()); 45 | 46 | return stackDequeue.isEmpty() ? null : stackDequeue.remove(); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /#000 - #099/#054 - Dropbox/#054.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#054 - Dropbox/#054.pdf -------------------------------------------------------------------------------- /#000 - #099/#055 - Microsoft/#055.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#055 - Microsoft/#055.pdf -------------------------------------------------------------------------------- /#000 - #099/#056 - Google/#056.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#056 - Google/#056.pdf -------------------------------------------------------------------------------- /#000 - #099/#057 - Amazon/#057.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#057 - Amazon/#057.pdf -------------------------------------------------------------------------------- /#000 - #099/#057 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 31.10.2018 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | List res = splitIntoWords("the quick brown fox jumps over the lazy dog", 10); 13 | System.out.println(res != null ? Arrays.toString(res.toArray(new String[0])) : null); 14 | } 15 | 16 | public static List splitIntoWords(String str, int k) { 17 | String[] words = str.split("\\s+"); 18 | List res = new ArrayList<>(words.length); 19 | StringBuilder buf = null; 20 | 21 | for (String word : words) { 22 | if (word.length() > k) 23 | return null; 24 | if (buf == null) 25 | buf = new StringBuilder(word); 26 | else if (buf.length() + word.length() + 1 <= k) 27 | buf.append(' ').append(word); 28 | else { 29 | res.add(buf.toString()); 30 | buf = new StringBuilder(word); 31 | } 32 | } 33 | 34 | if (buf != null) 35 | res.add(buf.toString()); 36 | 37 | return res; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /#000 - #099/#058 - Amazon/#058.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#058 - Amazon/#058.pdf -------------------------------------------------------------------------------- /#000 - #099/#058 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 05.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findIndexByValue(new int[] { 13, 18, 25, 2, 8, 10 }, 8)); // 4 9 | System.out.println(findIndexByValue(new int[] { 7, 8, 9, 1, 2, 3, 4, 5, 6 }, 8)); // 1 10 | } 11 | 12 | public static Integer findIndexByValue(int[] arr, int val) { 13 | return find(arr, val, 0, arr.length - 1); 14 | } 15 | 16 | private static Integer find(int[] arr, int val, int left, int right) { 17 | if (left == right) 18 | return arr[left] == val ? left : null; 19 | 20 | int mid = (left + right) / 2; 21 | 22 | if (arr[left] <= arr[mid] && arr[left] <= val && val <= arr[mid] 23 | || arr[mid + 1] <= arr[right] && (arr[mid + 1] > val || val > arr[right])) 24 | return find(arr, val, left, mid); 25 | return find(arr, val, mid + 1, right); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /#000 - #099/#059 - Google/#059.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#059 - Google/#059.pdf -------------------------------------------------------------------------------- /#000 - #099/#060 - Facebook/#060.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#060 - Facebook/#060.pdf -------------------------------------------------------------------------------- /#000 - #099/#061 - Google/#061.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#061 - Google/#061.pdf -------------------------------------------------------------------------------- /#000 - #099/#062 - Facebook/#062.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#062 - Facebook/#062.pdf -------------------------------------------------------------------------------- /#000 - #099/#062 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 04.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(countTotalWays(2, 2)); // 2 11 | System.out.println(countTotalWays(3, 4)); // 10 12 | System.out.println(countTotalWays(4, 4)); // 20 13 | } 14 | 15 | public static int countTotalWays(int N, int M) { 16 | int[] arr = new int[M]; 17 | Arrays.fill(arr, 1); 18 | 19 | for (int i = 1; i < N; i++) 20 | for (int j = 0; j < M; j++) 21 | arr[j] += j == 0 ? 0 : arr[j - 1]; 22 | 23 | return arr[M - 1]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /#000 - #099/#063 - Microsoft/#063.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#063 - Microsoft/#063.pdf -------------------------------------------------------------------------------- /#000 - #099/#063 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 04.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | char[][] matrix = { 9 | {'F', 'A', 'C', 'I'}, 10 | {'O', 'B', 'Q', 'P'}, 11 | {'A', 'N', 'O', 'B'}, 12 | {'M', 'A', 'S', 'S'}}; 13 | System.out.println(isContainsWord(matrix, "FOAM")); // true 14 | System.out.println(isContainsWord(matrix, "MASS")); // true 15 | System.out.println(isContainsWord(matrix, "MASSS")); // false 16 | } 17 | 18 | public static boolean isContainsWord(char[][] matrix, String word) { 19 | for (int row = 0; row < matrix.length; row++) 20 | for (int col = 0; col < matrix[row].length; col++) 21 | if (checkRow(matrix, row, col, word) || checkColumn(matrix, row, col, word)) 22 | return true; 23 | return false; 24 | } 25 | 26 | private static boolean checkColumn(char[][] matrix, int row, int col, String word) { 27 | if (col + word.length() > matrix[row].length) 28 | return false; 29 | 30 | for (int i = 0; i < matrix[row].length; i++) 31 | if (matrix[row][col + i] != word.charAt(i)) 32 | return false; 33 | 34 | return true; 35 | } 36 | 37 | private static boolean checkRow(char[][] matrix, int row, int col, String word) { 38 | if (row + word.length() > matrix.length) 39 | return false; 40 | 41 | for (int i = 0; i < matrix.length; i++) 42 | if (matrix[row + i][col] != word.charAt(i)) 43 | return false; 44 | 45 | return true; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /#000 - #099/#064 - Google/#064.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#064 - Google/#064.pdf -------------------------------------------------------------------------------- /#000 - #099/#065 - Amazon/#065.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#065 - Amazon/#065.pdf -------------------------------------------------------------------------------- /#000 - #099/#066 - Square/#066.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#066 - Square/#066.pdf -------------------------------------------------------------------------------- /#000 - #099/#067 - Google/#067.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#067 - Google/#067.pdf -------------------------------------------------------------------------------- /#000 - #099/#067 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedHashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 06.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | LRUCache cache = new LRUCache<>(3); 12 | cache.set("one", "one1"); 13 | cache.set("two", "two1"); 14 | cache.set("three", "three1"); 15 | cache.set("four", "four1"); 16 | System.out.println(cache.get("two")); // two1 17 | System.out.println(cache.get("one")); // null 18 | 19 | cache.set("five", "five1"); 20 | System.out.println(cache.get("two")); // two1 21 | System.out.println(cache.get("three")); // null 22 | 23 | cache.set("four", "four2"); 24 | System.out.println(cache.get("four")); // four2 25 | } 26 | 27 | public static final class LRUCache { 28 | private final Map map = new LinkedHashMap<>(); 29 | private final int maxSize; 30 | 31 | public LRUCache(int maxSize) { 32 | this.maxSize = maxSize; 33 | } 34 | 35 | public void set(K key, V value) { 36 | if (map.containsKey(key)) 37 | map.remove(key); 38 | else if (map.size() == maxSize) 39 | map.remove(map.keySet().iterator().next()); 40 | 41 | map.put(key, value); 42 | } 43 | 44 | public V get(K key) { 45 | if (!map.containsKey(key)) 46 | return null; 47 | 48 | V value = map.remove(key); 49 | map.put(key, value); 50 | return value; 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /#000 - #099/#068 - Google/#068.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#068 - Google/#068.pdf -------------------------------------------------------------------------------- /#000 - #099/#069 - Facebook/#069.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#069 - Facebook/#069.pdf -------------------------------------------------------------------------------- /#000 - #099/#069 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 07.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(maxTriplet(new int[] { -10, -10, 5, 2 })); // 500 11 | } 12 | 13 | public static int maxTriplet(int[] arr) { 14 | Arrays.sort(arr); 15 | int res = arr[arr.length - 1]; 16 | return Math.max(res, arr[arr.length - 3] * arr[arr.length - 2] * arr[arr.length - 1]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /#000 - #099/#070 - Microsoft/#070.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#070 - Microsoft/#070.pdf -------------------------------------------------------------------------------- /#000 - #099/#071 - Two Sigma/#071.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#071 - Two Sigma/#071.pdf -------------------------------------------------------------------------------- /#000 - #099/#072 - Google/#072.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#072 - Google/#072.pdf -------------------------------------------------------------------------------- /#000 - #099/#073 - Google/#073.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#073 - Google/#073.pdf -------------------------------------------------------------------------------- /#000 - #099/#073 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 30.10.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node root = createList(1, 2, 3, 4, 5); 9 | print(root); 10 | Node reverseRoot = reverseInPlace(root); 11 | print(reverseRoot); 12 | } 13 | 14 | public static Node reverseInPlace(Node node) { 15 | Node prv = null; 16 | 17 | while (node != null) { 18 | Node next = node.next; 19 | 20 | if (prv == null) { 21 | prv = node; 22 | prv.next = null; 23 | } else { 24 | node.next = prv; 25 | prv = node; 26 | } 27 | 28 | node = next; 29 | } 30 | 31 | return prv; 32 | } 33 | 34 | private static void print(Node node) { 35 | while (node != null) { 36 | System.out.print(node.val + ", "); 37 | node = node.next; 38 | } 39 | 40 | System.out.println(); 41 | } 42 | 43 | private static Node createList(int... vals) { 44 | Node root = null; 45 | Node node = null; 46 | 47 | for (int val : vals) { 48 | Node tmp = new Node(val); 49 | 50 | if (node == null) { 51 | node = tmp; 52 | root = node; 53 | } else { 54 | node.next = tmp; 55 | node = tmp; 56 | } 57 | } 58 | 59 | return root; 60 | } 61 | 62 | private static class Node { 63 | 64 | private final int val; 65 | private Node next; 66 | 67 | public Node(int val) { 68 | this.val = val; 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /#000 - #099/#074 - Apple/#074.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#074 - Apple/#074.pdf -------------------------------------------------------------------------------- /#000 - #099/#074 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 24.09.2022 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getTotalNumber(6, 12)); // 4 9 | } 10 | 11 | private static int getTotalNumber(int N, int X) { 12 | int res = 0; 13 | 14 | for (int i = 1; i <= N; i++) 15 | if (X % i == 0 && X / i <= N) 16 | res++; 17 | 18 | return res; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /#000 - #099/#075 - Microsoft/#075.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#075 - Microsoft/#075.pdf -------------------------------------------------------------------------------- /#000 - #099/#075 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 07.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findLongestIncreasingSubsequence(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15})); // 6 9 | } 10 | 11 | public static int findLongestIncreasingSubsequence(int[] arr) { 12 | int[] cache = new int[arr.length]; 13 | int res = 0; 14 | 15 | for (int i = 0, max = 0; i < arr.length; i++, max = 0) { 16 | for (int j = 0; j < i; j++) 17 | if (arr[j] < arr[i]) 18 | max = Math.max(max, cache[j]); 19 | 20 | res = Math.max(res, cache[i] = max + 1); 21 | } 22 | 23 | return res; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /#000 - #099/#076 - Google/#076.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#076 - Google/#076.pdf -------------------------------------------------------------------------------- /#000 - #099/#077 - Snapchat/#077.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#077 - Snapchat/#077.pdf -------------------------------------------------------------------------------- /#000 - #099/#077 - Snapchat/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | import java.util.Map; 4 | import java.util.TreeMap; 5 | 6 | /** 7 | * @author Oleg Cherednik 8 | * @since 07.02.2019 9 | */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | List res = mergeIntervals(new Interval[]{new Interval(1, 3), new Interval(5, 8), new Interval(4, 10), new Interval(20, 25)}); 14 | res.forEach(interval -> System.out.format("[%d:%d]\n", interval.from, interval.to)); 15 | } 16 | 17 | public static List mergeIntervals(Interval[] intervals) { 18 | Map map = new TreeMap<>(); 19 | 20 | for (Interval interval : intervals) { 21 | map.put(interval.from, map.getOrDefault(interval.from, 0) + 1); 22 | map.put(interval.to, map.getOrDefault(interval.to, 0) - 1); 23 | } 24 | 25 | List res = new ArrayList<>(); 26 | int from = -1; 27 | int count = 0; 28 | 29 | for (Map.Entry entry : map.entrySet()) { 30 | count += entry.getValue(); 31 | 32 | if (entry.getValue() > 0) 33 | from = from == -1 ? entry.getKey() : from; 34 | else if (count == 0) { 35 | res.add(new Interval(from, entry.getKey())); 36 | from = -1; 37 | } 38 | } 39 | 40 | return res; 41 | } 42 | 43 | private static final class Interval { 44 | private final int from; 45 | private final int to; 46 | 47 | public Interval(int from, int to) { 48 | this.from = from; 49 | this.to = to; 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /#000 - #099/#078 - Google/#078.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#078 - Google/#078.pdf -------------------------------------------------------------------------------- /#000 - #099/#078 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Iterator; 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * @author Oleg Cherednik 9 | * @since 07.02.2019 10 | */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | List> sortedLists = Arrays.asList( 15 | Arrays.asList(0, 1, 4, 5, 6, 8, 8), 16 | Arrays.asList(0, 1, 3, 3, 3, 3, 5, 6, 7), 17 | Arrays.asList(0, 0, 1, 3, 4, 5, 6, 6, 6, 7, 8, 8, 8) 18 | ); 19 | 20 | System.out.println(merge(sortedLists).stream() 21 | .map(val -> Integer.toString(val)) 22 | .collect(Collectors.joining(" "))); 23 | } 24 | 25 | public static List merge(List> sortedLists) { 26 | List res = new LinkedList<>(); 27 | Iterator[] its = sortedLists.stream().map(List::iterator).toArray(Iterator[]::new); 28 | Integer[] values = new Integer[sortedLists.size()]; 29 | 30 | while (true) { 31 | int minIndex = -1; 32 | 33 | for (int i = 0; i < values.length; i++) { 34 | values[i] = values[i] == null && its[i].hasNext() ? its[i].next() : values[i]; 35 | 36 | if (values[i] != null) 37 | minIndex = minIndex == -1 || values[minIndex] != null && values[minIndex] > values[i] ? i : minIndex; 38 | } 39 | 40 | if (minIndex == -1) 41 | break; 42 | 43 | res.add(values[minIndex]); 44 | values[minIndex] = null; 45 | } 46 | 47 | return res; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /#000 - #099/#079 - Facebook/#079.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#079 - Facebook/#079.pdf -------------------------------------------------------------------------------- /#000 - #099/#079 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 07.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(check(new int[]{10, 5, 7})); // true 12 | System.out.println(check(new int[]{10, 5, 1})); // false 13 | } 14 | 15 | public static boolean check(int[] arr) { 16 | int replace = 0; 17 | Deque stack = new LinkedList<>(); 18 | 19 | for (int val : arr) { 20 | while (!stack.isEmpty() && val < stack.element()) { 21 | stack.pop(); 22 | replace++; 23 | } 24 | 25 | if (replace > 1) 26 | return false; 27 | 28 | stack.push(val); 29 | } 30 | 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /#000 - #099/#080 - Google/#080.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#080 - Google/#080.pdf -------------------------------------------------------------------------------- /#000 - #099/#080 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 08.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(findDeepestNode(createTree())); // d 11 | } 12 | 13 | private static Node createTree() { 14 | Node d = new Node('d'); 15 | Node b = new Node('b'); 16 | b.left = d; 17 | 18 | Node c = new Node('c'); 19 | Node root = new Node('a'); 20 | root.left = b; 21 | root.right = c; 22 | 23 | return root; 24 | } 25 | 26 | public static char findDeepestNode(Node root) { 27 | if (root == null) 28 | return '\0'; 29 | 30 | Deque> queue = new LinkedList<>(); 31 | queue.add(Collections.singletonList(root)); 32 | List deepestNodes = Collections.emptyList(); 33 | 34 | while (!queue.isEmpty()) { 35 | deepestNodes = queue.poll(); 36 | List nodes = new ArrayList<>(); 37 | 38 | for (Node node : deepestNodes) { 39 | if (node.left != null) 40 | nodes.add(node.left); 41 | if (node.right != null) 42 | nodes.add(node.right); 43 | } 44 | 45 | if (!nodes.isEmpty()) 46 | queue.add(nodes); 47 | } 48 | 49 | return deepestNodes.isEmpty() ? '\0' : deepestNodes.iterator().next().data; 50 | } 51 | 52 | private static final class Node { 53 | private final char data; 54 | private Node left; 55 | private Node right; 56 | 57 | public Node(char data) { 58 | this.data = data; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /#000 - #099/#081 - Yelp/#081.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#081 - Yelp/#081.pdf -------------------------------------------------------------------------------- /#000 - #099/#082 - Microsoft/#082.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#082 - Microsoft/#082.pdf -------------------------------------------------------------------------------- /#000 - #099/#083 - Google/#083.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#083 - Google/#083.pdf -------------------------------------------------------------------------------- /#000 - #099/#083 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 09.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node root = invert(createTree()); 9 | } 10 | 11 | private static Node createTree() { 12 | Node a = new Node('a'); 13 | Node b = new Node('b'); 14 | Node c = new Node('c'); 15 | Node d = new Node('d'); 16 | Node e = new Node('e'); 17 | Node f = new Node('f'); 18 | 19 | a.left = b; 20 | a.right = c; 21 | 22 | b.left = d; 23 | b.right = e; 24 | 25 | c.left = f; 26 | 27 | return a; 28 | } 29 | 30 | public static Node invert(Node node) { 31 | if (node == null) 32 | return null; 33 | 34 | Node left = node.left; 35 | Node right = node.right; 36 | node.right = invert(left); 37 | node.left = invert(right); 38 | 39 | return node; 40 | } 41 | 42 | private static final class Node { 43 | private final char data; 44 | private Node left; 45 | private Node right; 46 | 47 | public Node(char data) { 48 | this.data = data; 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /#000 - #099/#084 - Amazon/#084.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#084 - Amazon/#084.pdf -------------------------------------------------------------------------------- /#000 - #099/#085 - Facebook/#085.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#085 - Facebook/#085.pdf -------------------------------------------------------------------------------- /#000 - #099/#085 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 09.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(ifElse(4, 7, 1)); // 4 9 | System.out.println(ifElse(4, 7, 0)); // 7 10 | } 11 | 12 | public static int ifElse(int x, int y, int b) { 13 | return (x * b) | (y * (1 - b)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /#000 - #099/#086 - Google/#086.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#086 - Google/#086.pdf -------------------------------------------------------------------------------- /#000 - #099/#086 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 09.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(minParenthesesToRemove("(()())((()))")); // 0 9 | System.out.println(minParenthesesToRemove("()())()")); // 1 10 | System.out.println(minParenthesesToRemove(")(")); // 2 11 | } 12 | 13 | public static int minParenthesesToRemove(String str) { 14 | int count = 0; 15 | int res = 0; 16 | 17 | for (int i = 0; i < str.length(); i++) { 18 | if (str.charAt(i) == '(') 19 | count++; 20 | else if (str.charAt(i) == ')') 21 | count--; 22 | else 23 | continue; 24 | 25 | if (count >= 0) 26 | continue; 27 | 28 | res++; 29 | count = 0; 30 | } 31 | 32 | return res + Math.abs(count); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /#000 - #099/#087 - Uber/#087.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#087 - Uber/#087.pdf -------------------------------------------------------------------------------- /#000 - #099/#088 - Context Logic/#088.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#088 - Context Logic/#088.pdf -------------------------------------------------------------------------------- /#000 - #099/#089 - LinkedIn/#089.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#089 - LinkedIn/#089.pdf -------------------------------------------------------------------------------- /#000 - #099/#090 - Google/#090.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#090 - Google/#090.pdf -------------------------------------------------------------------------------- /#000 - #099/#091 - Dropbox/#091.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#091 - Dropbox/#091.pdf -------------------------------------------------------------------------------- /#000 - #099/#092 - Airbnb/#092.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#092 - Airbnb/#092.pdf -------------------------------------------------------------------------------- /#000 - #099/#092 - Airbnb/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.Collections; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | /** 11 | * @author Oleg Cherednik 12 | * @since 10.02.2019 13 | */ 14 | public class Solution { 15 | 16 | public static void main(String... args) { 17 | System.out.println(String.join(" ", sort(create()))); 18 | } 19 | 20 | private static Map> create() { 21 | Map> map = new HashMap<>(); 22 | map.put("CSC300", new HashSet<>(Arrays.asList("CSC100", "CSC200"))); 23 | map.put("CSC200", new HashSet<>(Collections.singletonList("CSC100"))); 24 | map.put("CSC100", Collections.emptySet()); 25 | return map; 26 | } 27 | 28 | public static List sort(Map> map) { 29 | Set visited = new HashSet<>(); 30 | List res = new ArrayList<>(); 31 | 32 | for (String courseId : map.keySet()) 33 | dfs(courseId, map, visited, res); 34 | 35 | return res; 36 | } 37 | 38 | private static void dfs(String courseId, Map> map, Set visited, List res) { 39 | if (visited.contains(courseId)) 40 | return; 41 | 42 | visited.add(courseId); 43 | 44 | for (String id : map.getOrDefault(courseId, Collections.emptySet())) 45 | dfs(id, map, visited, res); 46 | 47 | res.add(courseId); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /#000 - #099/#093 - Apple/#093.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#093 - Apple/#093.pdf -------------------------------------------------------------------------------- /#000 - #099/#093 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 09.03.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node root = generate(); 9 | } 10 | 11 | public static int getLargestBstSize(Node node) { 12 | // TODO 13 | } 14 | 15 | private static boolean isBst(Node node) { 16 | if(node == null) 17 | return true; 18 | 19 | boolean bst = true; 20 | 21 | if(node.left != null) { 22 | bst &= isBst(node.left); 23 | } 24 | 25 | 26 | } 27 | 28 | private static Node generate() { 29 | return null; 30 | } 31 | 32 | public static final class Node { 33 | private final int val; 34 | private Node left; 35 | private Node right; 36 | private int size = -1; 37 | 38 | public Node(int val) { 39 | this.val = val; 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /#000 - #099/#094 - Google/#094.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#094 - Google/#094.pdf -------------------------------------------------------------------------------- /#000 - #099/#095 - Palantir/#095.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#095 - Palantir/#095.pdf -------------------------------------------------------------------------------- /#000 - #099/#096 - Microsoft/#096.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#096 - Microsoft/#096.pdf -------------------------------------------------------------------------------- /#000 - #099/#097 - Stripe/#097.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#097 - Stripe/#097.pdf -------------------------------------------------------------------------------- /#000 - #099/#098 - Coursera/#098.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#098 - Coursera/#098.pdf -------------------------------------------------------------------------------- /#000 - #099/#099 - Microsoft/#099.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#000 - #099/#099 - Microsoft/#099.pdf -------------------------------------------------------------------------------- /#000 - #099/#099 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 11.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findLongestConsecutiveElementsLength(100, 4, 200, 1, 3, 2)); // 4 9 | } 10 | 11 | public static int findLongestConsecutiveElementsLength(int... arr) { 12 | int min = Integer.MAX_VALUE; 13 | int max = Integer.MIN_VALUE; 14 | 15 | for (int num : arr) { 16 | min = Math.min(min, num); 17 | max = Math.max(max, num); 18 | } 19 | 20 | boolean[] existed = new boolean[max - min + 1]; 21 | 22 | for (int num : arr) 23 | existed[num - min] = true; 24 | 25 | int res = 0; 26 | 27 | for (int i = 0, cur = 0; i < existed.length; i++) { 28 | if (existed[i]) 29 | res = Math.max(res, ++cur); 30 | else 31 | cur = 0; 32 | } 33 | 34 | return res; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#100 - #199/#100 - Google/#100.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#100 - Google/#100.pdf -------------------------------------------------------------------------------- /#100 - #199/#101 - Alibaba/#101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#101 - Alibaba/#101.pdf -------------------------------------------------------------------------------- /#100 - #199/#101 - Alibaba/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 18.01.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getMinSum(4)); 9 | System.out.println(getMinSum(50)); 10 | } 11 | 12 | public static Pair getMinSum(int num) { 13 | for (int a = num / 2; a >= 2; a--) 14 | if (isPrime(a) && isPrime(num - a)) 15 | return new Pair(a, num - a); 16 | 17 | return null; 18 | } 19 | 20 | private static boolean isPrime(int num) { 21 | if (num < 2) 22 | return false; 23 | 24 | for (int i = 2, sqrt = (int)Math.sqrt(num); i <= sqrt; i++) 25 | if (num % i == 0) 26 | return false; 27 | 28 | return true; 29 | } 30 | 31 | private static final class Pair { 32 | private final int a; 33 | private final int b; 34 | 35 | private Pair(int a, int b) { 36 | this.a = a; 37 | this.b = b; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "[" + a + ',' + b + ']'; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /#100 - #199/#102 - Lyft/#102.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#102 - Lyft/#102.pdf -------------------------------------------------------------------------------- /#100 - #199/#102 - Lyft/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 18.01.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(getSum(new int[]{1, 2, 3, 4, 5}, 9))); 11 | } 12 | 13 | public static int[] getSum(int[] arr, int k) { 14 | int i = 0; 15 | int j = 0; 16 | int sum = 0; 17 | 18 | while (j <= arr.length && sum != k) { 19 | if (sum < k) 20 | sum += arr[j++]; 21 | else 22 | sum -= arr[i++]; 23 | } 24 | 25 | int[] res = new int[j - i]; 26 | System.arraycopy(arr, i, res, 0, res.length); 27 | 28 | return res; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /#100 - #199/#103 - Square/#103.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#103 - Square/#103.pdf -------------------------------------------------------------------------------- /#100 - #199/#103 - Square/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 19.01.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getShortestSubstring("figehaeci", "aei")); 9 | } 10 | 11 | private static String getShortestSubstring(String str, String test) { 12 | String res = null; 13 | 14 | for (int i = 0, j = i; i < str.length() - test.length(); i++, j = i) { 15 | if (test.indexOf(str.charAt(i)) == -1) 16 | continue; 17 | 18 | for (int k = 0, pos; k < test.length(); k++) 19 | if (test.charAt(k) != str.charAt(i) && (pos = str.indexOf(test.charAt(k), i)) != -1) 20 | j = Math.max(j, pos); 21 | 22 | if (res == null || res.length() > j + 1 - i) 23 | res = str.substring(i, j + 1); 24 | } 25 | 26 | return res; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /#100 - #199/#104 - Google/#104.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#104 - Google/#104.pdf -------------------------------------------------------------------------------- /#100 - #199/#105 - Facebook/#105.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#105 - Facebook/#105.pdf -------------------------------------------------------------------------------- /#100 - #199/#106 - Pinterest/#106.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#106 - Pinterest/#106.pdf -------------------------------------------------------------------------------- /#100 - #199/#106 - Pinterest/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 11.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(check1(2, 0, 1, 0)); // true 9 | System.out.println(check1(1, 1, 0, 1)); // false 10 | System.out.println(check1(2, 3, 1, 0, 0)); // true 11 | 12 | System.out.println(check2(2, 0, 1, 0)); // true 13 | System.out.println(check2(1, 1, 0, 1)); // false 14 | System.out.println(check2(2, 3, 1, 0, 0)); // true 15 | } 16 | 17 | public static boolean check1(int... arr) { 18 | boolean[] tmp = new boolean[arr.length]; 19 | tmp[tmp.length - 1] = true; 20 | 21 | for (int i = tmp.length - 2; i >= 0; i--) { 22 | if (arr[i] + i >= arr.length) 23 | tmp[i] = true; 24 | else { 25 | for (int j = i + 1; j < arr[i] + i + 1; j++) { 26 | if (tmp[j]) { 27 | tmp[i] = true; 28 | break; 29 | } 30 | } 31 | } 32 | } 33 | 34 | return tmp[0]; 35 | } 36 | 37 | public static boolean check2(int... arr) { 38 | for (int i = 0, max = 1; i < arr.length - 1; i++) 39 | if ((max = Math.max(max - 1, arr[i])) == 0) 40 | return false; 41 | 42 | return true; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /#100 - #199/#107 - Microsoft/#107.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#107 - Microsoft/#107.pdf -------------------------------------------------------------------------------- /#100 - #199/#108 - Google/#108.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#108 - Google/#108.pdf -------------------------------------------------------------------------------- /#100 - #199/#108 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 11.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(isShifted("abcde", "cdeab")); // true 9 | System.out.println(isShifted("abc", "acb")); // false 10 | } 11 | 12 | public static boolean isShifted(String one, String two) { 13 | if (one.length() != two.length()) 14 | return false; 15 | if (one.equals(two)) 16 | return true; 17 | 18 | for (int i = 0; i <= one.length(); i++) 19 | if ((one = shiftLeft(one)).equals(two)) 20 | return true; 21 | 22 | return false; 23 | } 24 | 25 | private static String shiftLeft(String str) { 26 | return str.substring(1) + str.charAt(0); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /#100 - #199/#109 - Cisco/#109.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#109 - Cisco/#109.pdf -------------------------------------------------------------------------------- /#100 - #199/#109 - Cisco/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 05.12.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(Integer.toBinaryString(swapEvenOddBIts(0b10101010))); 9 | System.out.println(Integer.toBinaryString(swapEvenOddBIts(0b11100010))); 10 | } 11 | 12 | private static int swapEvenOddBIts(int val) { 13 | return (val & 0b01010101) << 1 | (val & 0b10101010) >> 1; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /#100 - #199/#110 - Facebook/#110.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#110 - Facebook/#110.pdf -------------------------------------------------------------------------------- /#100 - #199/#111 - Google/#111.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#111 - Google/#111.pdf -------------------------------------------------------------------------------- /#100 - #199/#112 - Twitter/#112.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#112 - Twitter/#112.pdf -------------------------------------------------------------------------------- /#100 - #199/#113 - Google/#113.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#113 - Google/#113.pdf -------------------------------------------------------------------------------- /#100 - #199/#114 - Facebook/#114.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#114 - Facebook/#114.pdf -------------------------------------------------------------------------------- /#100 - #199/#115 - Google/#115.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#115 - Google/#115.pdf -------------------------------------------------------------------------------- /#100 - #199/#115 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 12.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node t = new Node(3); 9 | Node root = createTree(t); 10 | System.out.println(isSubtreeEquals(root, t)); // true 11 | } 12 | 13 | private static Node createTree(Node t) { 14 | Node four = new Node(4); 15 | four.left = new Node(7); 16 | 17 | Node two = new Node(2); 18 | two.right = four; 19 | 20 | t.left = new Node(5); 21 | t.right = new Node(6); 22 | 23 | Node root = new Node(1); 24 | root.left = two; 25 | root.right = t; 26 | 27 | return root; 28 | } 29 | 30 | public static boolean isSubtreeEquals(Node s, Node t) { 31 | String strS = serialize(s); 32 | String strT = serialize(t); 33 | return strS.contains(strT); 34 | } 35 | 36 | private static String serialize(Node root) { 37 | return dfs(root, new StringBuilder()).toString(); 38 | } 39 | 40 | private static StringBuilder dfs(Node node, StringBuilder buf) { 41 | if (node != null) { 42 | buf.append(node.data); 43 | 44 | buf.append('('); 45 | dfs(node.left, buf); 46 | buf.append(')'); 47 | buf.append('('); 48 | dfs(node.right, buf); 49 | buf.append(')'); 50 | } 51 | 52 | return buf; 53 | } 54 | 55 | private static final class Node { 56 | private final int data; 57 | private Node left; 58 | private Node right; 59 | 60 | public Node(int data) { 61 | this.data = data; 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /#100 - #199/#116 - Jane Street/#116.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#116 - Jane Street/#116.pdf -------------------------------------------------------------------------------- /#100 - #199/#116 - Jane Street/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.atomic.AtomicInteger; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 09.03.2020 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | Node root = generate(); 11 | System.out.println(root.val()); 12 | System.out.println(root.left().val()); 13 | System.out.println(root.right().val()); 14 | } 15 | 16 | public static Node generate() { 17 | return new Node(); 18 | } 19 | 20 | public static final class Node { 21 | 22 | private static final AtomicInteger COUNT = new AtomicInteger(); 23 | 24 | private final int val = COUNT.incrementAndGet(); 25 | private Node left; 26 | private Node right; 27 | 28 | public int val() { 29 | return val; 30 | } 31 | 32 | public Node left() { 33 | if (left == null) 34 | left = new Node(); 35 | return left; 36 | } 37 | 38 | public Node right() { 39 | if (right == null) 40 | right = new Node(); 41 | return right; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /#100 - #199/#117 - Facebook/#117.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#117 - Facebook/#117.pdf -------------------------------------------------------------------------------- /#100 - #199/#118 - Google/#118.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#118 - Google/#118.pdf -------------------------------------------------------------------------------- /#100 - #199/#118 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 12.12.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(squareAndSort(-9, -2, 0, 2, 3))); 11 | } 12 | 13 | public static int[] squareAndSort(int... sorted) { 14 | int[] res = new int[sorted.length]; 15 | 16 | for (int i = 0, j = sorted.length - 1, k = res.length - 1; i <= j; k--) { 17 | int absLeft = Math.abs(sorted[i]); 18 | int absRight = Math.abs(sorted[j]); 19 | 20 | if (absLeft >= absRight) { 21 | res[k] = absLeft * absLeft; 22 | i++; 23 | } else { 24 | res[k] = absRight * absRight; 25 | j--; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#100 - #199/#119 - Google/#119.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#119 - Google/#119.pdf -------------------------------------------------------------------------------- /#100 - #199/#119 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Comparator; 3 | import java.util.List; 4 | import java.util.Set; 5 | import java.util.TreeSet; 6 | import java.util.stream.Collectors; 7 | 8 | /** 9 | * @author Oleg Cherednik 10 | * @since 27.02.2019 11 | */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | Set nums = getCoversIntervals(Arrays.asList( 16 | new Interval(0, 3), 17 | new Interval(2, 6), 18 | new Interval(3, 4), 19 | new Interval(6, 9))); 20 | System.out.println(nums.stream().map(String::valueOf).collect(Collectors.joining(" "))); // 3 9 21 | } 22 | 23 | public static Set getCoversIntervals(List intervals) { 24 | final Comparator sortByStartAndLengthDesc = (one, two) -> 25 | one.start != two.start ? Integer.compare(one.start, two.start) : Integer.compare(two.start, one.start); 26 | 27 | Set uniqueIntervals = new TreeSet<>(sortByStartAndLengthDesc); 28 | uniqueIntervals.addAll(intervals); 29 | 30 | int end = -1; 31 | Set res = new TreeSet<>(); 32 | 33 | for (Interval interval : intervals) 34 | if (end == -1 || interval.start > end) 35 | res.add(end = interval.end); 36 | 37 | return res; 38 | } 39 | 40 | public static final class Interval { 41 | private final int start; 42 | private final int end; 43 | 44 | public Interval(int start, int end) { 45 | this.start = start; 46 | this.end = end; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#120 - Microsoft/#120.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#120 - Microsoft/#120.pdf -------------------------------------------------------------------------------- /#100 - #199/#120 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 12.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(TwistSingleton.getInstance().id); // 1 9 | System.out.println(TwistSingleton.getInstance().id); // 2 10 | System.out.println(TwistSingleton.getInstance().id); // 1 11 | System.out.println(TwistSingleton.getInstance().id); // 2 12 | } 13 | 14 | public static final class TwistSingleton { 15 | private static final TwistSingleton ODD = new TwistSingleton(1); 16 | private static final TwistSingleton EVEN = new TwistSingleton(2); 17 | private static boolean odd = true; 18 | 19 | public static TwistSingleton getInstance() { 20 | if (odd) { 21 | odd = false; 22 | return ODD; 23 | } 24 | 25 | odd = true; 26 | return EVEN; 27 | } 28 | 29 | private final int id; 30 | 31 | private TwistSingleton(int id) { 32 | this.id = id; 33 | } 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#100 - #199/#121 - Google/#121.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#121 - Google/#121.pdf -------------------------------------------------------------------------------- /#100 - #199/#122 - Zillow/#122.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#122 - Zillow/#122.pdf -------------------------------------------------------------------------------- /#100 - #199/#122 - Zillow/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.function.BiFunction; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 18.12.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[][] matrix = 11 | { 12 | { 0, 3, 1, 1 }, 13 | { 2, 0, 0, 4 }, 14 | { 1, 5, 3, 1 } 15 | }; 16 | System.out.println(maxCoins(matrix)); 17 | } 18 | 19 | public static int maxCoins(int[][] matrix) { 20 | if (matrix == null) 21 | return 0; 22 | 23 | BiFunction get = 24 | (row, col) -> row < 0 || row >= matrix.length || col < 0 || col >= matrix[row].length ? 0 : matrix[row][col]; 25 | int width = matrix[0].length; 26 | 27 | for (int row = 0; row < matrix.length; row++) 28 | for (int col = 0; col < width; col++) 29 | matrix[row][col] += Math.max(get.apply(row - 1, col), get.apply(row, col - 1)); 30 | 31 | return matrix[matrix.length - 1][width - 1]; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /#100 - #199/#123 - LinkedIn/#123.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#123 - LinkedIn/#123.pdf -------------------------------------------------------------------------------- /#100 - #199/#123 - LinkedIn/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 22.12.2018 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(isNumber("10")); 9 | System.out.println(isNumber("-10")); 10 | System.out.println(isNumber("10.1")); 11 | System.out.println(isNumber("-10.1")); 12 | System.out.println(isNumber("1e5")); 13 | System.out.println(); 14 | System.out.println(isNumber("a")); 15 | System.out.println(isNumber("x 1")); 16 | System.out.println(isNumber("a-2")); 17 | System.out.println(isNumber("-")); 18 | } 19 | 20 | public static boolean isNumber(String str) { 21 | try { 22 | Double.parseDouble(str); 23 | return true; 24 | } catch(Exception e) { 25 | return false; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /#100 - #199/#124 - Microsoft/#124.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#124 - Microsoft/#124.pdf -------------------------------------------------------------------------------- /#100 - #199/#125 - Google/#125.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#125 - Google/#125.pdf -------------------------------------------------------------------------------- /#100 - #199/#126 - Facebook/#126.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#126 - Facebook/#126.pdf -------------------------------------------------------------------------------- /#100 - #199/#126 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 22.12.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[] arr = { 1, 2, 3, 4, 5, 6 }; 11 | System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4, 5, 6] 12 | rotate(arr, 2); 13 | System.out.println(Arrays.toString(arr)); // [3, 4, 5, 6, 1, 2] 14 | } 15 | 16 | public static void rotate(int[] arr, int k) { 17 | if ((k %= arr.length) != 0) { 18 | k = k < 0 ? arr.length + k : k; 19 | swapSubArr(arr, 0, arr.length); 20 | swapSubArr(arr, 0, arr.length - k); 21 | swapSubArr(arr, arr.length - k, arr.length); 22 | } 23 | } 24 | 25 | private static void swapSubArr(int[] arr, int from, int to) { 26 | for (int i = from, j = to - 1; i < j; i++, j--) 27 | swap(arr, i, j); 28 | } 29 | 30 | private static void swap(int[] arr, int i, int j) { 31 | int tmp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = tmp; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /#100 - #199/#127 - Microsoft/#127.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#127 - Microsoft/#127.pdf -------------------------------------------------------------------------------- /#100 - #199/#127 - Microsoft/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 12.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(convertToNumber(convertToLinkedList(54321))); // 54321 9 | System.out.println(convertToNumber(sum(convertToLinkedList(99), convertToLinkedList(25)))); // 124 10 | } 11 | 12 | public static Node sum(Node one, Node two) { 13 | int sum = convertToNumber(one) + convertToNumber(two); 14 | return convertToLinkedList(sum); 15 | } 16 | 17 | private static int convertToNumber(Node node) { 18 | StringBuilder buf = new StringBuilder(); 19 | 20 | while (node != null) { 21 | buf.append(node.digit); 22 | node = node.next; 23 | } 24 | 25 | return Integer.parseInt(buf.reverse().toString()); 26 | } 27 | 28 | private static Node convertToLinkedList(int num) { 29 | String str = String.valueOf(num); 30 | Node root = null; 31 | Node prv = null; 32 | 33 | for (int i = str.length() - 1; i >= 0; i--) { 34 | Node node = new Node(str.charAt(i)); 35 | 36 | if (prv != null) 37 | prv.next = node; 38 | prv = node; 39 | 40 | root = root == null ? node : root; 41 | } 42 | 43 | return root; 44 | } 45 | 46 | private static final class Node { 47 | private final char digit; 48 | private Node next; 49 | 50 | public Node(char digit) { 51 | assert Character.isDigit(digit); 52 | this.digit = digit; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /#100 - #199/#128/#128.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#128/#128.pdf -------------------------------------------------------------------------------- /#100 - #199/#129/#129.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#129/#129.pdf -------------------------------------------------------------------------------- /#100 - #199/#129/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 13.02.2019 4 | */ 5 | public class Solution { 6 | public static void main(String[] args) { 7 | System.out.println(floorSqrt(81)); // 9 8 | System.out.println(floorSqrt(9)); // 3 9 | System.out.println(floorSqrt(200)); // 14 10 | } 11 | 12 | public static int floorSqrt(int n) { 13 | if (n == 0 || n == 1) 14 | return n; 15 | 16 | int lo = 0; 17 | int hi = n; 18 | int sqrt = 0; 19 | 20 | while (lo <= hi) { 21 | int mid = (lo + hi) / 2; 22 | 23 | if (mid * mid == n) 24 | return mid; 25 | 26 | if (mid * mid < n) { 27 | lo = mid + 1; 28 | sqrt = mid; 29 | } else 30 | hi = mid - 1; 31 | } 32 | 33 | return sqrt; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /#100 - #199/#130 - Facebook/#130.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#130 - Facebook/#130.pdf -------------------------------------------------------------------------------- /#100 - #199/#130 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 28.12.2018 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[] arr = { 1, 2, 3, 4, 5, 6 }; 11 | int[] tmp = Arrays.copyOf(arr, arr.length); 12 | System.out.println(Arrays.toString(arr)); 13 | rotate(arr, 2); 14 | System.out.println(Arrays.toString(arr)); 15 | } 16 | 17 | public static void rotate(int[] arr, int k) { 18 | if ((k %= arr.length) != 0) { 19 | k = k < 0 ? arr.length + k : k; 20 | swapSubArr(arr, 0, arr.length); 21 | swapSubArr(arr, 0, arr.length - k); 22 | swapSubArr(arr, arr.length - k, arr.length); 23 | } 24 | } 25 | 26 | private static void swapSubArr(int[] arr, int from, int to) { 27 | for (int i = from, j = to - 1; i < j; i++, j--) 28 | swap(arr, i, j); 29 | } 30 | 31 | private static void swap(int[] arr, int i, int j) { 32 | int tmp = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = tmp; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /#100 - #199/#131 - Snapchat/#131.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#131 - Snapchat/#131.pdf -------------------------------------------------------------------------------- /#100 - #199/#132 - Riot Games/#132.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#132 - Riot Games/#132.pdf -------------------------------------------------------------------------------- /#100 - #199/#132 - Riot Games/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 28.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | } 12 | 13 | public static class HitCounter { 14 | private final Map map = new HashMap<>(); 15 | private int total; 16 | 17 | public void record(long timestamp) { 18 | map.put(timestamp, map.getOrDefault(timestamp, 0) + 1); 19 | total++; 20 | } 21 | 22 | public int total() { 23 | return total; 24 | } 25 | 26 | public int range(long lower, long upper) { 27 | int res = 0; 28 | 29 | for (Map.Entry entry : map.entrySet()) 30 | if (lower <= entry.getKey() && entry.getKey() <= upper) 31 | res += entry.getValue(); 32 | 33 | return res; 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#100 - #199/#133 - Amazon/#133.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#133 - Amazon/#133.pdf -------------------------------------------------------------------------------- /#100 - #199/#134 - Facebook/#134.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#134 - Facebook/#134.pdf -------------------------------------------------------------------------------- /#100 - #199/#134 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 28.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | } 12 | 13 | public static class SparseArray { 14 | private final Map map = new HashMap<>(); 15 | private int size; 16 | 17 | public void init(int[] arr, int size) { 18 | map.clear(); 19 | this.size = size; 20 | 21 | for (int i = 0; i < arr.length; i++) 22 | set(i, arr[i]); 23 | } 24 | 25 | public void set(int i, int val) { 26 | if (i < 0 || i >= size) 27 | throw new ArrayIndexOutOfBoundsException(); 28 | 29 | if (val != 0) 30 | map.put(i, val); 31 | } 32 | 33 | public int get(int i) { 34 | if (i < 0 || i >= size) 35 | throw new ArrayIndexOutOfBoundsException(); 36 | return map.getOrDefault(i, 0); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /#100 - #199/#135 - Apple/#135.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#135 - Apple/#135.pdf -------------------------------------------------------------------------------- /#100 - #199/#136 - Google/#136.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#136 - Google/#136.pdf -------------------------------------------------------------------------------- /#100 - #199/#136 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 14.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | int[][] matrix = { 9 | {1, 1, 0, 0}, 10 | {1, 0, 1, 1}, 11 | {1, 0, 1, 1}, 12 | {0, 1, 0, 0}}; 13 | System.out.println(getLargestAreaSet(matrix)); // 4 14 | } 15 | 16 | public static int getLargestAreaSet(int[][] matrix) { 17 | int maxArea = 0; 18 | int[] cache = new int[matrix[0].length]; 19 | 20 | for (int row = 0; row < matrix.length; row++) { 21 | for (int col = 0; col < matrix[row].length; col++) { 22 | if (matrix[row][col] == 0) 23 | cache[col] = 0; 24 | else 25 | cache[col]++; 26 | } 27 | 28 | maxArea = Math.max(maxArea, inferArea(cache)); 29 | } 30 | 31 | return maxArea; 32 | } 33 | 34 | private static int inferArea(int[] cache) { 35 | int area = 0; 36 | 37 | for (int i = 0; i < cache.length; i++) { 38 | if (cache[i] == 0) 39 | continue; 40 | 41 | int min = cache[i]; 42 | 43 | for (int j = i; j < cache.length && cache[j] != 0; j++) 44 | area = Math.max(area, Math.min(min, cache[j]) * (j - i + 1)); 45 | } 46 | 47 | return area; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#137 - Amazon/#137.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#137 - Amazon/#137.pdf -------------------------------------------------------------------------------- /#100 - #199/#137 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 14.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | BitArray bitArray = new BitArray(16); 9 | bitArray.set(5, true); 10 | bitArray.set(6, true); 11 | bitArray.set(7, true); 12 | bitArray.set(9, true); 13 | bitArray.set(13, true); 14 | bitArray.set(15, true); 15 | 16 | for (int i = 0; i < 16; i++) 17 | System.out.println(i + ":" + bitArray.get(i)); 18 | } 19 | 20 | public static final class BitArray { 21 | private static final int INDEX_LENGTH = 8; // size of int 22 | 23 | private final int[] arr; 24 | 25 | public BitArray(int size) { 26 | arr = new int[size / INDEX_LENGTH + (size % INDEX_LENGTH != 0 ? 1 : 0)]; 27 | } 28 | 29 | public void set(int i, boolean val) { 30 | if (val) 31 | arr[getArrayIndex(i)] |= getBitMask(i); 32 | else 33 | arr[getArrayIndex(i)] &= ~getBitMask(i); 34 | } 35 | 36 | public boolean get(int i) { 37 | return (arr[getArrayIndex(i)] & getBitMask(i)) != 0; 38 | } 39 | 40 | private static int getArrayIndex(int i) { 41 | return i / INDEX_LENGTH; 42 | } 43 | 44 | private static int getBitMask(int i) { 45 | return 1 << i % INDEX_LENGTH; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#138 - Google/#138.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#138 - Google/#138.pdf -------------------------------------------------------------------------------- /#100 - #199/#138 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 14.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(getMinCoinsAmount(16)); // 3 11 | } 12 | 13 | private static final int[] DENOMINATION = {1, 5, 10, 25}; 14 | 15 | public static int getMinCoinsAmount(int n) { 16 | int[] cache = new int[n + 1]; 17 | Arrays.fill(cache, Integer.MAX_VALUE); 18 | 19 | for (int d : DENOMINATION) 20 | if (d < cache.length) 21 | cache[d] = 1; 22 | 23 | for (int i = 1; i < cache.length; i++) 24 | for (int d : DENOMINATION) 25 | if (i - d > 0) 26 | cache[i] = Math.min(cache[i], 1 + cache[i - d]); 27 | 28 | return cache[n]; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /#100 - #199/#139 - Google/#139.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#139 - Google/#139.pdf -------------------------------------------------------------------------------- /#100 - #199/#139 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Iterator; 3 | import java.util.List; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 14.02.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | List words = Arrays.asList("one", "two", "three", "four", "five"); 13 | PeekableInterface it = new PeekableInterface<>(words); 14 | 15 | int i = 0; 16 | 17 | while (it.hasNext()) { 18 | if (i++ % 2 == 0) 19 | System.out.format("next:%s -> peek:%s\n", it.next(), it.hasNext() ? it.peek() : null); 20 | else 21 | System.out.format("peek:%s -> next:%s\n", it.peek(), it.next()); 22 | } 23 | } 24 | 25 | public static final class PeekableInterface implements Iterator { 26 | private final Iterator it; 27 | private E next; 28 | private boolean nextExists; 29 | 30 | public PeekableInterface(Iterable iterable) { 31 | it = iterable.iterator(); 32 | } 33 | 34 | @Override 35 | public boolean hasNext() { 36 | return nextExists || it.hasNext(); 37 | } 38 | 39 | @Override 40 | public E next() { 41 | if (nextExists) { 42 | E e = next; 43 | nextExists = false; 44 | next = null; 45 | return e; 46 | } 47 | 48 | return it.next(); 49 | } 50 | 51 | public E peek() { 52 | if (!nextExists) 53 | next = it.next(); 54 | 55 | nextExists = true; 56 | return next; 57 | } 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /#100 - #199/#140 - Facebook/#140.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#140 - Facebook/#140.pdf -------------------------------------------------------------------------------- /#100 - #199/#141 - Microsoft/#141.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#141 - Microsoft/#141.pdf -------------------------------------------------------------------------------- /#100 - #199/#142 - Google/#142.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#142 - Google/#142.pdf -------------------------------------------------------------------------------- /#100 - #199/#142 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 28.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(isBalanced("(()*")); // true 9 | System.out.println(isBalanced("(*)")); // true 10 | System.out.println(isBalanced(")*(")); // false 11 | } 12 | 13 | public static boolean isBalanced(String str) { 14 | int count = 0; 15 | int star = 0; 16 | 17 | for (int i = 0; i < str.length(); i++) { 18 | if (str.charAt(i) == '*') 19 | star++; 20 | else if (str.charAt(i) == '(') 21 | count++; 22 | else if (str.charAt(i) == ')') { 23 | if (--count < 0) { 24 | count++; 25 | 26 | if (--star < 0) 27 | return false; 28 | } 29 | } 30 | } 31 | 32 | return Math.abs(count) <= star; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /#100 - #199/#143 - Amazon/#143.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#143 - Amazon/#143.pdf -------------------------------------------------------------------------------- /#100 - #199/#143 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 15.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[] arr = {9, 12, 3, 5, 14, 10, 10}; 11 | System.out.println(Arrays.toString(arr)); 12 | System.out.println(Arrays.toString(partition(arr, 10))); 13 | } 14 | 15 | public static int[] partition(int[] arr, int x) { 16 | for (int i = 0, lt = 0, gt = arr.length - 1; i < gt; ) { 17 | if (arr[i] < x) 18 | swap(arr, lt++, i++); 19 | else if (arr[i] > x) 20 | swap(arr, gt--, i); 21 | else 22 | i++; 23 | } 24 | 25 | return arr; 26 | } 27 | 28 | private static void swap(int[] arr, int i, int j) { 29 | int tmp = arr[i]; 30 | arr[i] = arr[j]; 31 | arr[j] = tmp; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /#100 - #199/#144 - Google/#144.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#144 - Google/#144.pdf -------------------------------------------------------------------------------- /#100 - #199/#144 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.HashMap; 3 | import java.util.HashSet; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.TreeSet; 7 | 8 | /** 9 | * @author Oleg Cherednik 10 | * @since 18.01.2019 11 | */ 12 | public class Solution { 13 | 14 | public static void main(String... args) { 15 | System.out.println(findNearestLargerNumberIndex(0)); 16 | } 17 | 18 | private static final int[] arr = { 4, 1, 3, 5, 6, 4 }; 19 | private static final Map> mapNumPos = new HashMap<>(); 20 | private static final Map mapNumNextNum = new HashMap<>(); 21 | 22 | static { 23 | Set unique = new TreeSet<>(); 24 | 25 | for (int i = 0; i < arr.length; i++) { 26 | unique.add(arr[i]); 27 | 28 | if (!mapNumPos.containsKey(arr[i])) 29 | mapNumPos.put(arr[i], new HashSet<>()); 30 | 31 | mapNumPos.get(arr[i]).add(i); 32 | } 33 | 34 | Integer prv = null; 35 | 36 | for (Integer num : unique) { 37 | if (prv != null) 38 | mapNumNextNum.put(prv, num); 39 | prv = num; 40 | } 41 | } 42 | 43 | public static int findNearestLargerNumberIndex(int i) { 44 | int num = arr[i]; 45 | Integer nextNum = mapNumNextNum.get(num); 46 | Set nextNumPos = nextNum != null ? mapNumPos.get(nextNum) : Collections.emptySet(); 47 | return nextNumPos.isEmpty() ? -1 : nextNumPos.iterator().next(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#145 - Google/#145.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#145 - Google/#145.pdf -------------------------------------------------------------------------------- /#100 - #199/#146 - BufferBox/#146.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#146 - BufferBox/#146.pdf -------------------------------------------------------------------------------- /#100 - #199/#146 - BufferBox/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 16.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | Node root = createTree(); 9 | root = prune(root); 10 | } 11 | 12 | private static Node createTree() { 13 | Node one = new Node(1); 14 | one.left = new Node(0); 15 | one.right = new Node(0); 16 | 17 | Node two = new Node(0); 18 | two.left = one; 19 | two.right = new Node(0); 20 | 21 | Node root = new Node(0); 22 | root.left = new Node(1); 23 | root.right = two; 24 | 25 | return root; 26 | } 27 | 28 | public static Node prune(Node node) { 29 | if (node == null) 30 | return null; 31 | 32 | node.left = prune(node.left); 33 | node.right = prune(node.right); 34 | return node.data == 0 && node.left == null && node.right == null ? null : node; 35 | } 36 | 37 | private static final class Node { 38 | private final int data; 39 | private Node left; 40 | private Node right; 41 | 42 | public Node(int data) { 43 | this.data = data; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /#100 - #199/#147/#147.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#147/#147.pdf -------------------------------------------------------------------------------- /#100 - #199/#147/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 14.01.2019 6 | */ 7 | public class Solution { 8 | public static void main(String[] args) { 9 | System.out.println(Arrays.toString(sort(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))); 10 | System.out.println(Arrays.toString(sort(new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }))); 11 | System.out.println(Arrays.toString(sort(new int[] { 18, 3, 11, 5, 9, 17, 18, 13, 5, 7 }))); 12 | } 13 | 14 | public static int[] sort(int[] arr) { 15 | // bubble sort (but Pancake sorting should be) 16 | for (int left = 0, right = arr.length - 1; left < right; left++, right--) { 17 | for (int i = left; i < right; i++) 18 | if (arr[i] > arr[i + 1]) 19 | reverse(arr, i, i + 1); 20 | 21 | for (int i = right; i > left; i--) 22 | if (arr[i - 1] > arr[i]) 23 | reverse(arr, i - 1, i); 24 | } 25 | 26 | return arr; 27 | } 28 | 29 | private static void reverse(int[] arr, int i, int j) { 30 | while (i < j) { 31 | int tmp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = tmp; 34 | i++; 35 | j--; 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /#100 - #199/#148 - Apple/#148.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#148 - Apple/#148.pdf -------------------------------------------------------------------------------- /#100 - #199/#148 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 16.02.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int length = 3; 11 | Arrays.stream(grayCode(length)) 12 | .mapToObj(code -> String.format("%" + length + 's', Integer.toBinaryString(code)).replace(' ', '0')) 13 | .forEach(System.out::println); 14 | } 15 | 16 | public static int[] grayCode(int n) { 17 | return gray(0, n, new int[(int)Math.pow(2, n)]); 18 | } 19 | 20 | private static int[] gray(int curLength, int maxLength, int[] codes) { 21 | if (curLength > maxLength) 22 | return codes; 23 | if (curLength == 0) 24 | codes[0] = 0; 25 | else 26 | for (int i = 0, t = 1 << (curLength - 1); i < t; i++) 27 | codes[t + i] = codes[t - i - 1] + (1 << (curLength - 1)); 28 | 29 | return gray(curLength + 1, maxLength, codes); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#100 - #199/#149 - Goldman Sachs/#149.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#149 - Goldman Sachs/#149.pdf -------------------------------------------------------------------------------- /#100 - #199/#149 - Goldman Sachs/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.HashMap; 3 | import java.util.Map; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 16.01.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | System.out.println(sum(1, 3)); // 5 13 | } 14 | 15 | private static final Map SUM = new HashMap<>(); 16 | 17 | static { 18 | int i = 0; 19 | long sum = 0; 20 | 21 | SUM.put(-1, 0L); 22 | 23 | for (int num : Arrays.asList(1, 2, 3, 4, 5, 6)) 24 | SUM.put(i++, sum += num); 25 | } 26 | 27 | public static long sum(int i, int j) { 28 | return SUM.get(j - 1) - SUM.get(i - 1); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /#100 - #199/#150 - LinkedIn/#150.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#150 - LinkedIn/#150.pdf -------------------------------------------------------------------------------- /#100 - #199/#150 - LinkedIn/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.Comparator; 4 | import java.util.List; 5 | import java.util.PriorityQueue; 6 | import java.util.Queue; 7 | import java.util.function.Function; 8 | 9 | /** 10 | * @author Oleg Cherednik 11 | * @since 17.01.2019 12 | */ 13 | public class Solution { 14 | 15 | public static void main(String... args) { 16 | List points = Arrays.asList(new Point(0, 0), new Point(5, 4), new Point(3, 1)); 17 | getClosest(points, new Point(1, 2), 2).forEach(point -> System.out.println(point.x + ":" + point.y)); 18 | } 19 | 20 | public static List getClosest(List points, Point centralPoint, int k) { 21 | final Function getDistance = point -> (point.x - centralPoint.x) * (point.x - centralPoint.x) 22 | + (point.y - centralPoint.y) * (point.y - centralPoint.y); 23 | final Comparator sortByDistanceDesc = (one, two) -> Integer.compare(getDistance.apply(two), getDistance.apply(one)); 24 | 25 | Queue queue = new PriorityQueue<>(sortByDistanceDesc); 26 | 27 | points.forEach(point -> { 28 | if (queue.size() < k) 29 | queue.add(point); 30 | else if (sortByDistanceDesc.compare(queue.element(), point) < 0) { 31 | queue.remove(); 32 | queue.add(point); 33 | } 34 | }); 35 | 36 | return new ArrayList<>(queue); 37 | } 38 | 39 | private static final class Point { 40 | 41 | private final int x; 42 | private final int y; 43 | 44 | public Point(int x, int y) { 45 | this.x = x; 46 | this.y = y; 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /#100 - #199/#151/#151.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#151/#151.pdf -------------------------------------------------------------------------------- /#100 - #199/#151/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.stream.Collectors; 2 | import java.util.stream.IntStream; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 16.02.2019 7 | */ 8 | public class Solution { 9 | public static void main(String[] args) { 10 | char[][] image = { 11 | { 'B', 'B', 'W' }, 12 | { 'W', 'W', 'W' }, 13 | { 'W', 'W', 'W' }, 14 | { 'B', 'W', 'B' } }; 15 | print(image); 16 | replaceColor(image, 'G', 2, 2); 17 | print(image); 18 | } 19 | 20 | private static void print(char[][] image) { 21 | for (char[] row : image) 22 | System.out.println(IntStream.range(0, row.length) 23 | .mapToObj(col -> String.valueOf(row[col])) 24 | .collect(Collectors.joining(" "))); 25 | System.out.println(); 26 | } 27 | 28 | public static void replaceColor(char[][] image, char dest, int row, int col) { 29 | if (image[row][col] != dest) 30 | dfs(image, image[row][col], dest, row, col); 31 | } 32 | 33 | private static void dfs(char[][] image, char src, char dest, int row, int col) { 34 | if (row < 0 || row >= image.length) 35 | return; 36 | if (col < 0 || col >= image[row].length) 37 | return; 38 | if (image[row][col] != src) 39 | return; 40 | 41 | image[row][col] = dest; 42 | dfs(image, src, dest, row, col + 1); 43 | dfs(image, src, dest, row, col - 1); 44 | dfs(image, src, dest, row + 1, col); 45 | dfs(image, src, dest, row - 1, col); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /#100 - #199/#152 - Triplebyte/#152.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#152 - Triplebyte/#152.pdf -------------------------------------------------------------------------------- /#100 - #199/#153/#153.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#153/#153.pdf -------------------------------------------------------------------------------- /#100 - #199/#153/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 28.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | System.out.println(getMinDistance("dog cat hello cat dog dog hello cat world", "hello", "world")); // 1 9 | } 10 | 11 | public static int getMinDistance(String str, String one, String two) { 12 | String[] words = str.split("\\s+"); 13 | 14 | int posOne = -1; 15 | int posTwo = -1; 16 | int res = Integer.MAX_VALUE; 17 | 18 | for (int i = 0; i < words.length; i++) { 19 | if (words[i].equals(one)) 20 | posOne = i; 21 | else if (words[i].equals(two)) 22 | posTwo = i; 23 | else 24 | continue; 25 | 26 | if (posOne != -1 && posTwo != -1) 27 | res = Math.min(res, Math.abs(posTwo - posOne) - 1); 28 | } 29 | 30 | return res == Integer.MAX_VALUE ? -1 : res; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /#100 - #199/#154 - Amazon/#154.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#154 - Amazon/#154.pdf -------------------------------------------------------------------------------- /#100 - #199/#154 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.PriorityQueue; 3 | import java.util.Queue; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 16.02.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | int[] arr = { 9, 12, 3, 5, 14, 10, 10 }; 13 | System.out.println(Arrays.toString(arr)); 14 | 15 | Stack stack = new Stack<>(); 16 | 17 | for (int item : arr) 18 | stack.push(item); 19 | 20 | for (int i = 0; i < arr.length; i++) 21 | System.out.println(stack.pop()); 22 | } 23 | 24 | public static final class Stack { 25 | private final Queue> maxHeap = new PriorityQueue<>(); 26 | private int size; 27 | 28 | public void push(T item) { 29 | maxHeap.add(new Node(size++, item)); 30 | } 31 | 32 | public T pop() { 33 | if (size == 0) 34 | throw new RuntimeException(); 35 | 36 | size--; 37 | return maxHeap.remove().item; 38 | } 39 | 40 | public static final class Node implements Comparable> { 41 | private final int pos; 42 | private final T item; 43 | 44 | public Node(int pos, T item) { 45 | this.pos = pos; 46 | this.item = item; 47 | } 48 | 49 | @Override 50 | public int compareTo(Node obj) { 51 | return Integer.compare(obj.pos, pos); 52 | } 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /#100 - #199/#155 - MongoDB/#155.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#155 - MongoDB/#155.pdf -------------------------------------------------------------------------------- /#100 - #199/#155 - MongoDB/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 28.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String[] args) { 11 | System.out.println(findMajorityElement(new int[] { 1, 2, 1, 1, 3, 4, 0 })); // 1 12 | } 13 | 14 | public static int findMajorityElement(int[] arr) { 15 | Map map = new HashMap<>(); 16 | int res = -1; 17 | 18 | for (int num : arr) { 19 | int count = map.getOrDefault(num, 0) + 1; 20 | 21 | if (res == -1 || count > map.getOrDefault(res, 0)) 22 | res = num; 23 | 24 | map.put(num, count); 25 | } 26 | 27 | return res; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#100 - #199/#156 - Facebook/#156.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#156 - Facebook/#156.pdf -------------------------------------------------------------------------------- /#100 - #199/#156 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 27.05.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findSmallest(13)); // 2 9 | System.out.println(findSmallest(27)); // 3 10 | } 11 | 12 | public static int findSmallest(int n) { 13 | if (n == 0) 14 | return 0; 15 | 16 | int[] cache = new int[n + 1]; 17 | 18 | for (int i = 1; i < cache.length; i++) 19 | cache[i] = Integer.MAX_VALUE; 20 | 21 | for (int i = 1; i < cache.length; i++) { 22 | int j = 1; 23 | 24 | while (j * j <= i) { 25 | cache[i] = Math.min(cache[i], cache[i - j * j] + 1); 26 | j++; 27 | } 28 | } 29 | 30 | return cache[n]; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /#100 - #199/#157 - Amazon/#157.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#157 - Amazon/#157.pdf -------------------------------------------------------------------------------- /#100 - #199/#157 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 28.02.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(isRearrangePalindrome("carrace")); // true 12 | System.out.println(isRearrangePalindrome("daily")); // false 13 | } 14 | 15 | public static boolean isRearrangePalindrome(String str) { 16 | Map map = new HashMap<>(); 17 | 18 | for (int i = 0; i < str.length(); i++) 19 | map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1); 20 | 21 | int noPair = 0; 22 | 23 | for (int count : map.values()) 24 | if (count % 2 != 0) 25 | noPair++; 26 | 27 | return noPair <= 1; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /#100 - #199/#158 - Slack/#158.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#158 - Slack/#158.pdf -------------------------------------------------------------------------------- /#100 - #199/#158 - Slack/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 28.02.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | int[][] board = { 9 | { 0, 0, 1 }, 10 | { 0, 0, 1 }, 11 | { 1, 0, 0 } }; 12 | System.out.println(findWaysAmount(board)); // 2 13 | } 14 | 15 | public static int findWaysAmount(int[][] board) { 16 | final int width = board[0].length; 17 | final int height = board.length; 18 | 19 | int[] arr = new int[width]; 20 | arr[0] = 1; 21 | 22 | for (int row = 0; row < height; row++) { 23 | for (int col = 0; col < width; col++) { 24 | if (board[row][col] == 1) 25 | arr[col] = 0; 26 | else if (col > 0) 27 | arr[col] += arr[col - 1]; 28 | } 29 | } 30 | 31 | return arr[width - 1]; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /#100 - #199/#159 - Google/#159.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#159 - Google/#159.pdf -------------------------------------------------------------------------------- /#100 - #199/#159 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 20.03.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(getFirstRecurringChar("acbbac")); // b 12 | System.out.println(getFirstRecurringChar("abcdef")); // null 13 | } 14 | 15 | public static String getFirstRecurringChar(String str) { 16 | Set letters = new HashSet<>(); 17 | 18 | for (int i = 0; i < str.length(); i++) { 19 | char ch = str.charAt(i); 20 | 21 | if (letters.contains(ch)) 22 | return String.valueOf(ch); 23 | 24 | letters.add(ch); 25 | } 26 | 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#100 - #199/#160 - Uber/#160.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#160 - Uber/#160.pdf -------------------------------------------------------------------------------- /#100 - #199/#161 - Facebook/#161.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#161 - Facebook/#161.pdf -------------------------------------------------------------------------------- /#100 - #199/#161 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 31.03.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(Long.toBinaryString(reverseBits(0xF0F0F0F0))); // 0x0F0F0F0F 9 | } 10 | 11 | public static int reverseBits(int val) { 12 | for (long i = 1L, j = 1L << 31; i < j; i <<= 1, j >>= 1) { 13 | if ((val & i) != (val & j)) 14 | val = (int)(val & ~i & val & ~j | (~val & i) | (~val & j)); 15 | 16 | System.out.println(Long.toBinaryString(val)); 17 | } 18 | 19 | return val; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /#100 - #199/#162 - Square/#162.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#162 - Square/#162.pdf -------------------------------------------------------------------------------- /#100 - #199/#162 - Square/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | /** 8 | * @author Oleg Cherednik 9 | * @since 31.03.2019 10 | */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | List words = Arrays.asList("dog", "cat", "apple", "apricot", "fish"); 15 | List uniquePrefixes = getShortestUniquePrefix(words); 16 | uniquePrefixes.forEach(System.out::println); // d, c, app, apr, f 17 | } 18 | 19 | public static List getShortestUniquePrefix(List words) { 20 | Map prefixesAmount = new HashMap<>(); 21 | 22 | for (String word : words) { 23 | for (int i = 1; i <= word.length(); i++) { 24 | String prefix = word.substring(0, i); 25 | int amount = prefixesAmount.getOrDefault(prefix, 0); 26 | prefixesAmount.put(prefix, amount + 1); 27 | } 28 | } 29 | 30 | List uniquePrefixes = new ArrayList<>(); 31 | 32 | for (String word : words) { 33 | int size = uniquePrefixes.size(); 34 | 35 | for (int i = 1; i <= word.length(); i++) { 36 | String prefix = word.substring(0, i); 37 | 38 | if (prefixesAmount.get(prefix) == 1) { 39 | uniquePrefixes.add(prefix); 40 | break; 41 | } 42 | } 43 | 44 | if (uniquePrefixes.size() == size) 45 | throw new RuntimeException("Cannot get unique prefix for '" + word + '\''); 46 | } 47 | 48 | return uniquePrefixes; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /#100 - #199/#163 - Jane Street/#163.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#163 - Jane Street/#163.pdf -------------------------------------------------------------------------------- /#100 - #199/#163 - Jane Street/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 31.03.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(evaluateReversePolishNotation(new Object[] { 5, 3, '+' })); // 8 12 | System.out.println(evaluateReversePolishNotation(new Object[] { 15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-' })); // 5 13 | } 14 | 15 | public static double evaluateReversePolishNotation(Object[] expr) { 16 | Deque stack = new LinkedList<>(); 17 | 18 | 19 | for (Object item : expr) { 20 | if (item instanceof Number) 21 | stack.push(((Number)item).doubleValue()); 22 | else { 23 | double two = stack.pop(); 24 | double one = stack.pop(); 25 | char op = (Character)item; 26 | 27 | if (op == '+') 28 | stack.push(one + two); 29 | else if (op == '-') 30 | stack.push(one - two); 31 | else if (op == '/') 32 | stack.push(one / two); 33 | else if (op == '*') 34 | stack.push(one * two); 35 | } 36 | } 37 | 38 | return stack.pop(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /#100 - #199/#164 - Google/#164.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#164 - Google/#164.pdf -------------------------------------------------------------------------------- /#100 - #199/#164 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 31.03.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findDuplicate(new int[] { 1, 2, 3, 4, 5, 3 })); // 3 9 | System.out.println(findDuplicate(new int[] { 1, 2, 3, 4, 4, 5 })); // 4 10 | } 11 | 12 | public static int findDuplicate(int[] arr) { 13 | int expected = 0; 14 | int actual = 0; 15 | 16 | for (int i = 0; i < arr.length; i++) { 17 | expected += i; 18 | actual += arr[i]; 19 | } 20 | 21 | return actual - expected; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /#100 - #199/#165 - Google/#165.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#165 - Google/#165.pdf -------------------------------------------------------------------------------- /#100 - #199/#165 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 31.03.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(modify(new int[] { 3, 4, 9, 6, 1 }))); // [1, 1, 2, 1, 0] 11 | } 12 | 13 | public static int[] modify(int[] arr) { 14 | int[] res = new int[arr.length]; 15 | 16 | for (int i = arr.length - 1; i >= 0; i--) { 17 | for (int j = i + 1; j < arr.length; j++) { 18 | if (arr[j] > arr[i]) 19 | continue; 20 | 21 | res[i] = arr[j] == arr[i] ? res[j] : res[j] + 1; 22 | break; 23 | } 24 | } 25 | 26 | return res; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /#100 - #199/#166 - Uber/#166.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#166 - Uber/#166.pdf -------------------------------------------------------------------------------- /#100 - #199/#166 - Uber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 01.04.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | int[][] data = { 9 | { 1, 2 }, 10 | { 3 }, 11 | {}, 12 | { 4, 5, 6 } }; 13 | 14 | Iterator2D it = new Iterator2D(data); 15 | 16 | for (int i = 0; i < 7; i++) 17 | System.out.println(it.next()); 18 | } 19 | 20 | public static final class Iterator2D { 21 | private final int[][] data; 22 | private int row = 0; 23 | private int col = -1; 24 | private boolean used = true; 25 | 26 | public Iterator2D(int[][] data) { 27 | this.data = data; 28 | } 29 | 30 | public int next() { 31 | if (!findNextElement()) 32 | throw new RuntimeException("No more elements"); 33 | 34 | used = true; 35 | return data[row][col]; 36 | } 37 | 38 | public boolean has_next() { 39 | return findNextElement(); 40 | } 41 | 42 | private boolean findNextElement() { 43 | if (row >= data.length) 44 | return false; 45 | if (!used) 46 | return true; 47 | 48 | used = false; 49 | 50 | while (row < data.length) { 51 | col++; 52 | 53 | if (col < data[row].length) 54 | return true; 55 | 56 | col = -1; 57 | row++; 58 | } 59 | 60 | return false; 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /#100 - #199/#167 - Airbnb/#167.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#167 - Airbnb/#167.pdf -------------------------------------------------------------------------------- /#100 - #199/#167 - Airbnb/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 01.04.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | String[] words = { "code", "edoc", "da", "d" }; 12 | List pairs = findAllPalindromePair(words); 13 | pairs.forEach(pair -> System.out.println("(" + pair.i + ", " + pair.j + ')')); 14 | } 15 | 16 | public static List findAllPalindromePair(String[] words) { 17 | List res = new ArrayList<>(); 18 | 19 | for (int i = 0; i < words.length; i++) { 20 | for (int j = i + 1; j < words.length; j++) { 21 | if (isPalindrome(words[i] + words[j])) 22 | res.add(new Pair(i, j)); 23 | if (isPalindrome(words[j] + words[i])) 24 | res.add(new Pair(j, i)); 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | 31 | private static boolean isPalindrome(String word) { 32 | for (int i = 0, j = word.length() - 1; i < j; i++, j--) 33 | if (word.charAt(i) != word.charAt(j)) 34 | return false; 35 | 36 | return true; 37 | } 38 | 39 | public static final class Pair { 40 | private final int i; 41 | private final int j; 42 | 43 | public Pair(int i, int j) { 44 | this.i = i; 45 | this.j = j; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#168 - Facebook/#168.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#168 - Facebook/#168.pdf -------------------------------------------------------------------------------- /#100 - #199/#168 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 01.04.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[][] matrix = { 11 | { 1, 2, 3 }, 12 | { 4, 5, 6 }, 13 | { 7, 8, 9 } }; 14 | 15 | rotateClockwise(matrix); 16 | 17 | for (int[] row : matrix) 18 | System.out.println(Arrays.toString(row)); // [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 19 | } 20 | 21 | public static void rotateClockwise(int[][] matrix) { 22 | final int width = matrix[0].length; 23 | final int height = matrix.length; 24 | 25 | // flip horizontally 26 | for (int row = 0; row < height; row++) 27 | for (int col1 = 0, col2 = width - 1; col1 < col2; col1++, col2--) 28 | swap(matrix, row, col1, row, col2); 29 | 30 | // flip diagonally 31 | for (int i = 0; i < width - 1; i++) { 32 | int row1 = i; 33 | int row2 = height - 1; 34 | int col1 = 0; 35 | int col2 = width - i - 1; 36 | 37 | while (row1 >= 0) { 38 | swap(matrix, row1--, col1++, row2--, col2++); 39 | } 40 | } 41 | } 42 | 43 | private static void swap(int[][] matrix, int row1, int col1, int row2, int col2) { 44 | int tmp = matrix[row1][col1]; 45 | matrix[row1][col1] = matrix[row2][col2]; 46 | matrix[row2][col2] = tmp; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#169 - Google/#169.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#169 - Google/#169.pdf -------------------------------------------------------------------------------- /#100 - #199/#170 - Facebook/#170.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#170 - Facebook/#170.pdf -------------------------------------------------------------------------------- /#100 - #199/#171 - Amazon/#171.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#171 - Amazon/#171.pdf -------------------------------------------------------------------------------- /#100 - #199/#172 - Dropbox/#172.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#172 - Dropbox/#172.pdf -------------------------------------------------------------------------------- /#100 - #199/#173 - Stripe/#173.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#173 - Stripe/#173.pdf -------------------------------------------------------------------------------- /#100 - #199/#173 - Stripe/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.LinkedHashMap; 3 | import java.util.Map; 4 | 5 | /** 6 | * @author Oleg Cherednik 7 | * @since 28.05.2019 8 | */ 9 | public class Solution { 10 | 11 | public static void main(String... args) { 12 | Map foo = new LinkedHashMap<>(); 13 | foo.put("a", 5); 14 | foo.put("bar", Collections.singletonMap("baz", 8)); 15 | 16 | Map dic = new LinkedHashMap<>(); 17 | dic.put("key", 3); 18 | dic.put("foo", foo); 19 | 20 | Map map = flatten(dic); 21 | map.forEach((key, value) -> System.out.println(key + ": " + value)); 22 | } 23 | 24 | public static Map flatten(Map dic) { 25 | return flatten(null, dic, new LinkedHashMap<>()); 26 | } 27 | 28 | private static Map flatten(String prefix, Map dic, Map map) { 29 | for (Map.Entry entry : dic.entrySet()) { 30 | String key = prefix == null ? entry.getKey() : prefix + '.' + entry.getKey(); 31 | Object value = entry.getValue(); 32 | 33 | if (value instanceof Map) 34 | flatten(key, (Map)value, map); 35 | else 36 | map.put(key, String.valueOf(value)); 37 | } 38 | 39 | return map; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /#100 - #199/#174 - Microsoft/#174.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#174 - Microsoft/#174.pdf -------------------------------------------------------------------------------- /#100 - #199/#175 - Google/#175.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#175 - Google/#175.pdf -------------------------------------------------------------------------------- /#100 - #199/#176 - Bloomberg/#176.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#176 - Bloomberg/#176.pdf -------------------------------------------------------------------------------- /#100 - #199/#176 - Bloomberg/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 28.05.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String[] args) { 8 | System.out.println(exists("abc", "bcd")); // true 9 | System.out.println(exists("foo", "bar")); // false 10 | } 11 | 12 | public static boolean exists(String one, String two) { 13 | if (one.length() != two.length()) 14 | return false; 15 | 16 | char[] arr = new char[26]; 17 | 18 | for (int i = 0; i < one.length(); i++) { 19 | char chOne = one.charAt(i); 20 | char chTwo = two.charAt(i); 21 | 22 | if (arr[chOne - 'a'] == '\0') 23 | arr[chOne - 'a'] = chTwo; 24 | 25 | if (arr[chOne - 'a'] != chTwo) 26 | return false; 27 | } 28 | 29 | return true; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#100 - #199/#177 - Airbnb/#177.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#177 - Airbnb/#177.pdf -------------------------------------------------------------------------------- /#100 - #199/#178 - Two Sigma/#178.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#178 - Two Sigma/#178.pdf -------------------------------------------------------------------------------- /#100 - #199/#179 - Google/#179.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#179 - Google/#179.pdf -------------------------------------------------------------------------------- /#100 - #199/#180 - Google/#180.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#180 - Google/#180.pdf -------------------------------------------------------------------------------- /#100 - #199/#180 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | import java.util.Arrays; 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | 7 | /** 8 | * @author Oleg Cherednik 9 | * @since 29.05.2019 10 | */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | System.out.println(Arrays.toString(interleave(new ArrayDeque<>(Arrays.asList(5, 4, 3, 2, 1))).toArray(new Integer[0]))); // [1, 5, 2, 4, 3] 15 | System.out.println(Arrays.toString(interleave(new ArrayDeque<>(Arrays.asList(4, 3, 2, 1))).toArray(new Integer[0]))); // [1, 4, 2, 3] 16 | } 17 | 18 | public static Deque interleave(Deque stack) { 19 | Queue queue = new LinkedList<>(); 20 | 21 | for (int i = stack.size() - 1; i > 1; i--) { 22 | for (int j = 0; j < i; j++) 23 | queue.add(stack.pop()); 24 | 25 | for (int j = 0; j < i; j++) 26 | stack.push(queue.remove()); 27 | } 28 | 29 | return stack; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#100 - #199/#181 - Google/#181.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#181 - Google/#181.pdf -------------------------------------------------------------------------------- /#100 - #199/#181 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.Collections; 4 | import java.util.List; 5 | import java.util.Optional; 6 | 7 | /** 8 | * @author Oleg Cherednik 9 | * @since 29.05.2019 10 | */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | System.out.println(Arrays.toString(split("racecarannakayak").toArray(new String[0]))); // ["racecar", "anna", "kayak"] 15 | System.out.println(Arrays.toString(split("abc").toArray(new String[0]))); // ["a", "b", "c"] 16 | } 17 | 18 | public static List split(String str) { 19 | return Optional.ofNullable(findPalindromes(str, 0, str.length() - 1)).orElse(Collections.emptyList()); 20 | } 21 | 22 | private static List findPalindromes(String str, int left, int right) { 23 | for (int i = right; i >= left; i--) { 24 | if (!isPalindrome(str, left, i)) 25 | continue; 26 | 27 | List tail = i + 1 <= right ? findPalindromes(str, i + 1, right) : Collections.emptyList(); 28 | 29 | if (tail == null) 30 | continue; 31 | 32 | List res = new ArrayList<>(tail.size() + 1); 33 | res.add(str.substring(left, i + 1)); 34 | res.addAll(tail); 35 | 36 | return res; 37 | } 38 | 39 | return null; 40 | } 41 | 42 | private static boolean isPalindrome(String str, int i, int j) { 43 | while (i < j) 44 | if (str.charAt(i++) != str.charAt(j--)) 45 | return false; 46 | 47 | return true; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /#100 - #199/#182 - Facebook/#182.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#182 - Facebook/#182.pdf -------------------------------------------------------------------------------- /#100 - #199/#183 - Twitch/#183.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#183 - Twitch/#183.pdf -------------------------------------------------------------------------------- /#100 - #199/#184 - Amazon/#184.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#184 - Amazon/#184.pdf -------------------------------------------------------------------------------- /#100 - #199/#184 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 31.04.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getGreatestCommonDenominator(new int[] { 42, 56, 14 })); // 14 9 | } 10 | 11 | public static int getGreatestCommonDenominator(int[] arr) { 12 | int min = getMin(arr); 13 | 14 | for (int i = 1, j = min / 2; i < j; i++) { 15 | int k = min / i; 16 | 17 | if (isAccepted(k, arr)) 18 | return k; 19 | } 20 | 21 | return Integer.MAX_VALUE; 22 | } 23 | 24 | private static int getMin(int[] arr) { 25 | int min = Integer.MAX_VALUE; 26 | 27 | for (int val : arr) 28 | min = Math.min(min, val); 29 | 30 | return min; 31 | } 32 | 33 | private static boolean isAccepted(int k, int[] arr) { 34 | for (int val : arr) 35 | if (val % k != 0) 36 | return false; 37 | return true; 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /#100 - #199/#185 - Google/#185.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#185 - Google/#185.pdf -------------------------------------------------------------------------------- /#100 - #199/#186 - Microsoft/#186.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#186 - Microsoft/#186.pdf -------------------------------------------------------------------------------- /#100 - #199/#187 - Google/#187.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#187 - Google/#187.pdf -------------------------------------------------------------------------------- /#100 - #199/#188 - Google/#188.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#188 - Google/#188.pdf -------------------------------------------------------------------------------- /#100 - #199/#189 - Google/#189.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#189 - Google/#189.pdf -------------------------------------------------------------------------------- /#100 - #199/#189 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 31.05.2019 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(getLongestDistinctLength(new int[] { 5, 1, 3, 5, 2, 3, 4, 1 })); // 5 12 | } 13 | 14 | public static int getLongestDistinctLength(int[] arr) { 15 | int res = 0; 16 | Set unique = new HashSet<>(); 17 | 18 | for (int i = 0, j = 0; j < arr.length; ) { 19 | if (unique.contains(arr[j])) 20 | unique.remove(arr[i++]); 21 | else { 22 | unique.add(arr[j]); 23 | res = Math.max(res, unique.size()); 24 | j++; 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /#100 - #199/#190 - Facebook/#190.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#190 - Facebook/#190.pdf -------------------------------------------------------------------------------- /#100 - #199/#190 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 31.04.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findMaxSubarraySum(new int[] { 8, -1, 3, 4 })); // 15; 9 | System.out.println(findMaxSubarraySum(new int[] { -4, 5, 1, 0 })); // 6; 10 | } 11 | 12 | public static int findMaxSubarraySum(int[] arr) { 13 | int[] sum = new int[arr.length]; 14 | sum[0] = arr[0]; 15 | 16 | for (int i = 1; i < sum.length; i++) 17 | if (arr[i] >= 0) 18 | sum[i] = Math.max(sum[i - 1] + arr[i], arr[i]); 19 | else 20 | sum[i] = arr[i]; 21 | 22 | int max = 0; 23 | int left = sum[0]; 24 | 25 | for (int i = 0; i < sum.length; i++) { 26 | max = Math.max(max, sum[i]); 27 | 28 | if (sum[0] < 0 || i > 0 && sum[i] < sum[i - 1]) 29 | continue; 30 | 31 | left = Math.max(left, max); 32 | } 33 | 34 | return Math.max(max, left + sum[sum.length - 1]); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /#100 - #199/#191 - Stripe/#191.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#191 - Stripe/#191.pdf -------------------------------------------------------------------------------- /#100 - #199/#192 - Google/#192.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#192 - Google/#192.pdf -------------------------------------------------------------------------------- /#100 - #199/#192 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 01.06.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(canGetEnd(new int[] { 1, 3, 1, 2, 0, 1 })); // true 9 | System.out.println(canGetEnd(new int[] { 1, 2, 1, 0, 0 })); // false 10 | System.out.println(canGetEnd(new int[] { 2, 2, 0, 0 })); // false 11 | } 12 | 13 | public static boolean canGetEnd(int[] arr) { 14 | boolean[] tmp = new boolean[arr.length]; 15 | 16 | for (int i = arr.length - 1; i >= 0; i--) { 17 | tmp[i] = i + arr[i] >= arr.length; 18 | 19 | for (int j = i + 1; j <= Math.min(i + arr[i], arr.length - 1) && !tmp[i]; j++) 20 | tmp[i] = tmp[j]; 21 | } 22 | 23 | return tmp[0]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /#100 - #199/#193 - Affirm/#193.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#193 - Affirm/#193.pdf -------------------------------------------------------------------------------- /#100 - #199/#193 - Affirm/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 23.09.2022 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(calcMaxProfit1(new int[] { 1, 3, 2, 8, 4, 10 }, 2)); // 9 9 | System.out.println(calcMaxProfit(new int[] { 1, 3, 2, 8, 4, 10 }, 2)); // 9 10 | } 11 | 12 | private static int calcMaxProfit1(int[] prices, int fee) { 13 | int curMaxProfit = 0; 14 | int hold = prices[0]; 15 | 16 | for (int i = 1; i < prices.length; i++) { 17 | curMaxProfit = Math.max(curMaxProfit, prices[i] - hold - fee); 18 | hold = Math.min(hold, prices[i] - curMaxProfit); 19 | } 20 | 21 | return curMaxProfit; 22 | } 23 | 24 | private static int calcMaxProfit(int[] prices, int fee) { 25 | return calcMaxProfit(prices, 0, fee); 26 | } 27 | 28 | private static int calcMaxProfit(int[] prices, int lo, int fee) { 29 | if (lo >= prices.length - 1) 30 | return 0; 31 | 32 | int i = lo + 1; 33 | int maxProfit = 0; 34 | 35 | for (; i < prices.length; i++) { 36 | int curProfit = prices[i] - prices[lo] - fee; 37 | 38 | if (curProfit > 0) { 39 | int curMaxProfit = calcMaxProfit(prices, i + 1, fee); 40 | maxProfit = Math.max(maxProfit, curProfit + curMaxProfit); 41 | } 42 | } 43 | 44 | return maxProfit; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /#100 - #199/#194 - Facebook/#194.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#194 - Facebook/#194.pdf -------------------------------------------------------------------------------- /#100 - #199/#195 - Google/#195.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#195 - Google/#195.pdf -------------------------------------------------------------------------------- /#100 - #199/#195 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 02.06.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | int[][] matrix = { 9 | { 1, 3, 7, 10, 15, 20 }, 10 | { 2, 6, 9, 14, 22, 25 }, 11 | { 3, 8, 10, 15, 25, 30 }, 12 | { 10, 11, 12, 23, 30, 35 }, 13 | { 20, 25, 30, 35, 40, 45 } }; 14 | System.out.println(count(matrix, 1, 1, 3, 3)); 15 | } 16 | 17 | public static int count(int[][] matrix, int i1, int j1, int i2, int j2) { 18 | int res = 0; 19 | int low = matrix[i1][j1]; 20 | int high = matrix[i2][j2]; 21 | 22 | for (int[] row : matrix) { 23 | if (row[0] > high || row[row.length - 1] < low) 24 | res += row.length; 25 | else if (row[0] >= low && row[row.length - 1] <= high) 26 | continue; 27 | 28 | for (int i = 0; i < row.length && row[i] < low; i++) 29 | res++; 30 | for (int i = row.length - 1; i >= 0 && row[i] > high; i--) 31 | res++; 32 | } 33 | 34 | return res; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /#100 - #199/#196 - Apple/#196.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#196 - Apple/#196.pdf -------------------------------------------------------------------------------- /#100 - #199/#197 - Amazon/#197.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#197 - Amazon/#197.pdf -------------------------------------------------------------------------------- /#100 - #199/#197 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 03.06.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | int[] arr = { 1, 2, 3, 4, 5 }; 11 | rotateRight(arr, 2); 12 | System.out.println(Arrays.toString(arr)); // { 4, 5, 1, 2, 3 } 13 | } 14 | 15 | public static void rotateRight(int[] arr, int k) { 16 | k %= arr.length; 17 | 18 | reverse(arr, 0, arr.length - 1); 19 | reverse(arr, 0, k - 1); 20 | reverse(arr, k, arr.length - 1); 21 | } 22 | 23 | private static void reverse(int[] arr, int l, int r) { 24 | while (l < r) 25 | swap(arr, l++, r--); 26 | } 27 | 28 | private static void swap(int[] arr, int i, int j) { 29 | int tmp = arr[i]; 30 | arr[i] = arr[j]; 31 | arr[j] = tmp; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /#100 - #199/#198 - Google/#198.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#198 - Google/#198.pdf -------------------------------------------------------------------------------- /#100 - #199/#198 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 03.06.2019 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(findLargestSubsetWithCondition(new int[] { 3, 5, 10, 20, 21 }))); // { 5, 10, 20 } 11 | System.out.println(Arrays.toString(findLargestSubsetWithCondition(new int[] { 1, 3, 6, 24 }))); // { 1, 3, 6, 24 } 12 | } 13 | 14 | public static int[] findLargestSubsetWithCondition(int[] arr) { 15 | int length = 0; 16 | int offs = 0; 17 | 18 | for (int i = 0, j = 0; j < arr.length; ) { 19 | int min = Math.min(arr[i], arr[j]); 20 | int max = Math.max(arr[i], arr[j]); 21 | 22 | if (max % min == 0) { 23 | if (j - i + 1 > length) { 24 | length = j - i + 1; 25 | offs = i; 26 | } 27 | 28 | j++; 29 | } else 30 | i = j; 31 | } 32 | 33 | int[] res = new int[length]; 34 | 35 | for (int i = 0; i < length; i++) 36 | res[i] = arr[offs + i]; 37 | 38 | return res; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /#100 - #199/#199 - Facebook/#199.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#100 - #199/#199 - Facebook/#199.pdf -------------------------------------------------------------------------------- /#100 - #199/#199 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 03.04.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findBalancedString("(()")); // (()); 9 | System.out.println(findBalancedString("))()(")); // ()(); 10 | } 11 | 12 | public static String findBalancedString(String str) { 13 | StringBuilder buf = new StringBuilder(); 14 | int count = 0; 15 | 16 | for (int i = 0; i < str.length(); i++) { 17 | char ch = str.charAt(i); 18 | 19 | if (ch == '(') { 20 | count++; 21 | buf.append('('); 22 | } else if (ch == ')') { 23 | if (count > 0) { 24 | buf.append(')'); 25 | count--; 26 | } 27 | } 28 | } 29 | 30 | for (int i = 0; i < count; i++) 31 | buf.append(')'); 32 | 33 | return buf.toString(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /#200 - #299/#200 - Microsoft/#200.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#200 - Microsoft/#200.pdf -------------------------------------------------------------------------------- /#200 - #299/#201 - Google/#201.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#201 - Google/#201.pdf -------------------------------------------------------------------------------- /#200 - #299/#201 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 19.02.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findMaxWeightPath(new int[][] { { 1 }, { 2, 3 }, { 1, 5, 1 } })); // 9 9 | } 10 | 11 | public static int findMaxWeightPath(int[][] arr) { 12 | int res = 0; 13 | 14 | for (int row = 0; row < arr.length; row++) { 15 | for (int col = 0; col < arr[row].length; col++) { 16 | arr[row][col] += Math.max(left(arr, row, col), right(arr, row, col)); 17 | res = Math.max(res, arr[row][col]); 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | 24 | private static int left(int[][] arr, int row, int col) { 25 | return row == 0 || col == 0 ? 0 : arr[row - 1][col - 1]; 26 | } 27 | 28 | private static int right(int[][] arr, int row, int col) { 29 | return row == 0 || col == arr[row].length - 1 ? 0 : arr[row - 1][col]; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /#200 - #299/#202 - Palantir/#202.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#202 - Palantir/#202.pdf -------------------------------------------------------------------------------- /#200 - #299/#202 - Palantir/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 04.06.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(isPalindrome(121)); // true 9 | System.out.println(isPalindrome(888)); // true 10 | System.out.println(isPalindrome(678)); // true 11 | } 12 | 13 | public static boolean isPalindrome(int val) { 14 | int width = getWidth(val); 15 | 16 | for (int i = 0, j = width - 1; i < j; i++, j--) 17 | if (getDigit(val, i) != getDigit(val, j)) 18 | return false; 19 | 20 | return true; 21 | } 22 | 23 | private static int getWidth(int val) { 24 | int width = 0; 25 | 26 | while (val != 0) { 27 | width++; 28 | val /= 10; 29 | } 30 | 31 | return width; 32 | } 33 | 34 | private static int getDigit(int val, int i) { 35 | while (i-- > 0) { 36 | val /= 10; 37 | } 38 | 39 | return val % 10; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /#200 - #299/#203 - Uber/#203.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#203 - Uber/#203.pdf -------------------------------------------------------------------------------- /#200 - #299/#203 - Uber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 04.06.2019 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(findOffs(new int[] { 5, 7, 10, 3, 4 })); // 3 9 | System.out.println(findOffs(new int[] { 4, 5, 6, 7, 1, 2, 3 })); // 4 10 | System.out.println(findOffs(new int[] { 2, 1 })); // 1 11 | } 12 | 13 | public static int findOffs(int[] arr) { 14 | return find(arr, 0, arr.length - 1); 15 | } 16 | 17 | private static int find(int[] arr, int i, int j) { 18 | if (i == j) 19 | return i; 20 | int mid = (i + j) / 2; 21 | return arr[mid] < arr[j] ? find(arr, i, mid) : find(arr, mid + 1, j); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /#200 - #299/#204 - Amazon/#204.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#204 - Amazon/#204.pdf -------------------------------------------------------------------------------- /#200 - #299/#205 - IBM/#205.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#205 - IBM/#205.pdf -------------------------------------------------------------------------------- /#200 - #299/#206 - Twitter/#206.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#206 - Twitter/#206.pdf -------------------------------------------------------------------------------- /#200 - #299/#207 - Dropbox/#207.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#207 - Dropbox/#207.pdf -------------------------------------------------------------------------------- /#200 - #299/#208 - LinkedIn/#208.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#208 - LinkedIn/#208.pdf -------------------------------------------------------------------------------- /#200 - #299/#209 - YouTube/#209.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#209 - YouTube/#209.pdf -------------------------------------------------------------------------------- /#200 - #299/#210 - Apple/#210.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#210 - Apple/#210.pdf -------------------------------------------------------------------------------- /#200 - #299/#211 - Microsoft/#211.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#211 - Microsoft/#211.pdf -------------------------------------------------------------------------------- /#200 - #299/#212 - Dropbox/#212.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#212 - Dropbox/#212.pdf -------------------------------------------------------------------------------- /#200 - #299/#212 - Dropbox/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 05.12.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(getColumnName(1)); // A 9 | System.out.println(getColumnName(10)); // J 10 | System.out.println(getColumnName(100)); // CV 11 | System.out.println(getColumnName(500)); // SF 12 | System.out.println(getColumnName(1000)); // ALL 13 | } 14 | 15 | public static String getColumnName(int num) { 16 | StringBuilder buf = new StringBuilder(); 17 | 18 | while (num > 0) { 19 | buf.append((char)('A' + num % 26 - 1)); 20 | num /= 26; 21 | } 22 | 23 | return buf.reverse().toString(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /#200 - #299/#213 - Snapchat/#213.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#213 - Snapchat/#213.pdf -------------------------------------------------------------------------------- /#200 - #299/#214 - Stripe/#214.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#214 - Stripe/#214.pdf -------------------------------------------------------------------------------- /#200 - #299/#215 - Yelp/#215.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#215 - Yelp/#215.pdf -------------------------------------------------------------------------------- /#200 - #299/#216 - Facebook/#216.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#216 - Facebook/#216.pdf -------------------------------------------------------------------------------- /#200 - #299/#217 - Oracle/#217.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#217 - Oracle/#217.pdf -------------------------------------------------------------------------------- /#200 - #299/#218 - Yahoo/#218.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#218 - Yahoo/#218.pdf -------------------------------------------------------------------------------- /#200 - #299/#218 - Yahoo/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 05.12.2020 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(reverseList(1, 2, 3, 4, 5, 6)); // [6, 5, 4, 3, 2, 1] 12 | } 13 | 14 | private static List reverseList(int... vals) { 15 | Node head = build(vals); 16 | head = reverseList(head); 17 | 18 | List res = new ArrayList<>(); 19 | 20 | while (head != null) { 21 | res.add(head.val); 22 | head = head.next; 23 | } 24 | 25 | return res; 26 | } 27 | 28 | // 1 -> 2 -> 3 29 | private static Node reverseList(Node head) { 30 | Node prv = null; 31 | 32 | while (head != null) { 33 | Node next = head.next; 34 | head.next = prv; 35 | prv = head; 36 | head = next; 37 | } 38 | 39 | return prv; 40 | } 41 | 42 | private static Node build(int... vals) { 43 | Node head = null; 44 | Node tail = null; 45 | 46 | for (int val : vals) { 47 | Node node = new Node(val); 48 | 49 | if (head == null) 50 | head = node; 51 | else 52 | tail.next = node; 53 | 54 | tail = node; 55 | } 56 | 57 | return head; 58 | } 59 | 60 | private static final class Node { 61 | 62 | private final int val; 63 | private Node next; 64 | 65 | public Node(int val) { 66 | this.val = val; 67 | } 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /#200 - #299/#219 - Salesforce/#219.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#219 - Salesforce/#219.pdf -------------------------------------------------------------------------------- /#200 - #299/#220 - Square/#220.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#220 - Square/#220.pdf -------------------------------------------------------------------------------- /#200 - #299/#221 - Zillow/#221.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#221 - Zillow/#221.pdf -------------------------------------------------------------------------------- /#200 - #299/#222 - Quora/#222.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#222 - Quora/#222.pdf -------------------------------------------------------------------------------- /#200 - #299/#223 - Palantir/#223.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#223 - Palantir/#223.pdf -------------------------------------------------------------------------------- /#200 - #299/#224 - Amazon/#224.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#224 - Amazon/#224.pdf -------------------------------------------------------------------------------- /#200 - #299/#224 - Amazon/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.Comparator; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Oleg Cherednik 8 | * @since 24.09.2022 9 | */ 10 | public class Solution { 11 | 12 | public static void main(String... args) { 13 | System.out.println(getSmallestImpossibleSum(1, 2, 3, 10)); // 7 14 | } 15 | 16 | private static int getSmallestImpossibleSum(int... nums) { 17 | int sum = 1; 18 | 19 | for (int num : nums) { 20 | if (num > sum) 21 | break; 22 | 23 | sum += num; 24 | } 25 | 26 | return sum; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /#200 - #299/#225 - Bloomberg/#225.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#225 - Bloomberg/#225.pdf -------------------------------------------------------------------------------- /#200 - #299/#226 - Airbnb/#226.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#226 - Airbnb/#226.pdf -------------------------------------------------------------------------------- /#200 - #299/#227 - Facebook/#227.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#227 - Facebook/#227.pdf -------------------------------------------------------------------------------- /#200 - #299/#228 - Twitter/#228.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#228 - Twitter/#228.pdf -------------------------------------------------------------------------------- /#200 - #299/#229 - Flipkart/#229.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#229 - Flipkart/#229.pdf -------------------------------------------------------------------------------- /#200 - #299/#230 - Goldman Sachs/#230.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#230 - Goldman Sachs/#230.pdf -------------------------------------------------------------------------------- /#200 - #299/#231 - IBM/#231.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#231 - IBM/#231.pdf -------------------------------------------------------------------------------- /#200 - #299/#231 - IBM/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 08.03.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(rearrange("aaabbc")); // ababac 9 | System.out.println(rearrange("a")); // a 10 | System.out.println(rearrange("aaab")); // None 11 | System.out.println(rearrange("")); // None 12 | } 13 | 14 | public static String rearrange(String str) { 15 | char[] arr = groupLetters(str); 16 | 17 | if (arr == null) 18 | return "None"; 19 | 20 | for (int i = 1, lst = arr.length - 1; i < arr.length; i++) { 21 | if (arr[i] != arr[i - 1]) 22 | continue; 23 | if (arr[i] == arr[lst]) 24 | return "None"; 25 | arr[i] = arr[lst]; 26 | arr[lst] = arr[i - 1]; 27 | } 28 | 29 | return new String(arr); 30 | } 31 | 32 | private static char[] groupLetters(String str) { 33 | if (str == null || str.isEmpty()) 34 | return null; 35 | if (str.length() == 1) 36 | return str.toCharArray(); 37 | 38 | int[] counts = new int[26]; 39 | str = str.toLowerCase(); 40 | 41 | for (int i = 0; i < str.length(); i++) 42 | counts[str.charAt(i) - 'a']++; 43 | 44 | char[] arr = new char[str.length()]; 45 | 46 | for (int i = 0, j = 0; i < counts.length; i++) 47 | for (int k = 0; k < counts[i]; k++) 48 | arr[j++] = (char)('a' + i); 49 | 50 | return arr; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /#200 - #299/#232 - Google/#232.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#232 - Google/#232.pdf -------------------------------------------------------------------------------- /#200 - #299/#232 - Google/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 08.03.2020 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | PrefixMapSum mapsum = new PrefixMapSum(); 12 | 13 | mapsum.insert("columnar", 3); 14 | System.out.println(mapsum.sum("col")); // 3 15 | 16 | mapsum.insert("column", 2); 17 | System.out.println(mapsum.sum("col")); // 5 18 | } 19 | 20 | private static final class PrefixMapSum { 21 | 22 | private final Map map = new HashMap<>(); 23 | 24 | public void insert(String key, int value) { 25 | map.put(key, value); 26 | } 27 | 28 | public int sum(String prefix) { 29 | int sum = 0; 30 | 31 | for (Map.Entry entry : map.entrySet()) 32 | if (entry.getKey().startsWith(prefix)) 33 | sum += entry.getValue(); 34 | 35 | return sum; 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /#200 - #299/#233 - Apple/#233.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#233 - Apple/#233.pdf -------------------------------------------------------------------------------- /#200 - #299/#233 - Apple/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Oleg Cherednik 3 | * @since 18.02.2020 4 | */ 5 | public class Solution { 6 | 7 | public static void main(String... args) { 8 | System.out.println(fib(0)); // 0 9 | System.out.println(fib(1)); // 1 10 | System.out.println(fib(2)); // 1 11 | System.out.println(fib(3)); // 2 12 | System.out.println(fib(4)); // 3 13 | System.out.println(fib(5)); // 5 14 | System.out.println(fib(6)); // 8 15 | System.out.println(fib(22)); // 17711 16 | } 17 | 18 | public static long fib(int n) { 19 | long x1 = 1; 20 | long x2 = 0; 21 | 22 | for(int i = 0; i < n; i++) { 23 | x2 = x1 + x2; 24 | x1 = x2 - x1; 25 | } 26 | 27 | return x2; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /#200 - #299/#234 - Microsoft/#234.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#234 - Microsoft/#234.pdf -------------------------------------------------------------------------------- /#200 - #299/#235 - Facebook/#235.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#235 - Facebook/#235.pdf -------------------------------------------------------------------------------- /#200 - #299/#235 - Facebook/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 20.01.2021 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(Arrays.toString(getMinMax(4, 2, 7, 5, -1, 3, 6))); // [-1, 7] 11 | } 12 | 13 | public static int[] getMinMax(int... arr) { 14 | int min = arr[0]; 15 | int max = arr[0]; 16 | 17 | for (int i = 1; i < arr.length; i += 2) { 18 | int one = arr[i]; 19 | int two = i + 1 < arr.length ? arr[i + 1] : arr[i]; 20 | boolean oneLess = one < two; 21 | 22 | min = Math.min(min, oneLess ? one : two); 23 | max = Math.max(max, oneLess ? two : one); 24 | } 25 | 26 | return new int[] { min, max }; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /#200 - #299/#236 - Nvidia/#236.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#236 - Nvidia/#236.pdf -------------------------------------------------------------------------------- /#200 - #299/#237 - Amazon/#237.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#237 - Amazon/#237.pdf -------------------------------------------------------------------------------- /#200 - #299/#238 - Mit/#238.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#238 - Mit/#238.pdf -------------------------------------------------------------------------------- /#200 - #299/#239 - Uber/#239.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#239 - Uber/#239.pdf -------------------------------------------------------------------------------- /#200 - #299/#240 - Spotify/#240.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#240 - Spotify/#240.pdf -------------------------------------------------------------------------------- /#200 - #299/#241 - Palantir/#241.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#241 - Palantir/#241.pdf -------------------------------------------------------------------------------- /#200 - #299/#241 - Palantir/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * @author Oleg Cherednik 5 | * @since 08.03.2020 6 | */ 7 | public class Solution { 8 | 9 | public static void main(String... args) { 10 | System.out.println(calcIndex(new int[] { 4, 3, 0, 1, 5 })); // 3 11 | } 12 | 13 | public static int calcIndex(int[] papers) { 14 | Arrays.sort(papers); 15 | 16 | for (int i = papers.length - 1; 0 <= i; i--) 17 | if (papers[i] <= papers.length - i) 18 | return papers[i]; 19 | 20 | return 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /#200 - #299/#242 - Twitter/#242.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#242 - Twitter/#242.pdf -------------------------------------------------------------------------------- /#200 - #299/#243 - Etsy/#243.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#243 - Etsy/#243.pdf -------------------------------------------------------------------------------- /#200 - #299/#244 - Square/#244.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#244 - Square/#244.pdf -------------------------------------------------------------------------------- /#200 - #299/#244 - Square/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.HashSet; 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | import java.util.Set; 6 | 7 | /** 8 | * @author Oleg Cherednik 9 | * @since 18.02.2020 10 | */ 11 | public class Solution { 12 | 13 | public static void main(String... args) { 14 | System.out.println(Arrays.toString(getPrimeNumbersLessThan(100))); 15 | } 16 | 17 | public static int[] getPrimeNumbersLessThan(int n) { 18 | PrimeNumberGenerator primeGenerator = new PrimeNumberGenerator(); 19 | List primes = new LinkedList<>(); 20 | 21 | while (true) { 22 | int prime = primeGenerator.next(); 23 | 24 | if (prime >= n) 25 | break; 26 | 27 | primes.add(prime); 28 | } 29 | 30 | return primes.stream().mapToInt(i -> i).toArray(); 31 | } 32 | 33 | public static final class PrimeNumberGenerator { 34 | 35 | private final Set primes = new HashSet<>(); 36 | private int prime = 1; 37 | 38 | public int next() { 39 | int n = prime; 40 | 41 | do { 42 | n++; 43 | } while (isMarked(n)); 44 | 45 | primes.add(n); 46 | prime = n; 47 | 48 | return prime; 49 | } 50 | 51 | private boolean isMarked(int n) { 52 | for (int prime : primes) 53 | if (n % prime == 0) 54 | return true; 55 | return false; 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /#200 - #299/#245 - Yelp/#245.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#245 - Yelp/#245.pdf -------------------------------------------------------------------------------- /#200 - #299/#246 - Dropbox/#246.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/#200 - #299/#246 - Dropbox/#246.pdf -------------------------------------------------------------------------------- /#200 - #299/#246 - Dropbox/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * @author Oleg Cherednik 6 | * @since 22.05.2020 7 | */ 8 | public class Solution { 9 | 10 | public static void main(String... args) { 11 | System.out.println(isBalanced("([])[]({})")); // true 12 | System.out.println(isBalanced("([)]")); // false 13 | System.out.println(isBalanced("((()")); // false 14 | } 15 | 16 | public static boolean isBalanced(String str) { 17 | Deque stack = new LinkedList<>(); 18 | 19 | for (int i = 0; i < str.length(); i++) { 20 | char ch = str.charAt(i); 21 | 22 | if (ch == '(' || ch == '[' || ch == '{') 23 | stack.push(ch); 24 | else { 25 | if (stack.isEmpty()) 26 | return false; 27 | 28 | char prv = stack.pop(); 29 | 30 | if (prv == '(' && ch != ')') 31 | return false; 32 | if (prv == '[' && ch != ']') 33 | return false; 34 | if (prv == '{' && ch != '}') 35 | return false; 36 | } 37 | } 38 | 39 | return stack.isEmpty(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | # Packages # 11 | ############ 12 | # it's better to unpack these files and commit the raw source 13 | # git has its own built in compression methods 14 | *.7z 15 | *.dmg 16 | *.gz 17 | *.iso 18 | #*.jar 19 | *.rar 20 | *.tar 21 | 22 | # Logs and databases # 23 | ###################### 24 | *.log 25 | #*.sql 26 | *.sqlite 27 | 28 | # OS generated files # 29 | ###################### 30 | .DS_Store 31 | .DS_Store? 32 | ._* 33 | .Spotlight-V100 34 | .Trashes 35 | thumbs.db 36 | Thumbs.db 37 | 38 | # IntelliJ IDEA # 39 | ################# 40 | *.iml 41 | .idea/ 42 | .classpath 43 | .project 44 | .settings/ 45 | target/ 46 | out/ 47 | #classes/ 48 | 49 | # Gradle # 50 | ################# 51 | .gradle/ 52 | atlassian*.xml 53 | build/ 54 | buildPlugins/ 55 | plugins/ 56 | gradle/ 57 | gradlew 58 | gradlew.bat 59 | 60 | dependency-reduced-pom.xml 61 | -------------------------------------------------------------------------------- /dcp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oleg-cherednik/DailyCodingProblem/5125a08cb8cfb97fd801d69723a84d37379c7072/dcp.jpg --------------------------------------------------------------------------------