└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # :sunny:-Implemented, :new_moon:-Ignored, :waxing_crescent_moon:-Pending 2 | 3 | 4 | ## Problem-1:sunny: 5 | 6 | 7 | > This problem was recently asked by Google. 8 | 9 | Given a list of numbers and a number k, return whether any two numbers from the 10 | list add up to k. 11 | 12 | For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. 13 | 14 | Bonus: Can you do this in one pass? 15 | 16 | ## [Problem-2](src/main/java/in/ashwanik/dcp/problems/p1_30/p2):sunny: 17 | 18 | 19 | > This problem was asked by Uber. 20 | 21 | Given an array of integers, return a new array such that each element at index i 22 | of the new array is the product of all the numbers in the original array except 23 | the one at i. 24 | 25 | For example, if our input was [1, 2, 3, 4, 5], the expected output would be 26 | [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be 27 | [2, 3, 6]. 28 | 29 | Follow-up: what if you can't use division? 30 | 31 | ## [Problem-3](src/main/java/in/ashwanik/dcp/problems/p1_30/p3):sunny: 32 | 33 | 34 | > This problem was asked by Google. 35 | 36 | Given the root to a binary tree, implement serialize(root), which serializes the 37 | tree into a string, and deserialize(s), which deserializes the string back into 38 | the tree. 39 | 40 | For example, given the following Node class 41 | 42 | class Node: 43 | def __init__(self, val, left=None, right=None): 44 | self.val = val 45 | self.left = left 46 | self.right = right 47 | 48 | 49 | The following test should pass: 50 | 51 | node = Node('root', Node('left', Node('left.left')), Node('right')) 52 | assert deserialize(serialize(node)).left.left.val == 'left.left' 53 | 54 | 55 | ## [Problem-4](src/main/java/in/ashwanik/dcp/problems/p1_30/p4):sunny: 56 | 57 | 58 | > This problem was asked by Stripe. 59 | 60 | Given an array of integers, find the first missing positive integer in linear 61 | time and constant space. In other words, find the lowest positive integer that 62 | does not exist in the array. The array can contain duplicates and negative 63 | numbers as well. 64 | 65 | For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should 66 | give 3. 67 | 68 | You can modify the input array in-place. 69 | 70 | ## Problem-5:new_moon: 71 | 72 | 73 | > This problem was asked by Jane Street. 74 | 75 | cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and 76 | last element of that pair. For example, car(cons(3, 4)) returns 3, and 77 | cdr(cons(3, 4)) returns 4. 78 | 79 | Given this implementation of cons: 80 | 81 | def cons(a, b): 82 | def pair(f): 83 | return f(a, b) 84 | return pair 85 | 86 | 87 | Implement car and cdr. 88 | 89 | ## Problem-6:new_moon: 90 | 91 | 92 | > This problem was asked by Google. 93 | 94 | An XOR linked list is a more memory efficient doubly linked list. Instead of 95 | each node holding next and prev fields, it holds a field named both, which is an 96 | XOR of the next node and the previous node. Implement an XOR linked list; it has 97 | an add(element) which adds the element to the end, and a get(index) which 98 | returns the node at index. 99 | 100 | If using a language that has no pointers (such as Python), you can assume you 101 | have access to get_pointer anddereference_pointer functions that converts 102 | between nodes and memory addresses. 103 | 104 | ## [Problem-7](src/main/java/in/ashwanik/dcp/problems/p1_30/p7):sunny: 105 | 106 | 107 | > This problem was asked by Facebook. 108 | 109 | Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the 110 | number of ways it can be decoded. 111 | 112 | For example, the message '111' would give 3, since it could be decoded as 'aaa', 113 | 'ka', and 'ak'. 114 | 115 | You can assume that the messages are decodable. For example, '001' is not 116 | allowed. 117 | 118 | ## [Problem-8](src/main/java/in/ashwanik/dcp/problems/p1_30/p8):sunny: 119 | 120 | 121 | > This problem was asked by Google. 122 | 123 | A unival tree (which stands for "universal value") is a tree where all nodes 124 | under it have the same value. 125 | 126 | Given the root to a binary tree, count the number of unival subtrees. 127 | 128 | For example, the following tree has 5 unival subtrees: 129 | 130 | 0 131 | / \ 132 | 1 0 133 | / \ 134 | 1 0 135 | / \ 136 | 1 1 137 | 138 | 139 | ## [Problem-9](src/main/java/in/ashwanik/dcp/problems/p1_30/p9):sunny: 140 | 141 | 142 | > This problem was asked by Airbnb. 143 | 144 | Given a list of integers, write a function that returns the largest sum of 145 | non-adjacent numbers. Numbers can be 0 or negative. 146 | 147 | For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 148 | 1, 5] should return 10, since we pick 5 and 5. 149 | 150 | Follow-up: Can you do this in O(N) time and constant space? 151 | 152 | ## Problem-10:new_moon: 153 | 154 | 155 | > This problem was asked by Apple. 156 | 157 | Implement a job scheduler which takes in a function f and an integer n, and 158 | calls f after n milliseconds. 159 | 160 | ## [Problem-11](src/main/java/in/ashwanik/dcp/problems/p1_30/p11):sunny: 161 | 162 | 163 | > This problem was asked by Twitter. 164 | 165 | Implement an autocomplete system. That is, given a query string s and a set of 166 | all possible query strings, return all strings in the set that have s as a 167 | prefix. 168 | 169 | For example, given the query string de and the set of strings [dog, deer, deal], 170 | return [deer, deal]. 171 | 172 | Hint: Try preprocessing the dictionary into a more efficient data structure to 173 | speed up queries. 174 | 175 | ## [Problem-12](src/main/java/in/ashwanik/dcp/problems/p1_30/p12):sunny: 176 | 177 | 178 | > This problem was asked by Amazon. 179 | 180 | There exists a staircase with N steps, and you can climb up either 1 or 2 steps 181 | at a time. Given N, write a function that returns the number of unique ways you 182 | can climb the staircase. The order of the steps matters. 183 | 184 | For example, if N is 4, then there are 5 unique ways: 185 | 186 | * 1, 1, 1, 1 187 | * 2, 1, 1 188 | * 1, 2, 1 189 | * 1, 1, 2 190 | * 2, 2 191 | 192 | What if, instead of being able to climb 1 or 2 steps at a time, you could climb 193 | any number from a set of positive integers X? For example, if X = {1, 3, 5}, you 194 | could climb 1, 3, or 5 steps at a time. 195 | 196 | ## [Problem-13](src/main/java/in/ashwanik/dcp/problems/p1_30/p13):sunny: 197 | 198 | 199 | > This problem was asked by Amazon. 200 | 201 | Given an integer k and a string s, find the length of the longest substring that 202 | contains at most k distinct characters. 203 | 204 | For example, given s = "abcba" and k = 2, the longest substring with k distinct 205 | characters is "bcb". 206 | 207 | ## [Problem-14](src/main/java/in/ashwanik/dcp/problems/p1_30/p14):sunny: 208 | 209 | 210 | > This problem was asked by Google. 211 | 212 | The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a 213 | Monte Carlo method. 214 | 215 | Hint: The basic equation of a circle is x2 + y2 = r2. 216 | 217 | ## [Problem-15](src/main/java/in/ashwanik/dcp/problems/p1_30/p15):sunny: 218 | 219 | 220 | > This problem was asked by Facebook. 221 | 222 | Given a stream of elements too large to store in memory, pick a random element 223 | from the stream with uniform probability. 224 | 225 | ## [Problem-16](src/main/java/in/ashwanik/dcp/problems/p1_30/p16):sunny: 226 | 227 | 228 | > This problem was asked by Twitter. 229 | 230 | You run an e-commerce website and want to record the last N order ids in a log. 231 | Implement a data structure to accomplish this, with the following API: 232 | 233 | * record(order_id): adds the order_id to the log 234 | * get_last(i): gets the ith last element from the log. i is guaranteed to be 235 | smaller than or equal to N. 236 | 237 | You should be as efficient with time and space as possible. 238 | 239 | ## [Problem-17](src/main/java/in/ashwanik/dcp/problems/p1_30/p17):sunny: 240 | 241 | 242 | > This problem was asked by Google. 243 | 244 | Suppose we represent our file system by a string in the following manner: 245 | 246 | The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: 247 | 248 | dir 249 | subdir1 250 | subdir2 251 | file.ext 252 | 253 | 254 | The directory dir contains an empty sub-directory subdir1 and a sub-directory 255 | subdir2 containing a file file.ext. 256 | 257 | The string 258 | "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" 259 | represents: 260 | 261 | dir 262 | subdir1 263 | file1.ext 264 | subsubdir1 265 | subdir2 266 | subsubdir2 267 | file2.ext 268 | 269 | 270 | The directory dir contains two sub-directories subdir1 and subdir2. subdir1 271 | contains a file file1.extand an empty second-level sub-directory subsubdir1. 272 | subdir2 contains a second-level sub-directorysubsubdir2 containing a file 273 | file2.ext. 274 | 275 | We are interested in finding the longest (number of characters) absolute path to 276 | a file within our file system. For example, in the second example above, the 277 | longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 278 | 32 (not including the double quotes). 279 | 280 | Given a string representing the file system in the above format, return the 281 | length of the longest absolute path to a file in the abstracted file system. If 282 | there is no file in the system, return 0. 283 | 284 | Note: 285 | 286 | The name of a file contains at least a period and an extension. 287 | 288 | The name of a directory or sub-directory will not contain a period. 289 | 290 | ## [Problem-18](src/main/java/in/ashwanik/dcp/problems/p1_30/p18):sunny: 291 | 292 | 293 | > This problem was asked by Google. 294 | 295 | Given an array of integers and a number k, where 1 <= k <= length of the array, 296 | compute the maximum values of each subarray of length k. 297 | 298 | For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 299 | 8, 8], since: 300 | 301 | * 10 = max(10, 5, 2) 302 | * 7 = max(5, 2, 7) 303 | * 8 = max(2, 7, 8) 304 | * 8 = max(7, 8, 7) 305 | 306 | Do this in O(n) time and O(k) space. You can modify the input array in-place and 307 | you do not need to store the results. You can simply print them out as you 308 | compute them. 309 | 310 | ## Problem-19:waxing_crescent_moon: 311 | 312 | 313 | > This problem was asked by Facebook. 314 | 315 | A builder is looking to build a row of N houses that can be of K different 316 | colors. He has a goal of minimizing cost while ensuring that no two neighboring 317 | houses are of the same color. 318 | 319 | Given an N by K matrix where the nth row and kth column represents the cost to 320 | build the nthhouse with kth color, return the minimum cost which achieves this 321 | goal. 322 | 323 | ## [Problem-20](src/main/java/in/ashwanik/dcp/problems/p1_30/p20):sunny: 324 | 325 | 326 | > This problem was asked by Google. 327 | 328 | Given two singly linked lists that intersect at some point, find the 329 | intersecting node. The lists are non-cyclical. 330 | 331 | For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the 332 | node with value 8. 333 | 334 | In this example, assume nodes with the same value are the exact same node 335 | objects. 336 | 337 | Do this in O(M + N) time (where M and N are the lengths of the lists) and 338 | constant space. 339 | 340 | ## [Problem-21](src/main/java/in/ashwanik/dcp/problems/p1_30/p21):sunny: 341 | 342 | 343 | > This problem was asked by Snapchat. 344 | 345 | Given an array of time intervals (start, end) for classroom lectures (possibly 346 | overlapping), find the minimum number of rooms required. 347 | 348 | For example, given [(30, 75), (0, 50), (60, 150)], you should return 2. 349 | 350 | ## [Problem-22](src/main/java/in/ashwanik/dcp/problems/p1_30/p22):sunny: 351 | 352 | 353 | > This problem was asked by Microsoft. 354 | 355 | Given a dictionary of words and a string made up of those words (no spaces), 356 | return the original sentence in a list. If there is more than one possible 357 | reconstruction, return any of them. If there is no possible reconstruction, then 358 | return null. 359 | 360 | For example, given the set of words 'quick', 'brown', 'the', 'fox', and the 361 | string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox']. 362 | 363 | Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string 364 | "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 365 | 'and', 'beyond']. 366 | 367 | ## [Problem-23](src/main/java/in/ashwanik/dcp/problems/p1_30/p23):sunny: 368 | 369 | 370 | > This problem was asked by Google. 371 | 372 | You are given an M by N matrix consisting of booleans that represents a board. 373 | Each True boolean represents a wall. Each False boolean represents a tile you 374 | can walk on. 375 | 376 | Given this matrix, a start coordinate, and an end coordinate, return the minimum 377 | number of steps required to reach the end coordinate from the start. If there is 378 | no possible path, then return null. You can move up, left, down, and right. You 379 | cannot move through walls. You cannot wrap around the edges of the board. 380 | 381 | For example, given the following board: 382 | 383 | [[f, f, f, f], 384 | [t, t, f, t], 385 | [f, f, f, f], 386 | [f, f, f, f]] 387 | 388 | 389 | and start = (3, 0) (bottom left) and end = (0, 0) (top left), the minimum number 390 | of steps required to reach the end is 7, since we would need to go through (1, 391 | 2) because there is a wall everywhere else on the second row. 392 | 393 | ## [Problem-24](src/main/java/in/ashwanik/dcp/problems/p1_30/p24):sunny: 394 | 395 | 396 | > This problem was asked by Google. 397 | 398 | Implement locking in a binary tree. A binary tree node can be locked or unlocked 399 | only if all of its descendants or ancestors are not locked. 400 | 401 | Design a binary tree node class with the following methods: 402 | 403 | * is_locked, which returns whether the node is locked 404 | * lock, which attempts to lock the node. If it cannot be locked, then it should 405 | return false. Otherwise, it should lock it and return true. 406 | * unlock, which unlocks the node. If it cannot be unlocked, then it should 407 | return false. Otherwise, it should unlock it and return true. 408 | 409 | You may augment the node to add parent pointers or any other property you would 410 | like. You may assume the class is used in a single-threaded program, so there is 411 | no need for actual locks or mutexes. Each method should run in O(h), where h is 412 | the height of the tree. 413 | 414 | ## Problem-25:waxing_crescent_moon: 415 | 416 | 417 | > This problem was asked by Facebook. 418 | 419 | Implement regular expression matching with the following special characters: 420 | 421 | * . (period) which matches any single character 422 | * * (asterisk) which matches zero or more of the preceding element 423 | 424 | That is, implement a function that takes in a string and a valid regular 425 | expression and returns whether or not the string matches the regular expression. 426 | 427 | For example, given the regular expression "ra." and the string "ray", your 428 | function should return true. The same regular expression on the string "raymond" 429 | should return false. 430 | 431 | Given the regular expression ".*at" and the string "chat", your function should 432 | return true. The same regular expression on the string "chats" should return 433 | false. 434 | 435 | ## [Problem-26](src/main/java/in/ashwanik/dcp/problems/p1_30/p26):sunny: 436 | 437 | 438 | > This problem was asked by Google. 439 | 440 | Given a singly linked list and an integer k, remove the kth last element from 441 | the list. k is guaranteed to be smaller than the length of the list. 442 | 443 | The list is very long, so making more than one pass is prohibitively expensive. 444 | 445 | Do this in constant space and in one pass. 446 | 447 | ## [Problem-27](src/main/java/in/ashwanik/dcp/problems/p1_30/p27):sunny: 448 | 449 | 450 | > This problem was asked by Facebook. 451 | 452 | Given a string of round, curly, and square open and closing brackets, return 453 | whether the brackets are balanced (well-formed). 454 | 455 | For example, given the string "([])[]({})", you should return true. 456 | 457 | Given the string "([)]" or "((()", you should return false. 458 | 459 | ## [Problem-28](src/main/java/in/ashwanik/dcp/problems/p1_30/p28):sunny: 460 | 461 | 462 | > This problem was asked by Palantir. 463 | 464 | Write an algorithm to justify text. Given a sequence of words and an integer 465 | line length k, return a list of strings which represents each line, fully 466 | justified. 467 | 468 | More specifically, you should have as many words as possible in each line. There 469 | should be at least one space between each word. Pad extra spaces when necessary 470 | so that each line has exactly length k. Spaces should be distributed as equally 471 | as possible, with the extra spaces, if any, distributed starting from the left. 472 | 473 | If you can only fit one word on a line, then you should pad the right-hand side 474 | with spaces. 475 | 476 | Each word is guaranteed not to be longer than k. 477 | 478 | For example, given the list of words ["the", "quick", "brown", "fox", "jumps", 479 | "over", "the", "lazy", "dog"] and k = 16, you should return the following: 480 | 481 | ["the quick brown", # 1 extra space on the left 482 | "fox jumps over", # 2 extra spaces distributed evenly 483 | "the lazy dog"] # 4 extra spaces distributed evenly 484 | 485 | 486 | ## [Problem-29](src/main/java/in/ashwanik/dcp/problems/p1_30/p29):sunny: 487 | 488 | 489 | > This problem was asked by Amazon. 490 | 491 | Run-length encoding is a fast and simple method of encoding strings. The basic 492 | idea is to represent repeated successive characters as a single count and 493 | character. For example, the string "AAAABBBCCDAA" would be encoded as 494 | "4A3B2C1D2A". 495 | 496 | Implement run-length encoding and decoding. You can assume the string to be 497 | encoded have no digits and consists solely of alphabetic characters. You can 498 | assume the string to be decoded is valid. 499 | 500 | ## [Problem-30](src/main/java/in/ashwanik/dcp/problems/p1_30/p30):sunny: 501 | 502 | 503 | > This problem was asked by Facebook. 504 | 505 | You are given an array of non-negative integers that represents a 506 | two-dimensional elevation map where each element is unit-width wall and the 507 | integer is the height. Suppose it will rain and all spots between two walls get 508 | filled up. 509 | 510 | Compute how many units of water remain trapped on the map in O(N) time and O(1) 511 | space. 512 | 513 | For example, given the input [2, 1, 2], we can hold 1 unit of water in the 514 | middle. 515 | 516 | Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in 517 | the second, and 3 in the fourth index (we cannot hold 5 since it would run off 518 | to the left), so we can trap 8 units of water. 519 | 520 | ## [Problem-31](src/main/java/in/ashwanik/dcp/problems/p31_60/p31):sunny: 521 | 522 | 523 | > This problem was asked by Google. 524 | 525 | The edit distance between two strings refers to the minimum number of character 526 | insertions, deletions, and substitutions required to change one string to the 527 | other. For example, the edit distance between “kitten” and “sitting” is three: 528 | substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”. 529 | 530 | Given two strings, compute the edit distance between them. 531 | 532 | ## Problem-32:new_moon: 533 | 534 | 535 | > This problem was asked by Jane Street. 536 | 537 | Suppose you are given a table of currency exchange rates, represented as a 2D 538 | array. Determine whether there is a possible arbitrage: that is, whether there 539 | is some sequence of trades you can make, starting with some amount A of any 540 | currency, so that you can end up with some amount greater than A of that 541 | currency. 542 | 543 | There are no transaction costs and you can trade fractional quantities. 544 | 545 | ## [Problem-33](src/main/java/in/ashwanik/dcp/problems/p31_60/p33):sunny: 546 | 547 | 548 | > This problem was asked by Microsoft. 549 | 550 | Compute the running median of a sequence of numbers. That is, given a stream of 551 | numbers, print out the median of the list so far on each new element. 552 | 553 | Recall that the median of an even-numbered list is the average of the two middle 554 | numbers. 555 | 556 | For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should 557 | print out: 558 | 559 | 2 560 | 1.5 561 | 2 562 | 3.5 563 | 2 564 | 2 565 | 2 566 | 567 | 568 | ## Problem-34:waxing_crescent_moon: 569 | 570 | 571 | > This problem was asked by Quora. 572 | 573 | Given a string, find the palindrome that can be made by inserting the fewest 574 | number of characters as possible anywhere in the word. If there is more than one 575 | palindrome of minimum length that can be made, return the lexicographically 576 | earliest one (the first one alphabetically). 577 | 578 | For example, given the string "race", you should return "ecarace", since we can 579 | add three letters to it (which is the smallest amount to make a palindrome). 580 | There are seven other palindromes that can be made from "race" by adding three 581 | letters, but "ecarace" comes first alphabetically. 582 | 583 | As another example, given the string "google", you should return "elgoogle". 584 | 585 | ## [Problem-35](src/main/java/in/ashwanik/dcp/problems/p31_60/p35):sunny: 586 | 587 | 588 | > This problem was asked by Google. 589 | 590 | Given an array of strictly the characters 'R', 'G', and 'B', segregate the 591 | values of the array so that all the Rs come first, the Gs come second, and the 592 | Bs come last. You can only swap elements of the array. 593 | 594 | Do this in linear time and in-place. 595 | 596 | For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should 597 | become ['R', 'R', 'R', 'G', 'G', 'B', 'B']. 598 | 599 | ## [Problem-36](src/main/java/in/ashwanik/dcp/problems/p31_60/p36):sunny: 600 | 601 | 602 | > This problem was asked by Dropbox. 603 | 604 | Given the root to a binary search tree, find the second largest node in the 605 | tree. 606 | 607 | ## [Problem-37](src/main/java/in/ashwanik/dcp/problems/p31_60/p37):sunny: 608 | 609 | 610 | > This problem was asked by Google. 611 | 612 | The power set of a set is the set of all its subsets. Write a function that, 613 | given a set, generates its power set. 614 | 615 | For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 616 | 2}, {1, 3}, {2, 3}, {1, 2, 3}}. 617 | 618 | You may also use a list or array to represent a set. 619 | 620 | ## [Problem-38](src/main/java/in/ashwanik/dcp/problems/p31_60/p38):sunny: 621 | 622 | 623 | > This problem was asked by Microsoft. 624 | 625 | You have an N by N board. Write a function that, given N, returns the number of 626 | possible arrangements of the board where N queens can be placed on the board 627 | without threatening each other, i.e. no two queens share the same row, column, 628 | or diagonal. 629 | 630 | ## [Problem-39](src/main/java/in/ashwanik/dcp/problems/p31_60/p39):sunny: 631 | 632 | 633 | > This problem was asked by Dropbox. 634 | 635 | Conway's Game of Life takes place on an infinite two-dimensional board of square 636 | cells. Each cell is either dead or alive, and at each tick, the following rules 637 | apply: 638 | 639 | * Any live cell with less than two live neighbours dies. 640 | * Any live cell with two or three live neighbours remains living. 641 | * Any live cell with more than three live neighbours dies. 642 | * Any dead cell with exactly three live neighbours becomes a live cell. 643 | 644 | A cell neighbours another cell if it is horizontally, vertically, or diagonally 645 | adjacent. 646 | 647 | Implement Conway's Game of Life. It should be able to be initialized with a 648 | starting list of live cell coordinates and the number of steps it should run 649 | for. Once initialized, it should print out the board state at each step. Since 650 | it's an infinite board, print out only the relevant coordinates, i.e. from the 651 | top-leftmost live cell to bottom-rightmost live cell. 652 | 653 | You can represent a live cell with an asterisk (*) and a dead cell with a dot (. 654 | ). 655 | 656 | ## Problem-40:new_moon: 657 | 658 | 659 | > This problem was asked by Google. 660 | 661 | Given an array of integers where every integer occurs three times except for one 662 | integer, which only occurs once, find and return the non-duplicated integer. 663 | 664 | For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], 665 | return 19. 666 | 667 | Do this in O(N) time and O(1) space. 668 | 669 | ## [Problem-41](src/main/java/in/ashwanik/dcp/problems/p31_60/p41):sunny: 670 | 671 | 672 | > This problem was asked by Facebook. 673 | 674 | Given an unordered list of flights taken by someone, each represented as 675 | (origin, destination) pairs, and a starting airport, compute the person's 676 | itinerary. If no such itinerary exists, return null. If there are multiple 677 | possible itineraries, return the lexicographically smallest one. All flights 678 | must be used in the itinerary. 679 | 680 | For example, given the list of flights [('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 681 | 'YYZ'), ('HKO', 'ORD')] and starting airport 'YUL', you should return the list 682 | ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD']. 683 | 684 | Given the list of flights [('SFO', 'COM'), ('COM', 'YYZ')] and starting airport 685 | 'COM', you should return null. 686 | 687 | Given the list of flights [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')] and 688 | starting airport 'A', you should return the list ['A', 'B', 'C', 'A', 'C'] even 689 | though ['A', 'C', 'A', 'B', 'C'] is also a valid itinerary. However, the first 690 | one is lexicographically smaller. 691 | 692 | ## [Problem-42](src/main/java/in/ashwanik/dcp/problems/p31_60/p42):sunny: 693 | 694 | 695 | > This problem was asked by Google. 696 | 697 | Given a list of integers S and a target number k, write a function that returns 698 | a subset of S that adds up to k. If such a subset cannot be made, then return 699 | null. 700 | 701 | Integers can appear more than once in the list. You may assume all numbers in 702 | the list are positive. 703 | 704 | For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] 705 | since it sums up to 24. 706 | 707 | ## [Problem-43](src/main/java/in/ashwanik/dcp/problems/p31_60/p43):sunny: 708 | 709 | 710 | > This problem was asked by Amazon. 711 | 712 | Implement a stack that has the following methods: 713 | 714 | * push(val), which pushes an element onto the stack 715 | * pop(), which pops off and returns the topmost element of the stack. If there 716 | are no elements in the stack, then it should throw an error or return null. 717 | * max(), which returns the maximum value in the stack currently. If there are 718 | no elements in the stack, then it should throw an error or return null. 719 | 720 | Each method should run in constant time. 721 | 722 | ## [Problem-44](src/main/java/in/ashwanik/dcp/problems/p31_60/p44):sunny: 723 | 724 | 725 | > This problem was asked by Google. 726 | 727 | We can determine how "out of order" an array A is by counting the number of 728 | inversions it has. Two elements A[i] and A[j] form an inversion if A[i] > A[j] 729 | but i < j. That is, a smaller element appears after a larger element. 730 | 731 | Given an array, count the number of inversions it has. Do this faster than 732 | O(N^2) time. 733 | 734 | You may assume each element in the array is distinct. 735 | 736 | For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5] has 737 | three inversions: (2, 1), (4, 1), and (4, 3). The array [5, 4, 3, 2, 1] has ten 738 | inversions: every distinct pair forms an inversion. 739 | 740 | ## [Problem-45](src/main/java/in/ashwanik/dcp/problems/p31_60/p45):sunny: 741 | 742 | 743 | > This problem was asked by Two Sigma. 744 | 745 | Using a function rand5() that returns an integer from 1 to 5 (inclusive) with 746 | uniform probability, implement a function rand7() that returns an integer from 1 747 | to 7 (inclusive). 748 | 749 | ## [Problem-46](src/main/java/in/ashwanik/dcp/problems/p31_60/p46):sunny: 750 | 751 | 752 | > This problem was asked by Amazon. 753 | 754 | Given a string, find the longest palindromic contiguous substring. If there are 755 | more than one with the maximum length, return any one. 756 | 757 | For example, the longest palindromic substring of "aabcdcb" is "bcdcb". The 758 | longest palindromic substring of "bananas" is "anana". 759 | 760 | ## [Problem-47](src/main/java/in/ashwanik/dcp/problems/p31_60/p47):sunny: 761 | 762 | 763 | > This problem was asked by Facebook. 764 | 765 | Given a array of numbers representing the stock prices of a company in 766 | chronological order, write a function that calculates the maximum profit you 767 | could have made from buying and selling that stock once. You must buy before you 768 | can sell it. 769 | 770 | For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could 771 | buy the stock at 5 dollars and sell it at 10 dollars. 772 | 773 | ## [Problem-48](src/main/java/in/ashwanik/dcp/problems/p31_60/p48):sunny: 774 | 775 | 776 | > This problem was asked by Google. 777 | 778 | Given pre-order and in-order traversals of a binary tree, write a function to 779 | reconstruct the tree. 780 | 781 | For example, given the following preorder traversal: 782 | 783 | [a, b, d, e, c, f, g] 784 | 785 | And the following inorder traversal: 786 | 787 | [d, b, e, a, f, c, g] 788 | 789 | You should return the following tree: 790 | 791 | a 792 | / \ 793 | b c 794 | / \ / \ 795 | d e f g 796 | 797 | 798 | ## [Problem-49](src/main/java/in/ashwanik/dcp/problems/p31_60/p49):sunny: 799 | 800 | 801 | > This problem was asked by Amazon. 802 | 803 | Given an array of numbers, find the maximum sum of any contiguous subarray of 804 | the array. 805 | 806 | For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 807 | 137, since we would take elements 42, 14, -5, and 86. 808 | 809 | Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not 810 | take any elements. 811 | 812 | Do this in O(N) time. 813 | 814 | ## [Problem-50](src/main/java/in/ashwanik/dcp/problems/p31_60/p50):sunny: 815 | 816 | 817 | > This problem was asked by Microsoft. 818 | 819 | Suppose an arithmetic expression is given as a binary tree. Each leaf is an 820 | integer and each internal node is one of '+', '−', '∗', or '/'. 821 | 822 | Given the root to such a tree, write a function to evaluate it. 823 | 824 | For example, given the following tree: 825 | 826 | * 827 | / \ 828 | + + 829 | / \ / \ 830 | 3 2 4 5 831 | 832 | 833 | You should return 45, as it is (3 + 2) * (4 + 5). 834 | 835 | ## [Problem-51](src/main/java/in/ashwanik/dcp/problems/p31_60/p51):sunny: 836 | 837 | 838 | > This problem was asked by Facebook. 839 | 840 | Given a function that generates perfectly random numbers between 1 and k 841 | (inclusive), where k is an input, write a function that shuffles a deck of cards 842 | represented as an array using only swaps. 843 | 844 | It should run in O(N) time. 845 | 846 | Hint: Make sure each one of the 52! permutations of the deck is equally likely. 847 | 848 | ## [Problem-52](src/main/java/in/ashwanik/dcp/problems/p31_60/p52):sunny: 849 | 850 | 851 | > This problem was asked by Google. 852 | 853 | Implement an LRU (Least Recently Used) cache. It should be able to be 854 | initialized with a cache size n, and contain the following methods: 855 | 856 | * set(key, value): sets key to value. If there are already n items in the cache 857 | and we are adding a new item, then it should also remove the least recently 858 | used item. 859 | * get(key): gets the value at key. If no such key exists, return null. 860 | 861 | Each operation should run in O(1) time. 862 | 863 | ## [Problem-53](src/main/java/in/ashwanik/dcp/problems/p31_60/p53):sunny: 864 | 865 | 866 | > This problem was asked by Apple. 867 | 868 | Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, 869 | first-out) data structure with the following methods: enqueue, which inserts an 870 | element into the queue, and dequeue, which removes it. 871 | 872 | ## Problem-54:waxing_crescent_moon: 873 | 874 | 875 | > This problem was asked by Dropbox. 876 | 877 | Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with 878 | digits. The objective is to fill the grid with the constraint that every row, 879 | column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9. 880 | 881 | Implement an efficient sudoku solver. 882 | 883 | ## [Problem-55](src/main/java/in/ashwanik/dcp/problems/p31_60/p55):sunny: 884 | 885 | 886 | > This problem was asked by Microsoft. 887 | 888 | Implement a URL shortener with the following methods: 889 | 890 | * shorten(url), which shortens the url into a six-character alphanumeric 891 | string, such as zLg6wl. 892 | * restore(short), which expands the shortened string into the original url. If 893 | no such shortened string exists, return null. 894 | 895 | Hint: What if we enter the same URL twice? 896 | 897 | ## Problem-56:waxing_crescent_moon: 898 | 899 | 900 | > This problem was asked by Google. 901 | 902 | Given an undirected graph represented as an adjacency matrix and an integer k, 903 | write a function to determine whether each vertex in the graph can be colored 904 | such that no two adjacent vertices share the same color using at most k colors. 905 | 906 | ## [Problem-57](src/main/java/in/ashwanik/dcp/problems/p31_60/p57):sunny: 907 | 908 | 909 | > This problem was asked by Amazon. 910 | 911 | Given a string s and an integer k, break up the string into multiple texts such 912 | that each text has a length of k or less. You must break it up so that words 913 | don't break across lines. If there's no way to break the text up, then return 914 | null. 915 | 916 | You can assume that there are no spaces at the ends of the string and that there 917 | is exactly one space between each word. 918 | 919 | For example, given the string "the quick brown fox jumps over the lazy dog" and 920 | k = 10, you should return: ["the quick", "brown fox", "jumps over", "the lazy", 921 | "dog"]. No string in the list has a length of more than 10. 922 | 923 | ## [Problem-58](src/main/java/in/ashwanik/dcp/problems/p31_60/p58):sunny: 924 | 925 | 926 | > This problem was asked by Amazon. 927 | 928 | An sorted array of integers was rotated an unknown number of times. 929 | 930 | Given such an array, find the index of the element in the array in faster than 931 | linear time. If the element doesn't exist in the array, return null. 932 | 933 | For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 934 | (the index of 8 in the array). 935 | 936 | You can assume all the integers in the array are unique. 937 | 938 | ## Problem-59:new_moon: 939 | 940 | 941 | > This problem was asked by Google. 942 | 943 | Implement a file syncing algorithm for two computers over a low-bandwidth 944 | network. What if we know the files in the two computers are mostly the same? 945 | 946 | ## [Problem-60](src/main/java/in/ashwanik/dcp/problems/p31_60/p60):sunny: 947 | 948 | 949 | > This problem was asked by Facebook. 950 | 951 | Given a multiset of integers, return whether it can be partitioned into two 952 | subsets whose sums are the same. 953 | 954 | For example, given the multiset {15, 5, 20, 10, 35, 15, 10}, it would return 955 | true, since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both 956 | add up to 55. 957 | 958 | Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't 959 | split it up into two subsets that add up to the same sum. 960 | 961 | ## [Problem-61](src/main/java/in/ashwanik/dcp/problems/p61_90/p61):sunny: 962 | 963 | 964 | > This problem was asked by Google. 965 | 966 | Implement integer exponentiation. That is, implement the pow(x, y) function, 967 | where x and y are integers and returns x^y. 968 | 969 | Do this faster than the naive method of repeated multiplication. 970 | 971 | For example, pow(2, 10) should return 1024. 972 | 973 | ## Problem-62:waxing_crescent_moon: 974 | 975 | 976 | > This problem was asked by Facebook. 977 | 978 | There is an N by M matrix of zeroes. Given N and M, write a function to count 979 | the number of ways of starting at the top-left corner and getting to the 980 | bottom-right corner. You can only move right or down. 981 | 982 | For example, given a 2 by 2 matrix, you should return 2, since there are two 983 | ways to get to the bottom-right: 984 | 985 | * Right, then down 986 | * Down, then right 987 | 988 | Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right. 989 | 990 | ## Problem-63:waxing_crescent_moon: 991 | 992 | 993 | > This problem was asked by Microsoft. 994 | 995 | Given a 2D matrix of characters and a target word, write a function that returns 996 | whether the word can be found in the matrix by going left-to-right, or 997 | up-to-down. 998 | 999 | For example, given the following matrix: 1000 | 1001 | [['F', 'A', 'C', 'I'], 1002 | ['O', 'B', 'Q', 'P'], 1003 | ['A', 'N', 'O', 'B'], 1004 | ['M', 'A', 'S', 'S']] 1005 | 1006 | 1007 | and the target word 'FOAM', you should return true, since it's the leftmost 1008 | column. Similarly, given the target word 'MASS', you should return true, since 1009 | it's the last row. 1010 | 1011 | ## Problem-64:waxing_crescent_moon: 1012 | 1013 | 1014 | > This problem was asked by Google. 1015 | 1016 | A knight's tour is a sequence of moves by a knight on a chessboard such that all 1017 | squares are visited once. 1018 | 1019 | Given N, write a function to return the number of knight's tours on an N by N 1020 | chessboard. 1021 | 1022 | ## [Problem-65](src/main/java/in/ashwanik/dcp/problems/p61_90/p65):sunny: 1023 | 1024 | 1025 | > This problem was asked by Amazon. 1026 | 1027 | Given a N by M matrix of numbers, print out the matrix in a clockwise spiral. 1028 | 1029 | For example, given the following matrix: 1030 | 1031 | [[1, 2, 3, 4, 5], 1032 | [6, 7, 8, 9, 10], 1033 | [11, 12, 13, 14, 15], 1034 | [16, 17, 18, 19, 20]] 1035 | 1036 | 1037 | You should print out the following: 1038 | 1039 | 1 1040 | 2 1041 | 3 1042 | 4 1043 | 5 1044 | 10 1045 | 15 1046 | 20 1047 | 19 1048 | 18 1049 | 17 1050 | 16 1051 | 11 1052 | 6 1053 | 7 1054 | 8 1055 | 9 1056 | 14 1057 | 13 1058 | 12 1059 | 1060 | 1061 | ## [Problem-66](src/main/java/in/ashwanik/dcp/problems/p61_90/p66):sunny: 1062 | 1063 | 1064 | > This problem was asked by Square. 1065 | 1066 | Assume you have access to a function toss_biased() which returns 0 or 1 with a 1067 | probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the 1068 | bias of the coin. 1069 | 1070 | Write a function to simulate an unbiased coin toss. 1071 | 1072 | ## [Problem-67](src/main/java/in/ashwanik/dcp/problems/p61_90/p67):sunny: 1073 | 1074 | 1075 | > This problem was asked by Google. 1076 | 1077 | Implement an LFU (Least Frequently Used) cache. It should be able to be 1078 | initialized with a cache size n, and contain the following methods: 1079 | 1080 | * set(key, value): sets key to value. If there are already n items in the cache 1081 | and we are adding a new item, then it should also remove the least frequently 1082 | used item. If there is a tie, then the least recently used key should be 1083 | removed. 1084 | * get(key): gets the value at key. If no such key exists, return null. 1085 | 1086 | Each operation should run in O(1) time. 1087 | 1088 | ## Problem-68:waxing_crescent_moon: 1089 | 1090 | 1091 | > This problem was asked by Google. 1092 | 1093 | On our special chessboard, two bishops attack each other if they share the same 1094 | diagonal. This includes bishops that have another bishop located between them, 1095 | i.e. bishops can attack through pieces. 1096 | 1097 | You are given N bishops, represented as (row, column) tuples on a M by M 1098 | chessboard. Write a function to count the number of pairs of bishops that attack 1099 | each other. The ordering of the pair doesn't matter: (1, 2) is considered the 1100 | same as (2, 1). 1101 | 1102 | For example, given M = 5 and the list of bishops: 1103 | 1104 | * (0, 0) 1105 | * (1, 2) 1106 | * (2, 2) 1107 | * (4, 0) 1108 | 1109 | The board would look like this: 1110 | 1111 | [b 0 0 0 0] 1112 | [0 0 b 0 0] 1113 | [0 0 b 0 0] 1114 | [0 0 0 0 0] 1115 | [b 0 0 0 0] 1116 | 1117 | 1118 | You should return 2, since bishops 1 and 3 attack each other, as well as bishops 1119 | 3 and 4. 1120 | 1121 | ## [Problem-69](src/main/java/in/ashwanik/dcp/problems/p61_90/p69):sunny: 1122 | 1123 | 1124 | > This problem was asked by Facebook. 1125 | 1126 | Given a list of integers, return the largest product that can be made by 1127 | multiplying any three integers. 1128 | 1129 | For example, if the list is [-10, -10, 5, 2], we should return 500, since that's 1130 | -10 * -10 * 5. 1131 | 1132 | You can assume the list has at least three integers. 1133 | 1134 | ## [Problem-70](src/main/java/in/ashwanik/dcp/problems/p61_90/p70):sunny: 1135 | 1136 | 1137 | > This problem was asked by Microsoft. 1138 | 1139 | A number is considered perfect if its digits sum up to exactly 10. 1140 | 1141 | Given a positive integer n, return the n-th perfect number. 1142 | 1143 | For example, given 1, you should return 19. Given 2, you should return 28. 1144 | 1145 | ## Problem-71:new_moon: 1146 | 1147 | 1148 | > This problem was asked by Two Sigma. 1149 | 1150 | Using a function rand7() that returns an integer from 1 to 7 (inclusive) with 1151 | uniform probability, implement a function rand5() that returns an integer from 1 1152 | to 5 (inclusive). 1153 | 1154 | ## Problem-72:waxing_crescent_moon: 1155 | 1156 | 1157 | > This problem was asked by Google. 1158 | 1159 | In a directed graph, each node is assigned an uppercase letter. We define a 1160 | path's value as the number of most frequently-occurring letter along that path. 1161 | For example, if a path in the graph goes through "ABACA", the value of the path 1162 | is 3, since there are 3 occurrences of 'A' on the path. 1163 | 1164 | Given a graph with n nodes and m directed edges, return the largest value path 1165 | of the graph. If the largest value is infinite, then return null. 1166 | 1167 | The graph is represented with a string and an edge list. The i-th character 1168 | represents the uppercase letter of the i-th node. Each tuple in the edge list 1169 | (i, j) means there is a directed edge from the i-th node to the j-th node. 1170 | Self-edges are possible, as well as multi-edges. 1171 | 1172 | For example, the following input graph: 1173 | 1174 | ABACA 1175 | 1176 | 1177 | [(0, 1), 1178 | (0, 2), 1179 | (2, 3), 1180 | (3, 4)] 1181 | 1182 | 1183 | Would have maximum value 3 using the path of vertices [0, 2, 3, 4], (A, A, C, A) 1184 | . 1185 | 1186 | The following input graph: 1187 | 1188 | A 1189 | 1190 | 1191 | [(0, 0)] 1192 | 1193 | 1194 | Should return null, since we have an infinite loop. 1195 | 1196 | ## [Problem-73](src/main/java/in/ashwanik/dcp/problems/p61_90/p73):sunny: 1197 | 1198 | 1199 | > This problem was asked by Google. 1200 | 1201 | Given the head of a singly linked list, reverse it in-place. 1202 | 1203 | ## [Problem-74](src/main/java/in/ashwanik/dcp/problems/p61_90/p74):sunny: 1204 | 1205 | 1206 | > This problem was asked by Apple. 1207 | 1208 | Suppose you have a multiplication table that is N by N. That is, a 2D array 1209 | where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 1210 | 0-indexed) or i * j (if 1-indexed). 1211 | 1212 | Given integers N and X, write a function that returns the number of times X 1213 | appears as a value in an N by N multiplication table. 1214 | 1215 | For example, given N = 6 and X = 12, you should return 4, since the 1216 | multiplication table looks like this: 1217 | 1218 | | 1 | 2 | 3 | 4 | 5 | 6 | 1219 | 1220 | | 2 | 4 | 6 | 8 | 10 | 12 | 1221 | 1222 | | 3 | 6 | 9 | 12 | 15 | 18 | 1223 | 1224 | | 4 | 8 | 12 | 16 | 20 | 24 | 1225 | 1226 | | 5 | 10 | 15 | 20 | 25 | 30 | 1227 | 1228 | | 6 | 12 | 18 | 24 | 30 | 36 | 1229 | 1230 | And there are 4 12's in the table. 1231 | 1232 | ## Problem-75:waxing_crescent_moon: 1233 | 1234 | 1235 | > This problem was asked by Microsoft. 1236 | 1237 | Given an array of numbers, find the length of the longest increasing subsequence 1238 | in the array. The subsequence does not necessarily have to be contiguous. 1239 | 1240 | For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 1241 | 15], the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15. 1242 | 1243 | ## Problem-76:waxing_crescent_moon: 1244 | 1245 | 1246 | > This problem was asked by Google. 1247 | 1248 | You are given an N by M 2D matrix of lowercase letters. Determine the minimum 1249 | number of columns that can be removed to ensure that each row is ordered from 1250 | top to bottom lexicographically. That is, the letter at each column is 1251 | lexicographically later as you go down each row. It does not matter whether each 1252 | row itself is ordered lexicographically. 1253 | 1254 | For example, given the following table: 1255 | 1256 | cba 1257 | daf 1258 | ghi 1259 | 1260 | 1261 | This is not ordered because of the a in the center. We can remove the second 1262 | column to make it ordered: 1263 | 1264 | ca 1265 | df 1266 | gi 1267 | 1268 | 1269 | So your function should return 1, since we only needed to remove 1 column. 1270 | 1271 | As another example, given the following table: 1272 | 1273 | abcdef 1274 | 1275 | 1276 | Your function should return 0, since the rows are already ordered (there's only 1277 | one row). 1278 | 1279 | As another example, given the following table: 1280 | 1281 | zyx 1282 | wvu 1283 | tsr 1284 | 1285 | 1286 | Your function should return 3, since we would need to remove all the columns to 1287 | order it. 1288 | 1289 | ## [Problem-77](src/main/java/in/ashwanik/dcp/problems/p61_90/p77):sunny: 1290 | 1291 | 1292 | > This problem was asked by Snapchat. 1293 | 1294 | Given a list of possibly overlapping intervals, return a new list of intervals 1295 | where all overlapping intervals have been merged. 1296 | 1297 | The input list is not necessarily ordered in any way. 1298 | 1299 | For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 1300 | 3), (4, 10), (20, 25)]. 1301 | 1302 | ## [Problem-78](src/main/java/in/ashwanik/dcp/problems/p61_90/p78):sunny: 1303 | 1304 | 1305 | > This problem was asked recently by Google. 1306 | 1307 | Given k sorted singly linked lists, write a function to merge all the lists into 1308 | one sorted singly linked list. 1309 | 1310 | ## [Problem-79](src/main/java/in/ashwanik/dcp/problems/p61_90/p79):sunny: 1311 | 1312 | 1313 | > This problem was asked by Facebook. 1314 | 1315 | Given an array of integers, write a function to determine whether the array 1316 | could become non-decreasing by modifying at most 1 element. 1317 | 1318 | For example, given the array [10, 5, 7], you should return true, since we can 1319 | modify the 10 into a 1 to make the array non-decreasing. 1320 | 1321 | Given the array [10, 5, 1], you should return false, since we can't modify any 1322 | one element to get a non-decreasing array. 1323 | 1324 | ## [Problem-80](src/main/java/in/ashwanik/dcp/problems/p61_90/p80):sunny: 1325 | 1326 | 1327 | > This problem was asked by Google. 1328 | 1329 | Given the root of a binary tree, return a deepest node. For example, in the 1330 | following tree, return d. 1331 | 1332 | a 1333 | / \ 1334 | b c 1335 | / 1336 | d 1337 | 1338 | 1339 | ## [Problem-81](src/main/java/in/ashwanik/dcp/problems/p61_90/p81):sunny: 1340 | 1341 | 1342 | > This problem was asked by Yelp. 1343 | 1344 | Given a mapping of digits to letters (as in a phone number), and a digit string, 1345 | return all possible letters the number could represent. You can assume each 1346 | valid number in the mapping is a single digit. 1347 | 1348 | For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should 1349 | return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"]. 1350 | 1351 | ## Problem-82:new_moon: 1352 | 1353 | 1354 | > This problem was asked Microsoft. 1355 | 1356 | Using a read7() method that returns 7 characters from a file, implement readN(n) 1357 | which reads n characters. 1358 | 1359 | For example, given a file with the content “Hello world”, three read7() returns 1360 | “Hello w”, “orld” and then “”. 1361 | 1362 | ## [Problem-83](src/main/java/in/ashwanik/dcp/problems/p61_90/p83):sunny: 1363 | 1364 | 1365 | > This problem was asked by Google. 1366 | 1367 | Invert a binary tree. 1368 | 1369 | For example, given the following tree: 1370 | 1371 | a 1372 | / \ 1373 | b c 1374 | / \ / 1375 | d e f 1376 | 1377 | 1378 | should become: 1379 | 1380 | a 1381 | / \ 1382 | c b 1383 | \ / \ 1384 | f e d 1385 | 1386 | 1387 | ## [Problem-84](src/main/java/in/ashwanik/dcp/problems/p61_90/p84):sunny: 1388 | 1389 | 1390 | > This problem was asked by Amazon. 1391 | 1392 | Given a matrix of 1s and 0s, return the number of "islands" in the matrix. A 1 1393 | represents land and 0 represents water, so an island is a group of 1s that are 1394 | neighboring and their perimiter is surrounded by water. 1395 | 1396 | For example, this matrix has 4 islands. 1397 | 1398 | 1 0 0 0 0 1399 | 0 0 1 1 0 1400 | 0 1 1 0 0 1401 | 0 0 0 0 0 1402 | 1 1 0 0 1 1403 | 1 1 0 0 1 1404 | 1405 | 1406 | ## [Problem-85](src/main/java/in/ashwanik/dcp/problems/p61_90/p85):sunny: 1407 | 1408 | 1409 | > This problem was asked by Facebook. 1410 | 1411 | Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, 1412 | using only mathematical or bit operations. You can assume b can only be 1 or 0. 1413 | 1414 | ## [Problem-86](src/main/java/in/ashwanik/dcp/problems/p61_90/p86):sunny: 1415 | 1416 | 1417 | > This problem was asked by Google. 1418 | 1419 | Given a string of parentheses, write a function to compute the minimum number of 1420 | parentheses to be removed to make the string valid (i.e. each open parenthesis 1421 | is eventually closed). 1422 | 1423 | For example, given the string "()())()", you should return 1. Given the string 1424 | ")(", you should return 2, since we must remove all of them. 1425 | 1426 | ## Problem-87:waxing_crescent_moon: 1427 | 1428 | 1429 | > This problem was asked by Uber. 1430 | 1431 | A rule looks like this: 1432 | 1433 | A NE B 1434 | 1435 | This means this means point A is located northeast of point B. 1436 | 1437 | A SW C 1438 | 1439 | means that point A is southwest of C. 1440 | 1441 | Given a list of rules, check if the sum of the rules validate. For example: 1442 | 1443 | A N B 1444 | B NE C 1445 | C N A 1446 | 1447 | 1448 | does not validate, since A cannot be both north and south of C. 1449 | 1450 | A NW B 1451 | A N B 1452 | 1453 | 1454 | is considered valid. 1455 | 1456 | ## [Problem-88](src/main/java/in/ashwanik/dcp/problems/p61_90/p88):sunny: 1457 | 1458 | 1459 | > This question was asked by ContextLogic. 1460 | 1461 | Implement division of two positive integers without using the division, 1462 | multiplication, or modulus operators. Return the quotient as an integer, 1463 | ignoring the remainder. 1464 | 1465 | ## [Problem-89](src/main/java/in/ashwanik/dcp/problems/p61_90/p89):sunny: 1466 | 1467 | 1468 | > This problem was asked by LinkedIn. 1469 | 1470 | Determine whether a tree is a valid binary search tree. 1471 | 1472 | A binary search tree is a tree with two children, left and right, and satisfies 1473 | the constraint that the key in the left child must be less than or equal to the 1474 | root and the key in the right child must be greater than or equal to the root. 1475 | 1476 | ## [Problem-90](src/main/java/in/ashwanik/dcp/problems/p61_90/p90):sunny: 1477 | 1478 | 1479 | > This question was asked by Google. 1480 | 1481 | Given an integer n and a list of integers l, write a function that randomly 1482 | generates a number from 0 to n-1 that isn't in l (uniform). 1483 | 1484 | ## Problem-91:new_moon: 1485 | 1486 | 1487 | > This problem was asked by Dropbox. 1488 | 1489 | What does the below code snippet print out? How can we fix the anonymous 1490 | functions to behave as we'd expect? 1491 | 1492 | functions = [] 1493 | for i in range(10): 1494 | functions.append(lambda : i) 1495 | 1496 | for f in functions: 1497 | print(f()) 1498 | 1499 | 1500 | ## [Problem-92](src/main/java/in/ashwanik/dcp/problems/p91_120/p92):sunny: 1501 | 1502 | 1503 | > This problem was asked by Airbnb. 1504 | 1505 | We're given a hashmap with a key courseId and value a list of courseIds, which 1506 | represents that the prerequsite of courseId is courseIds. Return a sorted 1507 | ordering of courses such that we can finish all courses. 1508 | 1509 | Return null if there is no such ordering. 1510 | 1511 | For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 1512 | 'CSC100': []}, should return ['CSC100', 'CSC200', 'CSCS300']. 1513 | 1514 | ## Problem-93:waxing_crescent_moon: 1515 | 1516 | 1517 | > This problem was asked by Apple. 1518 | 1519 | Given a tree, find the largest tree/subtree that is a BST. 1520 | 1521 | Given a tree, return the size of the largest tree/subtree that is a BST. 1522 | 1523 | ## [Problem-94](src/main/java/in/ashwanik/dcp/problems/p91_120/p94):sunny: 1524 | 1525 | 1526 | > This problem was asked by Google. 1527 | 1528 | Given a binary tree of integers, find the maximum path sum between two nodes. 1529 | The path must go through at least one node, and does not need to go through the 1530 | root. 1531 | 1532 | ## [Problem-95](src/main/java/in/ashwanik/dcp/problems/p91_120/p95):sunny: 1533 | 1534 | 1535 | > This problem was asked by Palantir. 1536 | 1537 | Given a number represented by a list of digits, find the next greater 1538 | permutation of a number, in terms of lexicographic ordering. If there is not 1539 | greater permutation possible, return the permutation with the lowest 1540 | value/ordering. 1541 | 1542 | For example, the list [1,2,3] should return [1,3,2]. The list [1,3,2] should 1543 | return [2,1,3]. The list [3,2,1] should return [1,2,3]. 1544 | 1545 | Can you perform the operation without allocating extra memory (disregarding the 1546 | input memory)? 1547 | 1548 | ## Problem-96:waxing_crescent_moon: 1549 | 1550 | 1551 | > This problem was asked by Microsoft. 1552 | 1553 | Given a number in the form of a list of digits, return all possible 1554 | permutations. 1555 | 1556 | For example, given [1,2,3], return 1557 | [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. 1558 | 1559 | ## [Problem-97](src/main/java/in/ashwanik/dcp/problems/p91_120/p97):sunny: 1560 | 1561 | 1562 | > This problem was asked by Stripe. 1563 | 1564 | Write a map implementation with a get function that lets you retrieve the value 1565 | of a key at a particular time. 1566 | 1567 | It should contain the following methods: 1568 | 1569 | * set(key, value, time): sets key to value for t = time. 1570 | * get(key, time): gets the key at t = time. 1571 | 1572 | The map should work like this. If we set a key at a particular time, it will 1573 | maintain that value forever or until it gets set at a later time. In other 1574 | words, when we get a key at a time, it should return the value that was set for 1575 | that key set at the most recent time. 1576 | 1577 | Consider the following examples: 1578 | 1579 | d.set(1, 1, 0) # set key 1 to value 1 at time 0 1580 | d.set(1, 2, 2) # set key 1 to value 2 at time 2 1581 | d.get(1, 1) # get key 1 at time 1 should be 1 1582 | d.get(1, 3) # get key 1 at time 3 should be 2 1583 | 1584 | 1585 | d.set(1, 1, 5) # set key 1 to value 1 at time 5 1586 | d.get(1, 0) # get key 1 at time 0 should be null 1587 | d.get(1, 10) # get key 1 at time 10 should be 1 1588 | 1589 | 1590 | d.set(1, 1, 0) # set key 1 to value 1 at time 0 1591 | d.set(1, 2, 0) # set key 1 to value 2 at time 0 1592 | d.get(1, 0) # get key 1 at time 0 should be 2 1593 | 1594 | 1595 | ## Problem-98:waxing_crescent_moon: 1596 | 1597 | 1598 | > This problem was asked by Coursera. 1599 | 1600 | Given a 2D board of characters and a word, find if the word exists in the grid. 1601 | 1602 | The word can be constructed from letters of sequentially adjacent cell, where 1603 | "adjacent" cells are those horizontally or vertically neighboring. The same 1604 | letter cell may not be used more than once. 1605 | 1606 | For example, given the following board: 1607 | 1608 | [ 1609 | ['A','B','C','E'], 1610 | ['S','F','C','S'], 1611 | ['A','D','E','E'] 1612 | ] 1613 | 1614 | 1615 | exists(board, "ABCCED") returns true,exists(board, "SEE") returns true, 1616 | exists(board, "ABCB") returns false. 1617 | 1618 | ## [Problem-99](src/main/java/in/ashwanik/dcp/problems/p91_120/p99):sunny: 1619 | 1620 | 1621 | > This problem was asked by Microsoft. 1622 | 1623 | Given an unsorted array of integers, find the length of the longest consecutive 1624 | elements sequence. 1625 | 1626 | For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element 1627 | sequence is [1, 2, 3, 4]. Return its length: 4. 1628 | 1629 | Your algorithm should run in O(n) complexity. 1630 | 1631 | ## [Problem-100](src/main/java/in/ashwanik/dcp/problems/p91_120/p100):sunny: 1632 | 1633 | 1634 | > This problem was asked by Google. 1635 | 1636 | You are in an infinite 2D grid where you can move in any of the 8 directions: 1637 | 1638 | (x,y) to 1639 | (x+1, y), 1640 | (x - 1, y), 1641 | (x, y+1), 1642 | (x, y-1), 1643 | (x-1, y-1), 1644 | (x+1,y+1), 1645 | (x-1,y+1), 1646 | (x+1,y-1) 1647 | 1648 | 1649 | You are given a sequence of points and the order in which you need to cover the 1650 | points. Give the minimum number of steps in which you can achieve it. You start 1651 | from the first point. 1652 | 1653 | Example: 1654 | 1655 | Input: [(0, 0), (1, 1), (1, 2)] 1656 | Output: 2 1657 | 1658 | 1659 | It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move 1660 | from (1, 1) to (1, 2). 1661 | 1662 | ## [Problem-101](src/main/java/in/ashwanik/dcp/problems/p91_120/p101):sunny: 1663 | 1664 | 1665 | > This problem was asked by Alibaba. 1666 | 1667 | Given an even number (greater than 2), return two prime numbers whose sum will 1668 | be equal to the given number. 1669 | 1670 | A solution will always exist. See Goldbach’s conjecture 1671 | [https://en.wikipedia.org/wiki/Goldbach%27s_conjecture]. 1672 | 1673 | Example: 1674 | 1675 | Input: 4 1676 | Output: 2 + 2 = 4 1677 | 1678 | 1679 | If there are more than one solution possible, return the lexicographically 1680 | smaller solution. 1681 | 1682 | If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= 1683 | d, then 1684 | 1685 | [a, b] < [c, d] 1686 | 1687 | 1688 | If a < c OR a==c AND b < d. 1689 | 1690 | ## [Problem-102](src/main/java/in/ashwanik/dcp/problems/p91_120/p102):sunny: 1691 | 1692 | 1693 | > This problem was asked by Lyft. 1694 | 1695 | Given a list of integers and a number K, return which contiguous elements of the 1696 | list sum to K. 1697 | 1698 | For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return 1699 | [2, 3, 4]. 1700 | 1701 | ## [Problem-103](src/main/java/in/ashwanik/dcp/problems/p91_120/p103):sunny: 1702 | 1703 | 1704 | > This problem was asked by Square. 1705 | 1706 | Given a string and a set of characters, return the shortest substring containing 1707 | all the characters in the set. 1708 | 1709 | For example, given the string "figehaeci" and the set of characters {a, e, i}, 1710 | you should return "aeci". 1711 | 1712 | If there is no substring containing all the characters in the set, return null. 1713 | 1714 | ## [Problem-104](src/main/java/in/ashwanik/dcp/problems/p91_120/p104):sunny: 1715 | 1716 | 1717 | > This problem was asked by Google. 1718 | 1719 | Determine whether a doubly linked list is a palindrome. What if it’s singly 1720 | linked? 1721 | 1722 | For example, 1 -> 4 -> 3 -> 4 -> 1 returns true while 1 -> 4 returns false. 1723 | 1724 | ## Problem-105:new_moon: 1725 | 1726 | 1727 | > This problem was asked by Facebook. 1728 | 1729 | Given a function f, and N return a debounced f of N milliseconds. 1730 | 1731 | That is, as long as the debounced f continues to be invoked, f itself will not 1732 | be called for N milliseconds. 1733 | 1734 | ## Problem-106:waxing_crescent_moon: 1735 | 1736 | 1737 | > This problem was asked by Pinterest. 1738 | 1739 | Given an integer list where each number represents the number of hops you can 1740 | make, determine whether you can reach to the last index starting at index 0. 1741 | 1742 | For example, [2, 0, 1, 0] returns true while [1, 1, 0, 1] returns false. 1743 | 1744 | ## [Problem-107](src/main/java/in/ashwanik/dcp/problems/p91_120/p107):sunny: 1745 | 1746 | 1747 | > This problem was asked by Microsoft. 1748 | 1749 | Print the nodes in a binary tree level-wise. For example, the following should 1750 | print 1, 2, 3, 4, 5. 1751 | 1752 | 1 1753 | / \ 1754 | 2 3 1755 | / \ 1756 | 4 5 1757 | 1758 | 1759 | ## [Problem-108](src/main/java/in/ashwanik/dcp/problems/p91_120/p108):sunny: 1760 | 1761 | 1762 | > This problem was asked by Google. 1763 | 1764 | Given two strings A and B, return whether or not A can be shifted some number of 1765 | times to get B. 1766 | 1767 | For example, if A is abcde and B is cdeab, return true. If A is abc and B is acb 1768 | , return false. 1769 | 1770 | ## Problem-109:new_moon: 1771 | 1772 | 1773 | > This problem was asked by Cisco. 1774 | 1775 | Given an unsigned 8-bit integer, swap its even and odd bits. The 1st and 2nd bit 1776 | should be swapped, the 3rd and 4th bit should be swapped, and so on. 1777 | 1778 | For example, 10101010 should be 01010101. 11100010 should be 11010001. 1779 | 1780 | Bonus: Can you do this in one line? 1781 | 1782 | ## [Problem-110](src/main/java/in/ashwanik/dcp/problems/p91_120/p110):sunny: 1783 | 1784 | 1785 | > This problem was asked by Facebook. 1786 | 1787 | Given a binary tree, return all paths from the root to leaves. 1788 | 1789 | For example, given the tree 1790 | 1791 | 1 1792 | / \ 1793 | 2 3 1794 | / \ 1795 | 4 5 1796 | 1797 | 1798 | it should return [[1, 2], [1, 3, 4], [1, 3, 5]]. 1799 | 1800 | ## [Problem-111](src/main/java/in/ashwanik/dcp/problems/p91_120/p111):sunny: 1801 | 1802 | 1803 | > This problem was asked by Google. 1804 | 1805 | Given a word W and a string S, find all starting indices in S which are anagrams 1806 | of W. 1807 | 1808 | For example, given that W is "ab", and S is "abxaba", return 0, 3, and 4. 1809 | 1810 | ## [Problem-112](src/main/java/in/ashwanik/dcp/problems/p91_120/p112):sunny: 1811 | 1812 | 1813 | > This problem was asked by Twitter. 1814 | 1815 | Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in 1816 | the tree. Assume that each node in the tree also has a pointer to its parent. 1817 | 1818 | According to the definition of LCA on Wikipedia 1819 | [https://en.wikipedia.org/wiki/Lowest_common_ancestor]: “The lowest common 1820 | ancestor is defined between two nodes v and w as the lowest node in T that has 1821 | both v and w as descendants (where we allow a node to be a descendant of 1822 | itself).” 1823 | 1824 | ## [Problem-113](src/main/java/in/ashwanik/dcp/problems/p91_120/p113):sunny: 1825 | 1826 | 1827 | > This problem was asked by Google. 1828 | 1829 | Given a string of words delimited by spaces, reverse the words in string. For 1830 | example, given "hello world here", return "here world hello" 1831 | 1832 | Follow-up: given a mutable string representation, can you perform this operation 1833 | in-place? 1834 | 1835 | ## [Problem-114](src/main/java/in/ashwanik/dcp/problems/p91_120/p114):sunny: 1836 | 1837 | 1838 | > This problem was asked by Facebook. 1839 | 1840 | Given a string and a set of delimiters, reverse the words in the string while 1841 | maintaining the relative order of the delimiters. For example, given 1842 | "hello/world:here", return "here/world:hello" 1843 | 1844 | Follow-up: Does your solution work for the following cases: "hello/world:here/", 1845 | "hello//world:here" 1846 | 1847 | ## [Problem-115](src/main/java/in/ashwanik/dcp/problems/p91_120/p115):sunny: 1848 | 1849 | 1850 | > This problem was asked by Google. 1851 | 1852 | Given two non-empty binary trees s and t, check whether tree t has exactly the 1853 | same structure and node values with a subtree of s. A subtree of s is a tree 1854 | consists of a node in s and all of this node's descendants. The tree s could 1855 | also be considered as a subtree of itself. 1856 | 1857 | ## [Problem-116](src/main/java/in/ashwanik/dcp/problems/p91_120/p116):sunny: 1858 | 1859 | 1860 | > This problem was asked by Jane Street. 1861 | 1862 | Generate a finite, but an arbitrarily large binary tree quickly in O(1). 1863 | 1864 | That is, generate() should return a tree whose size is unbounded but finite. 1865 | 1866 | ## [Problem-117](src/main/java/in/ashwanik/dcp/problems/p91_120/p117):sunny: 1867 | 1868 | 1869 | > This problem was asked by Facebook. 1870 | 1871 | Given a binary tree, return the level of the tree with minimum sum. 1872 | 1873 | ## [Problem-118](src/main/java/in/ashwanik/dcp/problems/p91_120/p118):sunny: 1874 | 1875 | 1876 | > This problem was asked by Google. 1877 | 1878 | Given a sorted list of integers, square the elements and give the output in 1879 | sorted order. 1880 | 1881 | For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81]. 1882 | 1883 | ## [Problem-119](src/main/java/in/ashwanik/dcp/problems/p91_120/p119):sunny: 1884 | 1885 | 1886 | > This problem was asked by Google. 1887 | 1888 | Given a set of closed intervals, find the smallest set of numbers that covers 1889 | all the intervals. If there are multiple smallest sets, return any of them. 1890 | 1891 | For example, given the intervals [0, 3], [2, 6], [3, 4], [6, 9], one set of 1892 | numbers that covers all these intervals is {3, 6}. 1893 | 1894 | ## [Problem-120](src/main/java/in/ashwanik/dcp/problems/p91_120/p120):sunny: 1895 | 1896 | 1897 | > This problem was asked by Microsoft. 1898 | 1899 | Implement the singleton pattern with a twist. First, instead of storing one 1900 | instance, store two instances. And in every even call of getInstance(), return 1901 | the first instance and in every odd call of getInstance(), return the second 1902 | instance. 1903 | 1904 | ## [Problem-121](src/main/java/in/ashwanik/dcp/problems/p121_150/p121):sunny: 1905 | 1906 | 1907 | > This problem was asked by Google. 1908 | 1909 | Given a string which we can delete at most k, return whether you can make a 1910 | palindrome. 1911 | 1912 | For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get 1913 | 'waterretaw'. 1914 | 1915 | ## Problem-122:waxing_crescent_moon: 1916 | 1917 | 1918 | > This question was asked by Zillow. 1919 | 1920 | You are given a 2-d matrix where each cell represents number of coins in that 1921 | cell. Assuming we start at matrix[0][0], and can only move right or down, find 1922 | the maximum number of coins you can collect by the bottom right corner. 1923 | 1924 | For example, in this matrix 1925 | 1926 | 0 3 1 1 1927 | 2 0 0 4 1928 | 1 5 3 1 1929 | 1930 | 1931 | The most we can collect is 0 + 2 + 1 + 5 + 3 + 1 = 12 coins. 1932 | 1933 | ## Problem-123:new_moon: 1934 | 1935 | 1936 | > This problem was asked by LinkedIn. 1937 | 1938 | Given a string, return whether it represents a number. Here are the different 1939 | kinds of numbers: 1940 | 1941 | * "10", a positive integer 1942 | * "-10", a negative integer 1943 | * "10.1", a positive real number 1944 | * "-10.1", a negative real number 1945 | * "1e5", a number in scientific notation 1946 | 1947 | And here are examples of non-numbers: 1948 | 1949 | * "a" 1950 | * "x 1" 1951 | * "a -2" 1952 | * "-" 1953 | 1954 | ## Problem-124:new_moon: 1955 | 1956 | 1957 | > This problem was asked by Microsoft. 1958 | 1959 | You have 100 fair coins and you flip them all at the same time. Any that come up 1960 | tails you set aside. The ones that come up heads you flip again. How many rounds 1961 | do you expect to play before only one coin remains? 1962 | 1963 | Write a function that, given n, returns the number of rounds you'd expect to 1964 | play until one coin remains. 1965 | 1966 | ## [Problem-125](src/main/java/in/ashwanik/dcp/problems/p121_150/p125):sunny: 1967 | 1968 | 1969 | > This problem was asked by Google. 1970 | 1971 | Given the root of a binary search tree, and a target K, return two nodes in the 1972 | tree whose sum equals K. 1973 | 1974 | For example, given the following tree and K of 20 1975 | 1976 | 10 1977 | / \ 1978 | 5 15 1979 | / \ 1980 | 11 15 1981 | 1982 | 1983 | Return the nodes 5 and 15. 1984 | 1985 | ## [Problem-126](src/main/java/in/ashwanik/dcp/problems/p121_150/p126):sunny: 1986 | 1987 | 1988 | > This problem was asked by Facebook. 1989 | 1990 | Write a function that rotates a list by k elements. For example, [1, 2, 3, 4, 5, 1991 | 6] rotated by two becomes [3, 4, 5, 6, 1, 2]. Try solving this without creating 1992 | a copy of the list. How many swap or move operations do you need? 1993 | 1994 | ## [Problem-127](src/main/java/in/ashwanik/dcp/problems/p121_150/p127):sunny: 1995 | 1996 | 1997 | > This problem was asked by Microsoft. 1998 | 1999 | Let's represent an integer in a linked list format by having each node represent 2000 | a digit in the number. The nodes make up the number in reversed order. 2001 | 2002 | For example, the following linked list: 2003 | 2004 | 1 -> 2 -> 3 -> 4 -> 5 2005 | 2006 | 2007 | is the number 54321. 2008 | 2009 | Given two linked lists in this format, return their sum in the same linked list 2010 | format. 2011 | 2012 | For example, given 2013 | 2014 | 9 -> 9 2015 | 2016 | 2017 | 5 -> 2 2018 | 2019 | 2020 | return 124 (99 + 25) as: 2021 | 2022 | 4 -> 2 -> 1 2023 | 2024 | 2025 | ## [Problem-128](src/main/java/in/ashwanik/dcp/problems/p121_150/p128):sunny: 2026 | 2027 | 2028 | The Tower of Hanoi is a puzzle game with three rods and n disks, each a 2029 | different size. 2030 | 2031 | All the disks start off on the first rod in a stack. They are ordered by size, 2032 | with the largest disk on the bottom and the smallest one at the top. 2033 | 2034 | The goal of this puzzle is to move all the disks from the first rod to the last 2035 | rod while following these rules: 2036 | 2037 | * You can only move one disk at a time. 2038 | * A move consists of taking the uppermost disk from one of the stacks and 2039 | placing it on top of another stack. 2040 | * You cannot place a larger disk on top of a smaller disk. 2041 | 2042 | Write a function that prints out all the steps necessary to complete the Tower 2043 | of Hanoi. You should assume that the rods are numbered, with the first rod being 2044 | 1, the second (auxiliary) rod being 2, and the last (goal) rod being 3. 2045 | 2046 | For example, with n = 3, we can do this in 7 moves: 2047 | 2048 | Move 1 to 3 2049 | Move 1 to 2 2050 | Move 3 to 2 2051 | Move 1 to 3 2052 | Move 2 to 1 2053 | Move 2 to 3 2054 | Move 1 to 3 2055 | 2056 | 2057 | ## [Problem-129](src/main/java/in/ashwanik/dcp/problems/p121_150/p129):sunny: 2058 | 2059 | 2060 | Given a real number n, find the square root of n. For example, given n = 9, 2061 | return 3. 2062 | 2063 | ## Problem-130:waxing_crescent_moon: 2064 | 2065 | 2066 | > This problem was asked by Facebook. 2067 | 2068 | Given an array of numbers representing the stock prices of a company in 2069 | chronological order and an integer k, return the maximum profit you can make 2070 | from k buys and sells. You must buy the stock before you can sell it, and you 2071 | must sell the stock before you can buy it again. 2072 | 2073 | For example, given k = 2 and the array [5, 2, 4, 0, 1], you should return 3. 2074 | 2075 | ## [Problem-131](src/main/java/in/ashwanik/dcp/problems/p121_150/p131):sunny: 2076 | 2077 | 2078 | > This question was asked by Snapchat. 2079 | 2080 | Given the head to a singly linked list, where each node also has a “random” 2081 | pointer that points to anywhere in the linked list, deep clone the list. 2082 | 2083 | ## Problem-132:waxing_crescent_moon: 2084 | 2085 | 2086 | > This question was asked by Riot Games. 2087 | 2088 | Design and implement a HitCounter class that keeps track of requests (or hits). 2089 | It should support the following operations: 2090 | 2091 | * record(timestamp): records a hit that happened at timestamp 2092 | * total(): returns the total number of hits recorded 2093 | * range(lower, upper): returns the number of hits that occurred between 2094 | timestamps lower and upper (inclusive) 2095 | 2096 | Follow-up: What if our system has limited memory? 2097 | 2098 | ## [Problem-133](src/main/java/in/ashwanik/dcp/problems/p121_150/p133):sunny: 2099 | 2100 | 2101 | > This problem was asked by Amazon. 2102 | 2103 | Given a node in a binary tree, return the next bigger element, also known as the 2104 | inorder successor. 2105 | 2106 | For example, the inorder successor of 22 is 30. 2107 | 2108 | 10 2109 | / \ 2110 | 5 30 2111 | / \ 2112 | 22 35 2113 | 2114 | 2115 | You can assume each node has a parent pointer. 2116 | 2117 | ## [Problem-134](src/main/java/in/ashwanik/dcp/problems/p121_150/p134):sunny: 2118 | 2119 | 2120 | > This problem was asked by Facebook. 2121 | 2122 | You have a large array with most of the elements as zero. 2123 | 2124 | Use a more space-efficient data structure, SparseArray, that implements the same 2125 | interface: 2126 | 2127 | * init(arr, size): initialize with the original large array and size. 2128 | * set(i, val): updates index at i with val. 2129 | * get(i): gets the value at index i. 2130 | 2131 | ## [Problem-135](src/main/java/in/ashwanik/dcp/problems/p121_150/p135):sunny: 2132 | 2133 | 2134 | > This question was asked by Apple. 2135 | 2136 | Given a binary tree, find a minimum path sum from root to a leaf. 2137 | 2138 | For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15. 2139 | 2140 | 10 2141 | / \ 2142 | 5 5 2143 | \ \ 2144 | 2 1 2145 | / 2146 | -1 2147 | 2148 | 2149 | ## [Problem-136](src/main/java/in/ashwanik/dcp/problems/p121_150/p136):sunny: 2150 | 2151 | 2152 | > This question was asked by Google. 2153 | 2154 | Given an N by M matrix consisting only of 1's and 0's, find the largest 2155 | rectangle containing only 1's and return its area. 2156 | 2157 | For example, given the following matrix: 2158 | 2159 | [[1, 0, 0, 0], 2160 | [1, 0, 1, 1], 2161 | [1, 0, 1, 1], 2162 | [0, 1, 0, 0]] 2163 | 2164 | 2165 | Return 4. 2166 | 2167 | ## [Problem-137](src/main/java/in/ashwanik/dcp/problems/p121_150/p137):sunny: 2168 | 2169 | 2170 | > This problem was asked by Amazon. 2171 | 2172 | Implement a bit array. 2173 | 2174 | A bit array is a space efficient array that holds a value of 1 or 0 at each 2175 | index. 2176 | 2177 | * init(size): initialize the array with size 2178 | * set(i, val): updates index at i with val where val is either 1 or 0. 2179 | * get(i): gets the value at index i. 2180 | 2181 | ## Problem-138:waxing_crescent_moon: 2182 | 2183 | 2184 | > This problem was asked by Google. 2185 | 2186 | Find the minimum number of coins required to make n cents. 2187 | 2188 | You can use standard American denominations, that is, 1¢, 5¢, 10¢, and 25¢. 2189 | 2190 | For example, given n = 16, return 3 since we can make it with a 10¢, a 5¢, and a 2191 | 1¢. 2192 | 2193 | ## Problem-139:new_moon: 2194 | 2195 | 2196 | > This problem was asked by Google. 2197 | 2198 | Given an iterator with methods next() and hasNext(), create a wrapper iterator, 2199 | PeekableInterface, which also implements peek(). peek shows the next element 2200 | that would be returned on next(). 2201 | 2202 | Here is the interface: 2203 | 2204 | class PeekableInterface(object): 2205 | def __init__(self, iterator): 2206 | pass 2207 | 2208 | def peek(self): 2209 | pass 2210 | 2211 | def next(self): 2212 | pass 2213 | 2214 | def hasNext(self): 2215 | pass 2216 | 2217 | 2218 | ## Problem-140:new_moon: 2219 | 2220 | 2221 | > This problem was asked by Facebook. 2222 | 2223 | Given an array of integers in which two elements appear exactly once and all 2224 | other elements appear exactly twice, find the two elements that appear only 2225 | once. 2226 | 2227 | For example, given the array [2, 4, 6, 8, 10, 2, 6, 10], return 4 and 8. The 2228 | order does not matter. 2229 | 2230 | Follow-up: Can you do this in linear time and constant space? 2231 | 2232 | ## [Problem-141](src/main/java/in/ashwanik/dcp/problems/p121_150/p141):sunny: 2233 | 2234 | 2235 | > This problem was asked by Microsoft. 2236 | 2237 | Implement 3 stacks using a single list: 2238 | 2239 | class Stack: 2240 | def __init__(self): 2241 | self.list = [] 2242 | 2243 | def pop(self, stack_number): 2244 | pass 2245 | 2246 | def push(self, item, stack_number): 2247 | pass 2248 | 2249 | 2250 | ## [Problem-142](src/main/java/in/ashwanik/dcp/problems/p121_150/p142):sunny: 2251 | 2252 | 2253 | > This problem was asked by Google. 2254 | 2255 | You're given a string consisting solely of (, ), and *. * can represent either a 2256 | (, ), or an empty string. Determine whether the parentheses are balanced. 2257 | 2258 | For example, (()* and (*) are balanced. )*( is not balanced. 2259 | 2260 | ## [Problem-143](src/main/java/in/ashwanik/dcp/problems/p121_150/p143):sunny: 2261 | 2262 | 2263 | > This problem was asked by Amazon. 2264 | 2265 | Given a pivot x, and a list lst, partition the list into three parts. 2266 | 2267 | * The first part contains all elements in lst that are less than x 2268 | * The second part contains all elements in lst that are equal to x 2269 | * The third part contains all elements in lst that are larger than x 2270 | 2271 | Ordering within a part can be arbitrary. 2272 | 2273 | For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10], one partition may 2274 | be `[9, 3, 5, 10, 10, 12, 14]. 2275 | 2276 | ## Problem-144:waxing_crescent_moon: 2277 | 2278 | 2279 | > This problem was asked by Google. 2280 | 2281 | Given an array of numbers and an index i, return the index of the nearest larger 2282 | number of the number at index i, where distance is measured in array indices. 2283 | 2284 | For example, given [4, 1, 3, 5, 6] and index 0, you should return 3. 2285 | 2286 | If two distances to larger numbers are the equal, then return any one of them. 2287 | If the array at i doesn't have a nearest larger integer, then return null. 2288 | 2289 | Follow-up: If you can preprocess the array, can you do this in constant time? 2290 | 2291 | ## [Problem-145](src/main/java/in/ashwanik/dcp/problems/p121_150/p145):sunny: 2292 | 2293 | 2294 | > This problem was asked by Google. 2295 | 2296 | Given the head of a singly linked list, swap every two nodes and return its 2297 | head. 2298 | 2299 | For example, given 1 -> 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3. 2300 | 2301 | ## [Problem-146](src/main/java/in/ashwanik/dcp/problems/p121_150/p146):sunny: 2302 | 2303 | 2304 | > This question was asked by BufferBox. 2305 | 2306 | Given a binary tree where all nodes are either 0 or 1, prune the tree so that 2307 | subtrees containing all 0s are removed. 2308 | 2309 | For example, given the following tree: 2310 | 2311 | 0 2312 | / \ 2313 | 1 0 2314 | / \ 2315 | 1 0 2316 | / \ 2317 | 0 0 2318 | 2319 | 2320 | should be pruned to: 2321 | 2322 | 0 2323 | / \ 2324 | 1 0 2325 | / 2326 | 1 2327 | 2328 | 2329 | We do not remove the tree at the root or its left child because it still has a 1 2330 | as a descendant. 2331 | 2332 | ## [Problem-147](src/main/java/in/ashwanik/dcp/problems/p121_150/p147):sunny: 2333 | 2334 | 2335 | Given a list, sort it using this method: reverse(lst, i, j), which sorts lst 2336 | from i to j`. 2337 | 2338 | ## [Problem-148](src/main/java/in/ashwanik/dcp/problems/p121_150/p148):sunny: 2339 | 2340 | 2341 | > This problem was asked by Apple. 2342 | 2343 | Gray code [https://en.wikipedia.org/wiki/Gray_code] is a binary code where each 2344 | successive value differ in only one bit, as well as when wrapping around. Gray 2345 | code is common in hardware so that we don't see temporary spurious values during 2346 | transitions. 2347 | 2348 | Given a number of bits n, generate a possible gray code for it. 2349 | 2350 | For example, for n = 2, one gray code would be [00, 01, 11, 10]. 2351 | 2352 | ## Problem-149:waxing_crescent_moon: 2353 | 2354 | 2355 | > This problem was asked by Goldman Sachs. 2356 | 2357 | Given a list of numbers L, implement a method sum(i, j) which returns the sum 2358 | from the sublist L[i:j] (including i, excluding j). 2359 | 2360 | For example, given L = [1, 2, 3, 4, 5], sum(1, 3) should return sum([2, 3]), 2361 | which is 5. 2362 | 2363 | You can assume that you can do some pre-processing. sum() should be optimized 2364 | over the pre-processing step. 2365 | 2366 | ## [Problem-150](src/main/java/in/ashwanik/dcp/problems/p121_150/p150):sunny: 2367 | 2368 | 2369 | > This problem was asked by LinkedIn. 2370 | 2371 | Given a list of points, a central point, and an integer k, find the nearest k 2372 | points from the central point. 2373 | 2374 | For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central 2375 | point (1, 2), and k = 2, return[(0, 0), (3, 1)]. 2376 | 2377 | ## [Problem-151](src/main/java/in/ashwanik/dcp/problems/p151_180/p151):sunny: 2378 | 2379 | 2380 | Given a 2-D matrix representing an image, a location of a pixel in the screen 2381 | and a color C, replace the color of the given pixel and all adjacent same 2382 | colored pixels with C. 2383 | 2384 | For example, given the following matrix, and location pixel of (2, 2), and 'G' 2385 | for green: 2386 | 2387 | B B W 2388 | W W W 2389 | W W W 2390 | B B B 2391 | 2392 | 2393 | Becomes 2394 | 2395 | B B G 2396 | G G G 2397 | G G G 2398 | B B B 2399 | 2400 | 2401 | ## Problem-152:new_moon: 2402 | 2403 | 2404 | > This problem was asked by Triplebyte. 2405 | 2406 | You are given n numbers as well as n probabilities that sum up to 1. Write a 2407 | function to generate one of the numbers with its corresponding probability. 2408 | 2409 | For example, given the numbers [1, 2, 3, 4] and probabilities [0.1, 0.5, 0.2, 2410 | 0.2], your function should return 1 10% of the time, 2 50% of the time, and 3 2411 | and 4 20% of the time. 2412 | 2413 | You can generate random numbers between 0 and 1 uniformly. 2414 | 2415 | ## [Problem-153](src/main/java/in/ashwanik/dcp/problems/p151_180/p153):sunny: 2416 | 2417 | 2418 | Find an efficient algorithm to find the smallest distance (measured in number of 2419 | words) between any two given words in a string. 2420 | 2421 | For example, given words "hello", and "world" and a text content of "dog cat 2422 | hello cat dog dog hello cat world", return 1 because there's only one word "cat" 2423 | in between the two words. 2424 | 2425 | ## [Problem-154](src/main/java/in/ashwanik/dcp/problems/p151_180/p154):sunny: 2426 | 2427 | 2428 | > This problem was asked by Amazon. 2429 | 2430 | Implement a stack API using only a heap. A stack implements the following 2431 | methods: 2432 | 2433 | * push(item), which adds an element to the stack 2434 | * pop(), which removes and returns the most recently added element (or throws 2435 | an error if there is nothing on the stack) 2436 | 2437 | Recall that a heap has the following operations: 2438 | 2439 | * push(item), which adds a new key to the heap 2440 | * pop(), which removes and returns the max value of the heap 2441 | 2442 | ## [Problem-155](src/main/java/in/ashwanik/dcp/problems/p151_180/p155):sunny: 2443 | 2444 | 2445 | > This problem was asked by MongoDB. 2446 | 2447 | Given a list of elements, find the majority element, which appears more than 2448 | half the time (> floor(len(lst) / 2.0)). 2449 | 2450 | You can assume that such element exists. 2451 | 2452 | For example, given [1, 2, 1, 1, 3, 4, 0], return 1. 2453 | 2454 | ## [Problem-156](src/main/java/in/ashwanik/dcp/problems/p151_180/p156):sunny: 2455 | 2456 | 2457 | > This problem was asked by Facebook. 2458 | 2459 | Given a positive integer n, find the smallest number of squared integers which 2460 | sum to n. 2461 | 2462 | For example, given n = 13, return 2 since 13 = 32 + 22 = 9 + 4. 2463 | 2464 | Given n = 27, return 3 since 27 = 32 + 32 + 32 = 9 + 9 + 9. 2465 | 2466 | ## [Problem-157](src/main/java/in/ashwanik/dcp/problems/p151_180/p157):sunny: 2467 | 2468 | 2469 | > This problem was asked by Amazon. 2470 | 2471 | Given a string, determine whether any permutation of it is a palindrome. 2472 | 2473 | For example, carrace should return true, since it can be rearranged to form 2474 | racecar, which is a palindrome. daily should return false, since there's no 2475 | rearrangement that can form a palindrome. 2476 | 2477 | ## Problem-158:waxing_crescent_moon: 2478 | 2479 | 2480 | > This problem was asked by Slack. 2481 | 2482 | You are given an N by M matrix of 0s and 1s. Starting from the top left corner, 2483 | how many ways are there to reach the bottom right corner? 2484 | 2485 | You can only move right and down. 0 represents an empty space while 1 represents 2486 | a wall you cannot walk through. 2487 | 2488 | For example, given the following matrix: 2489 | 2490 | [[0, 0, 1], 2491 | [0, 0, 1], 2492 | [1, 0, 0]] 2493 | 2494 | 2495 | Return two, as there are only two ways to get to the bottom right: 2496 | 2497 | * Right, down, down, right 2498 | * Down, right, down, right 2499 | 2500 | The top left corner and bottom right corner will always be 0. 2501 | 2502 | ## [Problem-159](src/main/java/in/ashwanik/dcp/problems/p151_180/p159):sunny: 2503 | 2504 | 2505 | > This problem was asked by Google. 2506 | 2507 | Given a string, return the first recurring character in it, or null if there is 2508 | no recurring character. 2509 | 2510 | For example, given the string "acbbac", return "b". Given the string "abcdef", 2511 | return null. 2512 | 2513 | ## Problem-160:waxing_crescent_moon: 2514 | 2515 | 2516 | > This problem was asked by Uber. 2517 | 2518 | Given a tree where each edge has a weight, compute the length of the longest 2519 | path in the tree. 2520 | 2521 | For example, given the following tree: 2522 | 2523 | a 2524 | /|\ 2525 | b c d 2526 | / \ 2527 | e f 2528 | / \ 2529 | g h 2530 | 2531 | 2532 | and the weights: a-b: 3, a-c: 5, a-d: 8, d-e: 2, d-f: 4, e-g: 1, e-h: 1, the 2533 | longest path would be c -> a -> d -> f, with a length of 17. 2534 | 2535 | The path does not have to pass through the root, and each node can have any 2536 | amount of children. 2537 | 2538 | ## Problem-161:new_moon: 2539 | 2540 | 2541 | > This problem was asked by Facebook. 2542 | 2543 | Given a 32-bit integer, return the number with its bits reversed. 2544 | 2545 | For example, given the binary number 1111 0000 1111 0000 1111 0000 1111 0000, 2546 | return 0000 1111 0000 1111 0000 1111 0000 1111. 2547 | 2548 | ## Problem-162:new_moon: 2549 | 2550 | 2551 | > This problem was asked by Square. 2552 | 2553 | Given a list of words, return the shortest unique prefix of each word. For 2554 | example, given the list: 2555 | 2556 | * dog 2557 | * cat 2558 | * apple 2559 | * apricot 2560 | * fish 2561 | 2562 | Return the list: 2563 | 2564 | * d 2565 | * c 2566 | * app 2567 | * apr 2568 | * f 2569 | 2570 | ## [Problem-163](src/main/java/in/ashwanik/dcp/problems/p151_180/p163):sunny: 2571 | 2572 | 2573 | > This problem was asked by Jane Street. 2574 | 2575 | Given an arithmetic expression in Reverse Polish Notation 2576 | [https://en.wikipedia.org/wiki/Reverse_Polish_notation], write a program to 2577 | evaluate it. 2578 | 2579 | The expression is given as a list of numbers and operands. For example: [5, 3, 2580 | '+'] should return 5 + 3 = 8. 2581 | 2582 | For example, [15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-'] should 2583 | return 5, since it is equivalent to ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 2584 | 5. 2585 | 2586 | You can assume the given expression is always valid. 2587 | 2588 | ## [Problem-164](src/main/java/in/ashwanik/dcp/problems/p151_180/p164):sunny: 2589 | 2590 | 2591 | > This problem was asked by Google. 2592 | 2593 | You are given an array of length n + 1 whose elements belong to the set {1, 2, 2594 | ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in 2595 | linear time and space. 2596 | 2597 | ## Problem-165:waxing_crescent_moon: 2598 | 2599 | 2600 | > This problem was asked by Google. 2601 | 2602 | Given an array of integers, return a new array where each element in the new 2603 | array is the number of smaller elements to the right of that element in the 2604 | original input array. 2605 | 2606 | For example, given the array [3, 4, 9, 6, 1], return [1, 1, 2, 1, 0], since: 2607 | 2608 | * There is 1 smaller element to the right of 3 2609 | * There is 1 smaller element to the right of 4 2610 | * There are 2 smaller elements to the right of 9 2611 | * There is 1 smaller element to the right of 6 2612 | * There are no smaller elements to the right of 1 2613 | 2614 | ## [Problem-166](src/main/java/in/ashwanik/dcp/problems/p151_180/p166):sunny: 2615 | 2616 | 2617 | > This problem was asked by Uber. 2618 | 2619 | Implement a 2D iterator class. It will be initialized with an array of arrays, 2620 | and should implement the following methods: 2621 | 2622 | * next(): returns the next element in the array of arrays. If there are no more 2623 | elements, raise an exception. 2624 | * has_next(): returns whether or not the iterator still has elements left. 2625 | 2626 | For example, given the input [[1, 2], [3], [], [4, 5, 6]], calling next() 2627 | repeatedly should output 1, 2, 3, 4, 5, 6. 2628 | 2629 | Do not use flatten or otherwise clone the arrays. Some of the arrays can be 2630 | empty. 2631 | 2632 | ## Problem-167:waxing_crescent_moon: 2633 | 2634 | 2635 | > This problem was asked by Airbnb. 2636 | 2637 | Given a list of words, find all pairs of unique indices such that the 2638 | concatenation of the two words is a palindrome. 2639 | 2640 | For example, given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), 2641 | (2, 3)]. 2642 | 2643 | ## [Problem-168](src/main/java/in/ashwanik/dcp/problems/p151_180/p168):sunny: 2644 | 2645 | 2646 | > This problem was asked by Facebook. 2647 | 2648 | Given an N by N matrix, rotate it by 90 degrees clockwise. 2649 | 2650 | For example, given the following matrix: 2651 | 2652 | [[1, 2, 3], 2653 | [4, 5, 6], 2654 | [7, 8, 9]] 2655 | 2656 | 2657 | you should return: 2658 | 2659 | [[7, 4, 1], 2660 | [8, 5, 2], 2661 | [9, 6, 3]] 2662 | 2663 | 2664 | Follow-up: What if you couldn't use any extra space? 2665 | 2666 | ## [Problem-169](src/main/java/in/ashwanik/dcp/problems/p151_180/p169):sunny: 2667 | 2668 | 2669 | > This problem was asked by Google. 2670 | 2671 | Given a linked list, sort it in O(n log n) time and constant space. 2672 | 2673 | For example, the linked list 4 -> 1 -> -3 -> 99 should become -3 -> 1 -> 4 -> 99 2674 | . 2675 | 2676 | ## Problem-170:waxing_crescent_moon: 2677 | 2678 | 2679 | > This problem was asked by Facebook. 2680 | 2681 | Given a start word, an end word, and a dictionary of valid words, find the 2682 | shortest transformation sequence from start to end such that only one letter is 2683 | changed at each step of the sequence, and each transformed word exists in the 2684 | dictionary. If there is no possible transformation, return null. Each word in 2685 | the dictionary have the same length as start and end and is lowercase. 2686 | 2687 | For example, given start = "dog", end = "cat", and dictionary = {"dot", "dop", 2688 | "dat", "cat"}, return ["dog", "dot", "dat", "cat"]. 2689 | 2690 | Given start = "dog", end = "cat", and dictionary = {"dot", "tod", "dat", "dar"}, 2691 | return null as there is no possible transformation from dog to cat. 2692 | 2693 | ## [Problem-171](src/main/java/in/ashwanik/dcp/problems/p151_180/p171):sunny: 2694 | 2695 | 2696 | > This problem was asked by Amazon. 2697 | 2698 | You are given a list of data entries that represent entries and exits of groups 2699 | of people into a building. An entry looks like this: 2700 | 2701 | {"timestamp": 1526579928, count: 3, "type": "enter"} 2702 | 2703 | This means 3 people entered the building. An exit looks like this: 2704 | 2705 | {"timestamp": 1526580382, count: 2, "type": "exit"} 2706 | 2707 | This means that 2 people exited the building. timestamp is in Unix time 2708 | [https://en.wikipedia.org/wiki/Unix_time]. 2709 | 2710 | Find the busiest period in the building, that is, the time with the most people 2711 | in the building. Return it as a pair of (start, end) timestamps. You can assume 2712 | the building always starts off and ends up empty, i.e. with 0 people inside. 2713 | 2714 | ## [Problem-172](src/main/java/in/ashwanik/dcp/problems/p151_180/p172):sunny: 2715 | 2716 | 2717 | > This problem was asked by Dropbox. 2718 | 2719 | Given a string s and a list of words words, where each word is the same length, 2720 | find all starting indices of substrings in sthat is a concatenation of every 2721 | word in words exactly once. 2722 | 2723 | For example, given s = "dogcatcatcodecatdog" and words = ["cat", "dog"], return 2724 | [0, 13], since "dogcat" starts at index 0 and "catdog" starts at index 13. 2725 | 2726 | Given s = "barfoobazbitbyte" and words = ["dog", "cat"], return [] since there 2727 | are no substrings composed of "dog" and "cat" in s. 2728 | 2729 | The order of the indices does not matter. 2730 | 2731 | ## [Problem-173](src/main/java/in/ashwanik/dcp/problems/p151_180/p173):sunny: 2732 | 2733 | 2734 | > This problem was asked by Stripe. 2735 | 2736 | Write a function to flatten a nested dictionary. Namespace the keys with a 2737 | period. 2738 | 2739 | For example, given the following dictionary: 2740 | 2741 | { 2742 | "key": 3, 2743 | "foo": { 2744 | "a": 5, 2745 | "bar": { 2746 | "baz": 8 2747 | } 2748 | } 2749 | } 2750 | 2751 | 2752 | it should become: 2753 | 2754 | { 2755 | "key": 3, 2756 | "foo.a": 5, 2757 | "foo.bar.baz": 8 2758 | } 2759 | 2760 | 2761 | You can assume keys do not contain dots in them, i.e. no clobbering will occur. 2762 | 2763 | ## Problem-174:new_moon: 2764 | 2765 | 2766 | > This problem was asked by Microsoft. 2767 | 2768 | Describe and give an example of each of the following types of polymorphism: 2769 | 2770 | * Ad-hoc polymorphism 2771 | * Parametric polymorphism 2772 | * Subtype polymorphism 2773 | 2774 | ## [Problem-175](src/main/java/in/ashwanik/dcp/problems/p151_180/p175):sunny: 2775 | 2776 | 2777 | > This problem was asked by Google. 2778 | 2779 | You are given a starting state start, a list of transition probabilities for a 2780 | Markov chain, and a number of steps num_steps. Run the Markov chain starting 2781 | from start for num_steps and compute the number of times we visited each state. 2782 | 2783 | For example, given the starting state a, number of steps 5000, and the following 2784 | transition probabilities: 2785 | 2786 | [ 2787 | ('a', 'a', 0.9), 2788 | ('a', 'b', 0.075), 2789 | ('a', 'c', 0.025), 2790 | ('b', 'a', 0.15), 2791 | ('b', 'b', 0.8), 2792 | ('b', 'c', 0.05), 2793 | ('c', 'a', 0.25), 2794 | ('c', 'b', 0.25), 2795 | ('c', 'c', 0.5) 2796 | ] 2797 | 2798 | 2799 | One instance of running this Markov chain might produce { 'a': 3012, 'b': 1656, 2800 | 'c': 332 }. 2801 | 2802 | ## [Problem-176](src/main/java/in/ashwanik/dcp/problems/p151_180/p176):sunny: 2803 | 2804 | 2805 | > This problem was asked by Bloomberg. 2806 | 2807 | Determine whether there exists a one-to-one character mapping from one string s1 2808 | to another s2. 2809 | 2810 | For example, given s1 = abc and s2 = bcd, return true since we can map a to b, b 2811 | to c, and c to d. 2812 | 2813 | Given s1 = foo and s2 = bar, return false since the o cannot map to two 2814 | characters. 2815 | 2816 | ## [Problem-177](src/main/java/in/ashwanik/dcp/problems/p151_180/p177):sunny: 2817 | 2818 | 2819 | > This problem was asked by Airbnb. 2820 | 2821 | Given a linked list and a positive integer k, rotate the list to the right by k 2822 | places. 2823 | 2824 | For example, given the linked list 7 -> 7 -> 3 -> 5 and k = 2, it should become 2825 | 3 -> 5 -> 7 -> 7. 2826 | 2827 | Given the linked list 1 -> 2 -> 3 -> 4 -> 5 and k = 3, it should become 3 -> 4 2828 | -> 5 -> 1 -> 2. 2829 | 2830 | ## [Problem-178](src/main/java/in/ashwanik/dcp/problems/p151_180/p178):sunny: 2831 | 2832 | 2833 | > This problem was asked by Two Sigma. 2834 | 2835 | Alice wants to join her school's Probability Student Club. Membership dues are 2836 | computed via one of two simple probabilistic games. 2837 | 2838 | The first game: roll a die repeatedly. Stop rolling once you get a five followed 2839 | by a six. Your number of rolls is the amount you pay, in dollars. 2840 | 2841 | The second game: same, except that the stopping condition is a five followed by 2842 | a five. 2843 | 2844 | Which of the two games should Alice elect to play? Does it even matter? Write a 2845 | program to simulate the two games and calculate their expected value. 2846 | 2847 | ## [Problem-179](src/main/java/in/ashwanik/dcp/problems/p151_180/p179):sunny: 2848 | 2849 | 2850 | > This problem was asked by Google. 2851 | 2852 | Given the sequence of keys visited by a postorder traversal of a binary search 2853 | tree, reconstruct the tree. 2854 | 2855 | For example, given the sequence 2, 4, 3, 8, 7, 5, you should construct the 2856 | following tree: 2857 | 2858 | 5 2859 | / \ 2860 | 3 7 2861 | / \ \ 2862 | 2 4 8 2863 | 2864 | 2865 | ## [Problem-180](src/main/java/in/ashwanik/dcp/problems/p151_180/p180):sunny: 2866 | 2867 | 2868 | > This problem was asked by Google. 2869 | 2870 | Given a stack of N elements, interleave the first half of the stack with the 2871 | second half reversed using only one other queue. This should be done in-place. 2872 | 2873 | Recall that you can only push or pop from a stack, and enqueue or dequeue from a 2874 | queue. 2875 | 2876 | For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. 2877 | If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3]. 2878 | 2879 | Hint: Try working backwards from the end state. 2880 | 2881 | ## Problem-181:waxing_crescent_moon: 2882 | 2883 | 2884 | > This problem was asked by Google. 2885 | 2886 | Given a string, split it into as few strings as possible such that each string 2887 | is a palindrome. 2888 | 2889 | For example, given the input string racecarannakayak, return ["racecar", "anna", 2890 | "kayak"]. 2891 | 2892 | Given the input string abc, return ["a", "b", "c"]. 2893 | 2894 | ## [Problem-182](src/main/java/in/ashwanik/dcp/problems/p181_210/p182):sunny: 2895 | 2896 | 2897 | > This problem was asked by Facebook. 2898 | 2899 | A graph is minimally-connected if it is connected and there is no edge that can 2900 | be removed while still leaving the graph connected. For example, any binary tree 2901 | is minimally-connected. 2902 | 2903 | Given an undirected graph, check if the graph is minimally-connected. You can 2904 | choose to represent the graph as either an adjacency matrix or adjacency list. 2905 | 2906 | ## Problem-183:new_moon: 2907 | 2908 | 2909 | > This problem was asked by Twitch. 2910 | 2911 | Describe what happens when you type a URL into your browser and press Enter. 2912 | 2913 | ## [Problem-184](src/main/java/in/ashwanik/dcp/problems/p181_210/p184):sunny: 2914 | 2915 | 2916 | > This problem was asked by Amazon. 2917 | 2918 | Given n numbers, find the greatest common denominator between them. 2919 | 2920 | For example, given the numbers [42, 56, 14], return 14. 2921 | 2922 | ## Problem-185:new_moon: 2923 | 2924 | 2925 | > This problem was asked by Google. 2926 | 2927 | Given two rectangles on a 2D graph, return the area of their intersection. If 2928 | the rectangles don't intersect, return 0. 2929 | 2930 | For example, given the following rectangles: 2931 | 2932 | { 2933 | "top_left": (1, 4), 2934 | "dimensions": (3, 3) # width, height 2935 | } 2936 | 2937 | 2938 | and 2939 | 2940 | { 2941 | "top_left": (0, 5), 2942 | "dimensions" (4, 3) # width, height 2943 | } 2944 | 2945 | 2946 | return 6. 2947 | 2948 | ## Problem-186:waxing_crescent_moon: 2949 | 2950 | 2951 | > This problem was asked by Microsoft. 2952 | 2953 | Given an array of positive integers, divide the array into two subsets such that 2954 | the difference between the sum of the subsets is as small as possible. 2955 | 2956 | For example, given [5, 10, 15, 20, 25], return the sets {10, 25} and {5, 15, 20} 2957 | , which has a difference of 5, which is the smallest possible difference. 2958 | 2959 | ## Problem-187:new_moon: 2960 | 2961 | 2962 | > This problem was asked by Google. 2963 | 2964 | You are given given a list of rectangles represented by min and max x- and 2965 | y-coordinates. Compute whether or not a pair of rectangles overlap each other. 2966 | If one rectangle completely covers another, it is considered overlapping. 2967 | 2968 | For example, given the following rectangles: 2969 | 2970 | { 2971 | "top_left": (1, 4), 2972 | "dimensions": (3, 3) # width, height 2973 | }, 2974 | { 2975 | "top_left": (-1, 3), 2976 | "dimensions": (2, 1) 2977 | }, 2978 | { 2979 | "top_left": (0, 5), 2980 | "dimensions": (4, 3) 2981 | } 2982 | 2983 | 2984 | return true as the first and third rectangle overlap each other. 2985 | 2986 | ## Problem-188:new_moon: 2987 | 2988 | 2989 | > This problem was asked by Google. 2990 | 2991 | What will this code print out? 2992 | 2993 | def make_functions(): 2994 | flist = [] 2995 | 2996 | for i in [1, 2, 3]: 2997 | def print_i(): 2998 | print(i) 2999 | flist.append(print_i) 3000 | 3001 | return flist 3002 | 3003 | functions = make_functions() 3004 | for f in functions: 3005 | f() 3006 | 3007 | 3008 | How can we make it print out what we apparently want? 3009 | 3010 | ## [Problem-189](src/main/java/in/ashwanik/dcp/problems/p181_210/p189):sunny: 3011 | 3012 | 3013 | > This problem was asked by Google. 3014 | 3015 | Given an array of elements, return the length of the longest subarray where all 3016 | its elements are distinct. 3017 | 3018 | For example, given the array [5, 1, 3, 5, 2, 3, 4, 1], return 5 as the longest 3019 | subarray of distinct elements is [5, 2, 3, 4, 1]. 3020 | 3021 | ## [Problem-190](src/main/java/in/ashwanik/dcp/problems/p181_210/p190):sunny: 3022 | 3023 | 3024 | > This problem was asked by Facebook. 3025 | 3026 | Given a circular array, compute its maximum subarray sum in O(n) time. 3027 | 3028 | For example, given [8, -1, 3, 4], return 15 as we choose the numbers 3, 4, and 8 3029 | where the 8 is obtained from wrapping around. 3030 | 3031 | Given [-4, 5, 1, 0], return 6 as we choose the numbers 5 and 1. 3032 | 3033 | ## [Problem-191](src/main/java/in/ashwanik/dcp/problems/p181_210/p191):sunny: 3034 | 3035 | 3036 | > This problem was asked by Stripe. 3037 | 3038 | Given a collection of intervals, find the minimum number of intervals you need 3039 | to remove to make the rest of the intervals non-overlapping. 3040 | 3041 | Intervals can "touch", such as [0, 1] and [1, 2], but they won't be considered 3042 | overlapping. 3043 | 3044 | For example, given the intervals (7, 9), (2, 4), (5, 8), return 1 as the last 3045 | interval can be removed and the first two won't overlap. 3046 | 3047 | The intervals are not necessarily sorted in any order. 3048 | 3049 | ## Problem-192:waxing_crescent_moon: 3050 | 3051 | 3052 | > This problem was asked by Google. 3053 | 3054 | You are given an array of nonnegative integers. Let's say you start at the 3055 | beginning of the array and are trying to advance to the end. You can advance at 3056 | most, the number of steps that you're currently on. Determine whether you can 3057 | get to the end of the array. 3058 | 3059 | For example, given the array [1, 3, 1, 2, 0, 1], we can go from indices 0 -> 1 3060 | -> 3 -> 5, so return true. 3061 | 3062 | Given the array [1, 2, 1, 0, 0], we can't reach the end, so return false. 3063 | 3064 | ## Problem-193:waxing_crescent_moon: 3065 | 3066 | 3067 | > This problem was asked by Affirm. 3068 | 3069 | Given a array of numbers representing the stock prices of a company in 3070 | chronological order, write a function that calculates the maximum profit you 3071 | could have made from buying and selling that stock. You're also given a number 3072 | fee that represents a transaction fee for each buy and sell transaction. 3073 | 3074 | You must buy before you can sell the stock, but you can make as many 3075 | transactions as you like. 3076 | 3077 | For example, given [1, 3, 2, 8, 4, 10] and fee = 2, you should return 9, since 3078 | you could buy the stock at 1 dollar, and sell at 8 dollars, and then buy it at 4 3079 | dollars and sell it at 10 dollars. Since we did two transactions, there is a 4 3080 | dollar fee, so we have 7 + 6 = 13 profit minus 4 dollars of fees. 3081 | 3082 | ## Problem-194:waxing_crescent_moon: 3083 | 3084 | 3085 | > This problem was asked by Facebook. 3086 | 3087 | Suppose you are given two lists of n points, one list p1, p2, ..., pnon the line 3088 | y = 0 and the other list q1, q2, ..., qn on the line y = 1. Imagine a set of n 3089 | line segments connecting each point pi to qi. Write an algorithm to determine 3090 | how many pairs of the line segments intersect. 3091 | 3092 | ## [Problem-195](src/main/java/in/ashwanik/dcp/problems/p181_210/p195):sunny: 3093 | 3094 | 3095 | > This problem was asked by Google. 3096 | 3097 | Let A be an N by M matrix in which every row and every column is sorted. 3098 | 3099 | Given i1, j1, i2, and j2, compute the number of elements of M smaller than M[i1, 3100 | j1] and larger than M[i2, j2]. 3101 | 3102 | For example, given the following matrix: 3103 | 3104 | [[1, 3, 7, 10, 15, 20], 3105 | [2, 6, 9, 14, 22, 25], 3106 | [3, 8, 10, 15, 25, 30], 3107 | [10, 11, 12, 23, 30, 35], 3108 | [20, 25, 30, 35, 40, 45]] 3109 | 3110 | 3111 | And i1 = 1, j1 = 1, i2 = 3, j2 = 3, return 15 as there are 15 numbers in the 3112 | matrix smaller than 6 or greater than 23. 3113 | 3114 | ## [Problem-196](src/main/java/in/ashwanik/dcp/problems/p181_210/p196):sunny: 3115 | 3116 | 3117 | > This problem was asked by Apple. 3118 | 3119 | Given the root of a binary tree, find the most frequent subtree sum. The subtree 3120 | sum of a node is the sum of all values under a node, including the node itself. 3121 | 3122 | For example, given the following tree: 3123 | 3124 | 5 3125 | / \ 3126 | 2 -5 3127 | 3128 | 3129 | Return 2 as it occurs twice: once as the left leaf, and once as the sum of 2 + 5 3130 | - 5. 3131 | 3132 | ## [Problem-197](src/main/java/in/ashwanik/dcp/problems/p181_210/p197):sunny: 3133 | 3134 | 3135 | > This problem was asked by Amazon. 3136 | 3137 | Given an array and a number k that's smaller than the length of the array, 3138 | rotate the array to the right k elements in-place. 3139 | 3140 | ## [Problem-198](src/main/java/in/ashwanik/dcp/problems/p181_210/p198):sunny: 3141 | 3142 | 3143 | > This problem was asked by Google. 3144 | 3145 | Given a set of distinct positive integers, find the largest subset such that 3146 | every pair of elements in the subset (i, j) satisfies either i % j = 0 or j % i 3147 | = 0. 3148 | 3149 | For example, given the set [3, 5, 10, 20, 21], you should return [5, 10, 20]. 3150 | Given [1, 3, 6, 24], return [1, 3, 6, 24]. 3151 | 3152 | ## [Problem-199](src/main/java/in/ashwanik/dcp/problems/p181_210/p199):sunny: 3153 | 3154 | 3155 | > This problem was asked by Facebook. 3156 | 3157 | Given a string of parentheses, find the balanced string that can be produced 3158 | from it using the minimum number of insertions and deletions. If there are 3159 | multiple solutions, return any of them. 3160 | 3161 | For example, given "(()", you could return "(())". Given "))()(", you could 3162 | return "()()()()". 3163 | 3164 | ## Problem-200:waxing_crescent_moon: 3165 | 3166 | 3167 | > This problem was asked by Microsoft. 3168 | 3169 | Let X be a set of n intervals on the real line. We say that a set of points P 3170 | "stabs" X if every interval in X contains at least one point in P. Compute the 3171 | smallest set of points that stabs X. 3172 | 3173 | For example, given the intervals [(1, 4), (4, 5), (7, 9), (9, 12)], you should 3174 | return [4, 9]. 3175 | 3176 | ## [Problem-201](src/main/java/in/ashwanik/dcp/problems/p181_210/p201):sunny: 3177 | 3178 | 3179 | > This problem was asked by Google. 3180 | 3181 | You are given an array of arrays of integers, where each array corresponds to a 3182 | row in a triangle of numbers. For example, [[1], [2, 3], [1, 5, 1]] represents 3183 | the triangle: 3184 | 3185 | 1 3186 | 2 3 3187 | 1 5 1 3188 | 3189 | 3190 | We define a path in the triangle to start at the top and go down one row at a 3191 | time to an adjacent value, eventually ending with an entry on the bottom row. 3192 | For example, 1 -> 3 -> 5. The weight of the path is the sum of the entries. 3193 | 3194 | Write a program that returns the weight of the maximum weight path. 3195 | 3196 | ## [Problem-202](src/main/java/in/ashwanik/dcp/problems/p181_210/p202):sunny: 3197 | 3198 | 3199 | > This problem was asked by Palantir. 3200 | 3201 | Write a program that checks whether an integer is a palindrome. For example, 121 3202 | is a palindrome, as well as 888. 678 is not a palindrome. Do not convert the 3203 | integer into a string. 3204 | 3205 | ## [Problem-203](src/main/java/in/ashwanik/dcp/problems/p181_210/p203):sunny: 3206 | 3207 | 3208 | > This problem was asked by Uber. 3209 | 3210 | Suppose an array sorted in ascending order is rotated at some pivot unknown to 3211 | you beforehand. Find the minimum element in O(log N) time. You may assume the 3212 | array does not contain duplicates. 3213 | 3214 | For example, given [5, 7, 10, 3, 4], return 3. 3215 | 3216 | ## [Problem-204](src/main/java/in/ashwanik/dcp/problems/p181_210/p204):sunny: 3217 | 3218 | 3219 | > This problem was asked by Amazon. 3220 | 3221 | Given a complete binary tree, count the number of nodes in faster than O(n) 3222 | time. Recall that a complete binary tree has every level filled except the last, 3223 | and the nodes in the last level are filled starting from the left. 3224 | 3225 | ## [Problem-205](src/main/java/in/ashwanik/dcp/problems/p181_210/p205):sunny: 3226 | 3227 | 3228 | > This problem was asked by IBM. 3229 | 3230 | Given an integer, find the next permutation of it in absolute order. For 3231 | example, given 48975, the next permutation would be 49578. 3232 | 3233 | ## [Problem-206](src/main/java/in/ashwanik/dcp/problems/p181_210/p206):sunny: 3234 | 3235 | 3236 | > This problem was asked by Twitter. 3237 | 3238 | A permutation can be specified by an array P, where P[i] represents the location 3239 | of the element at i in the permutation. For example, [2, 1, 0] represents the 3240 | permutation where elements at the index 0 and 2 are swapped. 3241 | 3242 | Given an array and a permutation, apply the permutation to the array. For 3243 | example, given the array ["a", "b", "c"] and the permutation [2, 1, 0], return 3244 | ["c", "b", "a"]. 3245 | 3246 | ## Problem-207:waxing_crescent_moon: 3247 | 3248 | 3249 | > This problem was asked by Dropbox. 3250 | 3251 | Given an undirected graph G, check whether it is bipartite. Recall that a graph 3252 | is bipartite if its vertices can be divided into two independent sets, U and V, 3253 | such that no edge connects vertices of the same set. 3254 | 3255 | ## [Problem-208](src/main/java/in/ashwanik/dcp/problems/p181_210/p208):sunny: 3256 | 3257 | 3258 | > This problem was asked by LinkedIn. 3259 | 3260 | Given a linked list of numbers and a pivot k, partition the linked list so that 3261 | all nodes less than k come before nodes greater than or equal to k. 3262 | 3263 | For example, given the linked list 5 -> 1 -> 8 -> 0 -> 3 and k = 3, the solution 3264 | could be 1 -> 0 -> 5 -> 8 -> 3. 3265 | 3266 | ## Problem-209:waxing_crescent_moon: 3267 | 3268 | 3269 | > This problem was asked by YouTube. 3270 | 3271 | Write a program that computes the length of the longest common subsequence of 3272 | three given strings. For example, given "epidemiologist", "refrigeration", and 3273 | "supercalifragilisticexpialodocious", it should return 5, since the longest 3274 | common subsequence is "eieio". 3275 | 3276 | ## [Problem-210](src/main/java/in/ashwanik/dcp/problems/p181_210/p210):sunny: 3277 | 3278 | 3279 | > This problem was asked by Apple. 3280 | 3281 | A Collatz sequence in mathematics can be defined as follows. Starting with any 3282 | positive integer: 3283 | 3284 | * if n is even, the next number in the sequence is n / 2 3285 | * if n is odd, the next number in the sequence is 3n + 1 3286 | 3287 | It is conjectured that every such sequence eventually reaches the number 1. Test 3288 | this conjecture. 3289 | 3290 | Bonus: What input n <= 1000000 gives the longest sequence? 3291 | 3292 | ## Problem-211:waxing_crescent_moon: 3293 | 3294 | 3295 | > This problem was asked by Microsoft. 3296 | 3297 | Given a string and a pattern, find the starting indices of all occurrences of 3298 | the pattern in the string. For example, given the string "abracadabra" and the 3299 | pattern "abr", you should return [0, 7]. 3300 | 3301 | ## [Problem-212](src/main/java/in/ashwanik/dcp/problems/p211_240/p212):sunny: 3302 | 3303 | 3304 | > This problem was asked by Dropbox. 3305 | 3306 | Spreadsheets often use this alphabetical encoding for its columns: "A", "B", 3307 | "C", ..., "AA", "AB", ..., "ZZ", "AAA", "AAB", .... 3308 | 3309 | Given a column number, return its alphabetical column id. For example, given 1, 3310 | return "A". Given 27, return "AA". 3311 | 3312 | ## [Problem-213](src/main/java/in/ashwanik/dcp/problems/p211_240/p213):sunny: 3313 | 3314 | 3315 | > This problem was asked by Snapchat. 3316 | 3317 | Given a string of digits, generate all possible valid IP address combinations. 3318 | 3319 | IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers 3320 | between 0 and 255. Zero-prefixed numbers, such as 01 and 065, are not allowed, 3321 | except for 0 itself. 3322 | 3323 | For example, given "2542540123", you should return ['254.25.40.123', 3324 | '254.254.0.123']. 3325 | 3326 | ## [Problem-214](src/main/java/in/ashwanik/dcp/problems/p211_240/p214):sunny: 3327 | 3328 | 3329 | > This problem was asked by Stripe. 3330 | 3331 | Given an integer n, return the length of the longest consecutive run of 1s in 3332 | its binary representation. 3333 | 3334 | For example, given 156, you should return 3. 3335 | 3336 | ## [Problem-215](src/main/java/in/ashwanik/dcp/problems/p211_240/p215):sunny: 3337 | 3338 | 3339 | > This problem was asked by Yelp. 3340 | 3341 | The horizontal distance of a binary tree node describes how far left or right 3342 | the node will be when the tree is printed out. 3343 | 3344 | More rigorously, we can define it as follows: 3345 | 3346 | * The horizontal distance of the root is 0. 3347 | * The horizontal distance of a left child is hd(parent) - 1. 3348 | * The horizontal distance of a right child is hd(parent) + 1. 3349 | 3350 | For example, for the following tree, hd(1) = -2, and hd(6) = 0. 3351 | 3352 | 5 3353 | / \ 3354 | 3 7 3355 | / \ / \ 3356 | 1 4 6 9 3357 | / / 3358 | 0 8 3359 | 3360 | 3361 | The bottom view of a tree, then, consists of the lowest node at each horizontal 3362 | distance. If there are two nodes at the same depth and horizontal distance, 3363 | either is acceptable. 3364 | 3365 | For this tree, for example, the bottom view could be [0, 1, 3, 6, 8, 9]. 3366 | 3367 | Given the root to a binary tree, return its bottom view. 3368 | 3369 | ## [Problem-216](src/main/java/in/ashwanik/dcp/problems/p211_240/p216):sunny: 3370 | 3371 | 3372 | > This problem was asked by Facebook. 3373 | 3374 | Given a number in Roman numeral [https://en.wikipedia.org/wiki/Roman_numerals] 3375 | format, convert it to decimal. 3376 | 3377 | The values of Roman numerals are as follows: 3378 | 3379 | { 3380 | 'M': 1000, 3381 | 'D': 500, 3382 | 'C': 100, 3383 | 'L': 50, 3384 | 'X': 10, 3385 | 'V': 5, 3386 | 'I': 1 3387 | } 3388 | 3389 | 3390 | In addition, note that the Roman numeral system uses subtractive notation 3391 | [https://en.wikipedia.org/wiki/Subtractive_notation] for numbers such as IV and 3392 | XL. 3393 | 3394 | For the input XIV, for instance, you should return 14. 3395 | 3396 | ## Problem-217:waxing_crescent_moon: 3397 | 3398 | 3399 | > This problem was asked by Oracle. 3400 | 3401 | We say a number is sparse if there are no adjacent ones in its binary 3402 | representation. For example, 21 (10101) is sparse, but 22 (10110) is not. For a 3403 | given input N, find the smallest sparse number greater than or equal to N. 3404 | 3405 | Do this in faster than O(N log N) time. 3406 | 3407 | ## Problem-218:waxing_crescent_moon: 3408 | 3409 | 3410 | > This problem was asked by Yahoo. 3411 | 3412 | Write an algorithm that computes the reversal of a directed graph. For example, 3413 | if a graph consists of A -> B -> C, it should become A <- B <- C. 3414 | 3415 | ## Problem-219:waxing_crescent_moon: 3416 | 3417 | 3418 | > This problem was asked by Salesforce. 3419 | 3420 | Connect 4 is a game where opponents take turns dropping red or black discs into 3421 | a 7 x 6 vertically suspended grid. The game ends either when one player creates 3422 | a line of four consecutive discs of their color (horizontally, vertically, or 3423 | diagonally), or when there are no more spots left in the grid. 3424 | 3425 | Design and implement Connect 4. 3426 | 3427 | ## Problem-220:waxing_crescent_moon: 3428 | 3429 | 3430 | > This problem was asked by Square. 3431 | 3432 | In front of you is a row of N coins, with values v1, v1, ..., vn. 3433 | 3434 | You are asked to play the following game. You and an opponent take turns 3435 | choosing either the first or last coin from the row, removing it from the row, 3436 | and receiving the value of the coin. 3437 | 3438 | Write a program that returns the maximum amount of money you can win with 3439 | certainty, if you move first, assuming your opponent plays optimally. 3440 | 3441 | ## Problem-221:waxing_crescent_moon: 3442 | 3443 | 3444 | > This problem was asked by Zillow. 3445 | 3446 | Let's define a "sevenish" number to be one which is either a power of 7, or the 3447 | sum of unique powers of 7. The first few sevenish numbers are 1, 7, 8, 49, and 3448 | so on. Create an algorithm to find the nth sevenish number. 3449 | 3450 | ## Problem-222:waxing_crescent_moon: 3451 | 3452 | 3453 | > This problem was asked by Quora. 3454 | 3455 | Given an absolute pathname that may have . or .. as part of it, return the 3456 | shortest standardized path. 3457 | 3458 | For example, given "/usr/bin/../bin/./scripts/../", return "/usr/bin/". 3459 | 3460 | ## Problem-223:waxing_crescent_moon: 3461 | 3462 | 3463 | > This problem was asked by Palantir. 3464 | 3465 | Typically, an implementation of in-order traversal of a binary tree has O(h) 3466 | space complexity, where h is the height of the tree. Write a program to compute 3467 | the in-order traversal of a binary tree using O(1) space. 3468 | 3469 | ## Problem-224:waxing_crescent_moon: 3470 | 3471 | 3472 | > This problem was asked by Amazon. 3473 | 3474 | Given a sorted array, find the smallest positive integer that is not the sum of 3475 | a subset of the array. 3476 | 3477 | For example, for the input [1, 2, 3, 10], you should return 7. 3478 | 3479 | Do this in O(N) time. 3480 | 3481 | ## Problem-225:waxing_crescent_moon: 3482 | 3483 | 3484 | > This problem was asked by Bloomberg. 3485 | 3486 | There are N prisoners standing in a circle, waiting to be executed. The 3487 | executions are carried out starting with the kth person, and removing every 3488 | successive kth person going clockwise until there is no one left. 3489 | 3490 | Given N and k, write an algorithm to determine where a prisoner should stand in 3491 | order to be the last survivor. 3492 | 3493 | For example, if N = 5 and k = 2, the order of executions would be [2, 4, 1, 5, 3494 | 3], so you should return 3. 3495 | 3496 | Bonus: Find an O(log N) solution if k = 2. 3497 | 3498 | ## Problem-226:waxing_crescent_moon: 3499 | 3500 | 3501 | > This problem was asked by Airbnb. 3502 | 3503 | You come across a dictionary of sorted words in a language you've never seen 3504 | before. Write a program that returns the correct order of letters in this 3505 | language. 3506 | 3507 | For example, given ['xww', 'wxyz', 'wxyw', 'ywx', 'ywz'], you should return 3508 | ['x', 'z', 'w', 'y']. 3509 | 3510 | ## Problem-227:waxing_crescent_moon: 3511 | 3512 | 3513 | > This problem was asked by Facebook. 3514 | 3515 | Boggle is a game played on a 4 x 4 grid of letters. The goal is to find as many 3516 | words as possible that can be formed by a sequence of adjacent letters in the 3517 | grid, using each cell at most once. Given a game board and a dictionary of valid 3518 | words, implement a Boggle solver. 3519 | 3520 | ## Problem-228:waxing_crescent_moon: 3521 | 3522 | 3523 | > This problem was asked by Twitter. 3524 | 3525 | Given a list of numbers, create an algorithm that arranges them in order to form 3526 | the largest possible integer. For example, given [10, 7, 76, 415], you should 3527 | return 77641510. 3528 | 3529 | ## Problem-229:waxing_crescent_moon: 3530 | 3531 | 3532 | > This problem was asked by Flipkart. 3533 | 3534 | Snakes and Ladders [https://en.wikipedia.org/wiki/Snakes_and_Ladders] is a game 3535 | played on a 10 x 10 board, the goal of which is get from square 1 to square 100. 3536 | On each turn players will roll a six-sided die and move forward a number of 3537 | spaces equal to the result. If they land on a square that represents a snake or 3538 | ladder, they will be transported ahead or behind, respectively, to a new square. 3539 | 3540 | Find the smallest number of turns it takes to play snakes and ladders. 3541 | 3542 | For convenience, here are the squares representing snakes and ladders, and their 3543 | outcomes: 3544 | 3545 | snakes = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98: 78} 3546 | ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100} 3547 | 3548 | 3549 | ## Problem-230:waxing_crescent_moon: 3550 | 3551 | 3552 | > This problem was asked by Goldman Sachs. 3553 | 3554 | You are given N identical eggs and access to a building with k floors. Your task 3555 | is to find the lowest floor that will cause an egg to break, if dropped from 3556 | that floor. Once an egg breaks, it cannot be dropped again. If an egg breaks 3557 | when dropped from the xth floor, you can assume it will also break when dropped 3558 | from any floor greater than x. 3559 | 3560 | Write an algorithm that finds the minimum number of trial drops it will take, in 3561 | the worst case, to identify this floor. 3562 | 3563 | For example, if N = 1 and k = 5, we will need to try dropping the egg at every 3564 | floor, beginning with the first, until we reach the fifth floor, so our solution 3565 | will be 5. 3566 | 3567 | ## Problem-231:waxing_crescent_moon: 3568 | 3569 | 3570 | > This problem was asked by IBM. 3571 | 3572 | Given a string with repeated characters, rearrange the string so that no two 3573 | adjacent characters are the same. If this is not possible, return None. 3574 | 3575 | For example, given "aaabbc", you could return "ababac". Given "aaab", return 3576 | None. 3577 | 3578 | ## Problem-232:waxing_crescent_moon: 3579 | 3580 | 3581 | > This problem was asked by Google. 3582 | 3583 | Implement a PrefixMapSum class with the following methods: 3584 | 3585 | * insert(key: str, value: int): Set a given key's value in the map. If the key 3586 | already exists, overwrite the value. 3587 | * sum(prefix: str): Return the sum of all values of keys that begin with a 3588 | given prefix. 3589 | 3590 | For example, you should be able to run the following code: 3591 | 3592 | mapsum.insert("columnar", 3) 3593 | assert mapsum.sum("col") == 3 3594 | 3595 | mapsum.insert("column", 2) 3596 | assert mapsum.sum("col") == 5 3597 | 3598 | 3599 | ## Problem-233:waxing_crescent_moon: 3600 | 3601 | 3602 | > This problem was asked by Apple. 3603 | 3604 | Implement the function fib(n), which returns the nth number in the Fibonacci 3605 | sequence, using only O(1) space. 3606 | 3607 | ## Problem-234:waxing_crescent_moon: 3608 | 3609 | 3610 | > This problem was asked by Microsoft. 3611 | 3612 | Recall that the minimum spanning tree is the subset of edges of a tree that 3613 | connect all its vertices with the smallest possible total edge weight. Given an 3614 | undirected graph with weighted edges, compute the maximum weight spanning tree. 3615 | 3616 | ## Problem-235:waxing_crescent_moon: 3617 | 3618 | 3619 | > This problem was asked by Facebook. 3620 | 3621 | Given an array of numbers of length N, find both the minimum and maximum using 3622 | less than 2 * (N - 2) comparisons. 3623 | 3624 | ## Problem-236:waxing_crescent_moon: 3625 | 3626 | 3627 | > This problem was asked by Nvidia. 3628 | 3629 | You are given a list of N points (x1, y1), (x2, y2), ..., (xN, yN) representing 3630 | a polygon. You can assume these points are given in order; that is, you can 3631 | construct the polygon by connecting point 1 to point 2, point 2 to point 3, and 3632 | so on, finally looping around to connect point N to point 1. 3633 | 3634 | Determine if a new point p lies inside this polygon. (If p is on the boundary of 3635 | the polygon, you should return False). 3636 | 3637 | ## Problem-237:waxing_crescent_moon: 3638 | 3639 | 3640 | > This problem was asked by Amazon. 3641 | 3642 | A tree is symmetric if its data and shape remain unchanged when it is reflected 3643 | about the root node. The following tree is an example: 3644 | 3645 | 4 3646 | / | \ 3647 | 3 5 3 3648 | / \ 3649 | 9 9 3650 | 3651 | 3652 | Given a k-ary tree, determine whether it is symmetric. 3653 | 3654 | ## Problem-238:waxing_crescent_moon: 3655 | 3656 | 3657 | > This problem was asked by MIT. 3658 | 3659 | Blackjack [https://en.wikipedia.org/wiki/Blackjack] is a two player card game 3660 | whose rules are as follows: 3661 | 3662 | * The player and then the dealer are each given two cards. 3663 | * The player can then "hit", or ask for arbitrarily many additional cards, so 3664 | long as their total does not exceed 21. 3665 | * The dealer must then hit if their total is 16 or lower, otherwise pass. 3666 | * Finally, the two compare totals, and the one with the greatest sum not 3667 | exceeding 21 is the winner. 3668 | 3669 | For this problem, cards values are counted as follows: each card between 2 and 3670 | 10 counts as their face value, face cards count as 10, and aces count as 1. 3671 | 3672 | Given perfect knowledge of the sequence of cards in the deck, implement a 3673 | blackjack solver that maximizes the player's score (that is, wins minus losses). 3674 | 3675 | ## Problem-239:waxing_crescent_moon: 3676 | 3677 | 3678 | > This problem was asked by Uber. 3679 | 3680 | One way to unlock an Android phone is through a pattern of swipes across a 1-9 3681 | keypad. 3682 | 3683 | For a pattern to be valid, it must satisfy the following: 3684 | 3685 | * All of its keys must be distinct. 3686 | * It must not connect two keys by jumping over a third key, unless that key has 3687 | already been used. 3688 | 3689 | For example, 4 - 2 - 1 - 7 is a valid pattern, whereas 2 - 1 - 7 is not. 3690 | 3691 | Find the total number of valid unlock patterns of length N, where 1 <= N <= 9. 3692 | 3693 | ## Problem-240:waxing_crescent_moon: 3694 | 3695 | 3696 | > This problem was asked by Spotify. 3697 | 3698 | There are N couples sitting in a row of length 2 * N. They are currently ordered 3699 | randomly, but would like to rearrange themselves so that each couple's partners 3700 | can sit side by side. 3701 | 3702 | What is the minimum number of swaps necessary for this to happen? 3703 | 3704 | ## Problem-241:waxing_crescent_moon: 3705 | 3706 | 3707 | > This problem was asked by Palantir. 3708 | 3709 | In academia, the h-index is a metric used to calculate the impact of a 3710 | researcher's papers. It is calculated as follows: 3711 | 3712 | A researcher has index h if at least h of her N papers have h citations each. If 3713 | there are multiple h satisfying this formula, the maximum is chosen. 3714 | 3715 | For example, suppose N = 5, and the respective citations of each paper are [4, 3716 | 3, 0, 1, 5]. Then the h-index would be 3, since the researcher has 3 papers with 3717 | at least 3 citations. 3718 | 3719 | Given a list of paper citations of a researcher, calculate their h-index. 3720 | 3721 | ## Problem-242:waxing_crescent_moon: 3722 | 3723 | 3724 | > This problem was asked by Twitter. 3725 | 3726 | You are given an array of length 24, where each element represents the number of 3727 | new subscribers during the corresponding hour. Implement a data structure that 3728 | efficiently supports the following: 3729 | 3730 | * update(hour: int, value: int): Increment the element at index hour by value. 3731 | * query(start: int, end: int): Retrieve the number of subscribers that have 3732 | signed up between start and end (inclusive). 3733 | 3734 | You can assume that all values get cleared at the end of the day, and that you 3735 | will not be asked for start and end values that wrap around midnight. 3736 | 3737 | ## Problem-243:waxing_crescent_moon: 3738 | 3739 | 3740 | > This problem was asked by Etsy. 3741 | 3742 | Given an array of numbers N and an integer k, your task is to split N into k 3743 | partitions such that the maximum sum of any partition is minimized. Return this 3744 | sum. 3745 | 3746 | For example, given N = [5, 1, 2, 7, 3, 4] and k = 3, you should return 8, since 3747 | the optimal partition is [5, 1, 2], [7], [3, 4]. 3748 | 3749 | ## Problem-244:waxing_crescent_moon: 3750 | 3751 | 3752 | > This problem was asked by Square. 3753 | 3754 | The Sieve of Eratosthenes is an algorithm used to generate all prime numbers 3755 | smaller than N. The method is to take increasingly larger prime numbers, and 3756 | mark their multiples as composite. 3757 | 3758 | For example, to find all primes less than 100, we would first mark [4, 6, 8, 3759 | ...] (multiples of two), then [6, 9, 12, ...] (multiples of three), and so on. 3760 | Once we have done this for all primes less than N, the unmarked numbers that 3761 | remain will be prime. 3762 | 3763 | Implement this algorithm. 3764 | 3765 | Bonus: Create a generator that produces primes indefinitely (that is, without 3766 | taking N as an input). 3767 | 3768 | ## Problem-245:waxing_crescent_moon: 3769 | 3770 | 3771 | > This problem was asked by Yelp. 3772 | 3773 | You are given an array of integers, where each element represents the maximum 3774 | number of steps that can be jumped going forward from that element. Write a 3775 | function to return the minimum number of jumps you must take in order to get 3776 | from the start to the end of the array. 3777 | 3778 | For example, given [6, 2, 4, 0, 5, 1, 1, 4, 2, 9], you should return 2, as the 3779 | optimal solution involves jumping from 6 to 5, and then from 5 to 9. 3780 | 3781 | ## Problem-246:waxing_crescent_moon: 3782 | 3783 | 3784 | > This problem was asked by Dropbox. 3785 | 3786 | Given a list of words, determine whether the words can be chained to form a 3787 | circle. A word X can be placed in front of another word Y in a circle if the 3788 | last character of X is same as the first character of Y. 3789 | 3790 | For example, the words ['chair', 'height', 'racket', touch', 'tunic'] can form 3791 | the following circle: chair --> racket --> touch --> height --> tunic --> chair. 3792 | 3793 | ## Problem-247:waxing_crescent_moon: 3794 | 3795 | 3796 | > This problem was asked by PayPal. 3797 | 3798 | Given a binary tree, determine whether or not it is height-balanced. A 3799 | height-balanced binary tree can be defined as one in which the heights of the 3800 | two subtrees of any node never differ by more than one. 3801 | 3802 | ## Problem-248:waxing_crescent_moon: 3803 | 3804 | 3805 | > This problem was asked by Nvidia. 3806 | 3807 | Find the maximum of two numbers without using any if-else statements, branching, 3808 | or direct comparisons. 3809 | 3810 | ## Problem-249:waxing_crescent_moon: 3811 | 3812 | 3813 | > This problem was asked by Salesforce. 3814 | 3815 | Given an array of integers, find the maximum XOR of any two elements. 3816 | 3817 | ## Problem-250:waxing_crescent_moon: 3818 | 3819 | 3820 | > This problem was asked by Google. 3821 | 3822 | A cryptarithmetic puzzle is a mathematical game where the digits of some numbers 3823 | are represented by letters. Each letter represents a unique digit. 3824 | 3825 | For example, a puzzle of the form: 3826 | 3827 | SEND 3828 | + MORE 3829 | -------- 3830 | MONEY 3831 | 3832 | 3833 | may have the solution: 3834 | 3835 | {'S': 9, 'E': 5, 'N': 6, 'D': 7, 'M': 1, 'O', 0, 'R': 8, 'Y': 2} 3836 | 3837 | 3838 | Given a three-word puzzle like the one above, create an algorithm that finds a 3839 | solution. 3840 | 3841 | ## Problem-251:waxing_crescent_moon: 3842 | 3843 | 3844 | > This problem was asked by Amazon. 3845 | 3846 | Given an array of a million integers between zero and a billion, out of order, 3847 | how can you efficiently sort it? Assume that you cannot store an array of a 3848 | billion elements in memory. 3849 | 3850 | ## Problem-252:waxing_crescent_moon: 3851 | 3852 | 3853 | > This problem was asked by Palantir. 3854 | 3855 | The ancient Egyptians used to express fractions as a sum of several terms where 3856 | each numerator is one. For example, 4 / 13 can be represented as 1 / 4 + 1 / 18 3857 | + 1 / 468. 3858 | 3859 | Create an algorithm to turn an ordinary fraction a / b, where a < b, into an 3860 | Egyptian fraction. 3861 | 3862 | 3863 | -------------------------------------------------------------------------------- 3864 | 3865 | ## Problem-253:waxing_crescent_moon: 3866 | 3867 | 3868 | > This problem was asked by PayPal. 3869 | 3870 | Given a string and a number of lines k, print the string in zigzag form. In 3871 | zigzag, characters are printed out diagonally from top left to bottom right 3872 | until reaching the kth line, then back up to top right, and so on. 3873 | 3874 | For example, given the sentence "thisisazigzag" and k = 4, you should print: 3875 | 3876 | t a g 3877 | h s z a 3878 | i i i z 3879 | s g 3880 | 3881 | 3882 | 3883 | -------------------------------------------------------------------------------- 3884 | 3885 | ## Problem-254:waxing_crescent_moon: 3886 | 3887 | 3888 | > This problem was asked by Yahoo. 3889 | 3890 | Recall that a full binary tree is one in which each node is either a leaf node, 3891 | or has two children. Given a binary tree, convert it to a full one by removing 3892 | nodes with only one child. 3893 | 3894 | For example, given the following tree: 3895 | 3896 | 0 3897 | / \ 3898 | 1 2 3899 | / \ 3900 | 3 4 3901 | \ / \ 3902 | 5 6 7 3903 | 3904 | 3905 | You should convert it to: 3906 | 3907 | 0 3908 | / \ 3909 | 5 4 3910 | / \ 3911 | 6 7 3912 | 3913 | 3914 | 3915 | -------------------------------------------------------------------------------- 3916 | 3917 | ## Problem-255:waxing_crescent_moon: 3918 | 3919 | 3920 | > This problem was asked by Microsoft. 3921 | 3922 | The transitive closure of a graph is a measure of which vertices are reachable 3923 | from other vertices. It can be represented as a matrix M, where M[i][j] == 1 if 3924 | there is a path between vertices i and j, and otherwise 0. 3925 | 3926 | For example, suppose we are given the following graph in adjacency list form: 3927 | 3928 | graph = [ 3929 | [0, 1, 3], 3930 | [1, 2], 3931 | [2], 3932 | [3] 3933 | ] 3934 | 3935 | 3936 | The transitive closure of this graph would be: 3937 | 3938 | [1, 1, 1, 1] 3939 | [0, 1, 1, 0] 3940 | [0, 0, 1, 0] 3941 | [0, 0, 0, 1] 3942 | 3943 | 3944 | Given a graph, find its transitive closure. 3945 | 3946 | 3947 | -------------------------------------------------------------------------------- 3948 | 3949 | ## Problem-256:waxing_crescent_moon: 3950 | 3951 | 3952 | > This problem was asked by Fitbit. 3953 | 3954 | Given a linked list, rearrange the node values such that they appear in 3955 | alternating low -> high -> low -> high ... form. For example, given 1 -> 2 -> 3 3956 | -> 4 -> 5, you should return 1 -> 3 -> 2 -> 5 -> 4. 3957 | 3958 | 3959 | -------------------------------------------------------------------------------- 3960 | 3961 | ## Problem-257:waxing_crescent_moon: 3962 | 3963 | 3964 | > This problem was asked by WhatsApp. 3965 | 3966 | Given an array of integers out of order, determine the bounds of the smallest 3967 | window that must be sorted in order for the entire array to be sorted. For 3968 | example, given [3, 7, 5, 6, 9], you should return (1, 3). 3969 | 3970 | 3971 | -------------------------------------------------------------------------------- 3972 | 3973 | ## Problem-258:waxing_crescent_moon: 3974 | 3975 | 3976 | > This problem was asked by Morgan Stanley. 3977 | 3978 | In Ancient Greece, it was common to write text with the first line going left to 3979 | right, the second line going right to left, and continuing to go back and forth. 3980 | This style was called "boustrophedon". 3981 | 3982 | Given a binary tree, write an algorithm to print the nodes in boustrophedon 3983 | order. 3984 | 3985 | For example, given the following tree: 3986 | 3987 | 1 3988 | / \ 3989 | 2 3 3990 | / \ / \ 3991 | 4 5 6 7 3992 | 3993 | 3994 | You should return [1, 3, 2, 4, 5, 6, 7]. 3995 | 3996 | 3997 | -------------------------------------------------------------------------------- 3998 | 3999 | ## Problem-259:waxing_crescent_moon: 4000 | 4001 | 4002 | > This problem was asked by Two Sigma. 4003 | 4004 | Ghost is a two-person word game where players alternate appending letters to a 4005 | word. The first person who spells out a word, or creates a prefix for which 4006 | there is no possible continuation, loses. Here is a sample game: 4007 | 4008 | * Player 1: g 4009 | * Player 2: h 4010 | * Player 1: o 4011 | * Player 2: s 4012 | * Player 1: t [loses] 4013 | 4014 | Given a dictionary of words, determine the letters the first player should start 4015 | with, such that with optimal play they cannot lose. 4016 | 4017 | For example, if the dictionary is ["cat", "calf", "dog", "bear"], the only 4018 | winning start letter would be b. 4019 | 4020 | 4021 | -------------------------------------------------------------------------------- 4022 | 4023 | ## Problem-260:waxing_crescent_moon: 4024 | 4025 | 4026 | > This problem was asked by Pinterest. 4027 | 4028 | The sequence [0, 1, ..., N] has been jumbled, and the only clue you have for its 4029 | order is an array representing whether each number is larger or smaller than the 4030 | last. Given this information, reconstruct an array that is consistent with it. 4031 | For example, given [None, +, +, -, +], you could return [1, 2, 3, 0, 4]. 4032 | 4033 | 4034 | -------------------------------------------------------------------------------- 4035 | 4036 | ## Problem-261:waxing_crescent_moon: 4037 | 4038 | 4039 | > This problem was asked by Amazon. 4040 | 4041 | Huffman coding is a method of encoding characters based on their frequency. Each 4042 | letter is assigned a variable-length binary string, such as 0101 or 111110, 4043 | where shorter lengths correspond to more common letters. To accomplish this, a 4044 | binary tree is built such that the path from the root to any leaf uniquely maps 4045 | to a character. When traversing the path, descending to a left child corresponds 4046 | to a 0 in the prefix, while descending right corresponds to 1. 4047 | 4048 | Here is an example tree (note that only the leaf nodes have letters): 4049 | 4050 | * 4051 | / \ 4052 | * * 4053 | / \ / \ 4054 | * a t * 4055 | / \ 4056 | c s 4057 | 4058 | 4059 | With this encoding, cats would be represented as 0000110111. 4060 | 4061 | Given a dictionary of character frequencies, build a Huffman tree, and use it to 4062 | determine a mapping between characters and their encoded binary strings. 4063 | 4064 | 4065 | -------------------------------------------------------------------------------- 4066 | 4067 | ## Problem-262:waxing_crescent_moon: 4068 | 4069 | 4070 | > This problem was asked by Mozilla. 4071 | 4072 | A bridge in a connected (undirected) graph is an edge that, if removed, causes 4073 | the graph to become disconnected. Find all the bridges in a graph. 4074 | 4075 | 4076 | -------------------------------------------------------------------------------- 4077 | 4078 | ## Problem-263:waxing_crescent_moon: 4079 | 4080 | 4081 | > This problem was asked by Nest. 4082 | 4083 | Create a basic sentence checker that takes in a stream of characters and 4084 | determines whether they form valid sentences. If a sentence is valid, the 4085 | program should print it out. 4086 | 4087 | We can consider a sentence valid if it conforms to the following rules: 4088 | 4089 | 1. The sentence must start with a capital letter, followed by a lowercase 4090 | letter or a space. 4091 | 2. All other characters must be lowercase letters, separators (,,;,:) or 4092 | terminal marks (.,?,!,‽). 4093 | 3. There must be a single space between each word. 4094 | 4. The sentence must end with a terminal mark immediately following a word. 4095 | 4096 | 4097 | -------------------------------------------------------------------------------- 4098 | 4099 | ## Problem-264:waxing_crescent_moon: 4100 | 4101 | 4102 | > This problem was asked by LinkedIn. 4103 | 4104 | Given a set of characters C and an integer k, a De Bruijn sequence is a cyclic 4105 | sequence in which every possible k-length string of characters in C occurs 4106 | exactly once. 4107 | 4108 | For example, suppose C = {0, 1} and k = 3. Then our sequence should contain the 4109 | substrings {'000', '001', '010', '011', '100', '101', '110', '111'}, and one 4110 | possible solution would be 00010111. 4111 | 4112 | Create an algorithm that finds a De Bruijn sequence. 4113 | 4114 | 4115 | -------------------------------------------------------------------------------- 4116 | 4117 | ## Problem-265:waxing_crescent_moon: 4118 | 4119 | 4120 | > This problem was asked by Atlassian. 4121 | 4122 | MegaCorp wants to give bonuses to its employees based on how many lines of codes 4123 | they have written. They would like to give the smallest positive amount to each 4124 | worker consistent with the constraint that if a developer has written more lines 4125 | of code than their neighbor, they should receive more money. 4126 | 4127 | Given an array representing a line of seats of employees at MegaCorp, determine 4128 | how much each one should get paid. 4129 | 4130 | For example, given [10, 40, 200, 1000, 60, 30], you should return [1, 2, 3, 4, 4131 | 2, 1]. 4132 | 4133 | 4134 | -------------------------------------------------------------------------------- 4135 | 4136 | ## Problem-266:waxing_crescent_moon: 4137 | 4138 | 4139 | > This problem was asked by Pivotal. 4140 | 4141 | A step word is formed by taking a given word, adding a letter, and anagramming 4142 | the result. For example, starting with the word "APPLE", you can add an "A" and 4143 | anagram to get "APPEAL". 4144 | 4145 | Given a dictionary of words and an input word, create a function that returns 4146 | all valid step words. 4147 | 4148 | 4149 | -------------------------------------------------------------------------------- 4150 | 4151 | ## Problem-267:waxing_crescent_moon: 4152 | 4153 | 4154 | > This problem was asked by Oracle. 4155 | 4156 | You are presented with an 8 by 8 matrix representing the positions of pieces on 4157 | a chess board. The only pieces on the board are the black king and various white 4158 | pieces. Given this matrix, determine whether the king is in check. 4159 | 4160 | For details on how each piece moves, see here 4161 | [https://en.wikipedia.org/wiki/Chess_piece#Moves_of_the_pieces]. 4162 | 4163 | For example, given the following matrix: 4164 | 4165 | ...K.... 4166 | ........ 4167 | .B...... 4168 | ......P. 4169 | .......R 4170 | ..N..... 4171 | ........ 4172 | .....Q.. 4173 | 4174 | 4175 | You should return True, since the bishop is attacking the king diagonally. 4176 | 4177 | 4178 | -------------------------------------------------------------------------------- 4179 | 4180 | ## Problem-268:waxing_crescent_moon: 4181 | 4182 | 4183 | > This problem was asked by Indeed. 4184 | 4185 | Given a 32-bit positive integer N, determine whether it is a power of four in 4186 | faster than O(log N) time. 4187 | 4188 | 4189 | -------------------------------------------------------------------------------- 4190 | 4191 | ## Problem-269:waxing_crescent_moon: 4192 | 4193 | 4194 | > This problem was asked by Microsoft. 4195 | 4196 | You are given an string representing the initial conditions of some dominoes. 4197 | Each element can take one of three values: 4198 | 4199 | * L, meaning the domino has just been pushed to the left, 4200 | * R, meaning the domino has just been pushed to the right, or 4201 | * ., meaning the domino is standing still. 4202 | 4203 | Determine the orientation of each tile when the dominoes stop falling. Note that 4204 | if a domino receives a force from the left and right side simultaneously, it 4205 | will remain upright. 4206 | 4207 | For example, given the string .L.R....L, you should return LL.RRRLLL. 4208 | 4209 | Given the string ..R...L.L, you should return ..RR.LLLL. 4210 | 4211 | 4212 | -------------------------------------------------------------------------------- 4213 | 4214 | ## Problem-270:waxing_crescent_moon: 4215 | 4216 | 4217 | > This problem was asked by Twitter. 4218 | 4219 | A network consists of nodes labeled 0 to N. You are given a list of edges (a, b, 4220 | t), describing the time t it takes for a message to be sent from node a to node 4221 | b. Whenever a node receives a message, it immediately passes the message on to a 4222 | neighboring node, if possible. 4223 | 4224 | Assuming all nodes are connected, determine how long it will take for every node 4225 | to receive a message that begins at node 0. 4226 | 4227 | For example, given N = 5, and the following edges: 4228 | 4229 | edges = [ 4230 | (0, 1, 5), 4231 | (0, 2, 3), 4232 | (0, 5, 4), 4233 | (1, 3, 8), 4234 | (2, 3, 1), 4235 | (3, 5, 10), 4236 | (3, 4, 5) 4237 | ] 4238 | 4239 | 4240 | You should return 9, because propagating the message from 0 -> 2 -> 3 -> 4 will 4241 | take that much time. 4242 | 4243 | 4244 | -------------------------------------------------------------------------------- 4245 | 4246 | ## Problem-271:waxing_crescent_moon: 4247 | 4248 | 4249 | > This problem was asked by Netflix. 4250 | 4251 | Given a sorted list of integers of length N, determine if an element x is in the 4252 | list without performing any multiplication, division, or bit-shift operations. 4253 | 4254 | Do this in O(log N) time. 4255 | 4256 | 4257 | -------------------------------------------------------------------------------- 4258 | 4259 | ## Problem-272:waxing_crescent_moon: 4260 | 4261 | 4262 | > This problem was asked by Spotify. 4263 | 4264 | Write a function, throw_dice(N, faces, total), that determines how many ways it 4265 | is possible to throw N dice with some number of faces each to get a specific 4266 | total. 4267 | 4268 | For example, throw_dice(3, 6, 7) should equal 15. 4269 | 4270 | 4271 | -------------------------------------------------------------------------------- 4272 | 4273 | ## Problem-273:waxing_crescent_moon: 4274 | 4275 | 4276 | > This problem was asked by Apple. 4277 | 4278 | A fixed point in an array is an element whose value is equal to its index. Given 4279 | a sorted array of distinct elements, return a fixed point, if one exists. 4280 | Otherwise, return False. 4281 | 4282 | For example, given [-6, 0, 2, 40], you should return 2. Given [1, 5, 7, 8], you 4283 | should return False. 4284 | 4285 | 4286 | -------------------------------------------------------------------------------- 4287 | 4288 | ## Problem-274:waxing_crescent_moon: 4289 | 4290 | 4291 | > This problem was asked by Facebook. 4292 | 4293 | Given a string consisting of parentheses, single digits, and positive and 4294 | negative signs, convert the string into a mathematical expression to obtain the 4295 | answer. 4296 | 4297 | Don't use eval or a similar built-in parser. 4298 | 4299 | For example, given '-1 + (2 + 3)', you should return 4. 4300 | 4301 | 4302 | -------------------------------------------------------------------------------- 4303 | 4304 | ## Problem-275:waxing_crescent_moon: 4305 | 4306 | 4307 | > This problem was asked by Epic. 4308 | 4309 | The "look and say" sequence is defined as follows: beginning with the term 1, 4310 | each subsequent term visually describes the digits appearing in the previous 4311 | term. The first few terms are as follows: 4312 | 4313 | 1 4314 | 11 4315 | 21 4316 | 1211 4317 | 111221 4318 | 4319 | 4320 | As an example, the fourth term is 1211, since the third term consists of one 2 4321 | and one 1. 4322 | 4323 | Given an integer N, print the Nth term of this sequence. 4324 | 4325 | 4326 | -------------------------------------------------------------------------------- 4327 | 4328 | ## Problem-276:waxing_crescent_moon: 4329 | 4330 | 4331 | > This problem was asked by Dropbox. 4332 | 4333 | Implement an efficient string matching algorithm. 4334 | 4335 | That is, given a string of length N and a pattern of length k, write a program 4336 | that searches for the pattern in the string with less than O(N * k) worst-case 4337 | time complexity. 4338 | 4339 | If the pattern is found, return the start index of its location. If not, return 4340 | False. 4341 | 4342 | 4343 | -------------------------------------------------------------------------------- 4344 | 4345 | ## Problem-277:waxing_crescent_moon: 4346 | 4347 | 4348 | > This problem was asked by Google. 4349 | 4350 | UTF-8 is a character encoding that maps each symbol to one, two, three, or four 4351 | bytes. 4352 | 4353 | For example, the Euro sign, €, corresponds to the three bytes 11100010 10000010 4354 | 10101100. The rules for mapping characters are as follows: 4355 | 4356 | * For a single-byte character, the first bit must be zero. 4357 | * For an n-byte character, the first byte starts with n ones and a zero. The 4358 | other n - 1 bytes all start with 10. 4359 | 4360 | Visually, this can be represented as follows. 4361 | 4362 | Bytes | Byte format 4363 | ----------------------------------------------- 4364 | 1 | 0xxxxxxx 4365 | 2 | 110xxxxx 10xxxxxx 4366 | 3 | 1110xxxx 10xxxxxx 10xxxxxx 4367 | 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 4368 | 4369 | 4370 | Write a program that takes in an array of integers representing byte values, and 4371 | returns whether it is a valid UTF-8 encoding. 4372 | 4373 | 4374 | -------------------------------------------------------------------------------- 4375 | 4376 | ## Problem-278:waxing_crescent_moon: 4377 | 4378 | 4379 | > This problem was asked by Amazon. 4380 | 4381 | Given an integer N, construct all possible binary search trees with N nodes. 4382 | 4383 | 4384 | -------------------------------------------------------------------------------- 4385 | 4386 | ## Problem-279:waxing_crescent_moon: 4387 | 4388 | 4389 | > This problem was asked by Twitter. 4390 | 4391 | A classroom consists of N students, whose friendships can be represented in an 4392 | adjacency list. For example, the following descibes a situation where 0 is 4393 | friends with 1 and 2, 3 is friends with 6, and so on. 4394 | 4395 | {0: [1, 2], 4396 | 1: [0, 5], 4397 | 2: [0], 4398 | 3: [6], 4399 | 4: [], 4400 | 5: [1], 4401 | 6: [3]} 4402 | 4403 | 4404 | Each student can be placed in a friend group, which can be defined as the 4405 | transitive closure of that student's friendship relations. In other words, this 4406 | is the smallest set such that no student in the group has any friends outside 4407 | this group. For the example above, the friend groups would be {0, 1, 2, 5}, {3, 4408 | 6}, {4}. 4409 | 4410 | Given a friendship list such as the one above, determine the number of friend 4411 | groups in the class. 4412 | 4413 | 4414 | -------------------------------------------------------------------------------- 4415 | 4416 | ## Problem-280:waxing_crescent_moon: 4417 | 4418 | 4419 | > This problem was asked by Pandora. 4420 | 4421 | Given an undirected graph, determine if it contains a cycle. 4422 | 4423 | 4424 | -------------------------------------------------------------------------------- 4425 | 4426 | ## Problem-281:waxing_crescent_moon: 4427 | 4428 | 4429 | > This problem was asked by LinkedIn. 4430 | 4431 | A wall consists of several rows of bricks of various integer lengths and uniform 4432 | height. Your goal is to find a vertical line going from the top to the bottom of 4433 | the wall that cuts through the fewest number of bricks. If the line goes through 4434 | the edge between two bricks, this does not count as a cut. 4435 | 4436 | For example, suppose the input is as follows, where values in each row represent 4437 | the lengths of bricks in that row: 4438 | 4439 | [[3, 5, 1, 1], 4440 | [2, 3, 3, 2], 4441 | [5, 5], 4442 | [4, 4, 2], 4443 | [1, 3, 3, 3], 4444 | [1, 1, 6, 1, 1]] 4445 | 4446 | 4447 | The best we can we do here is to draw a line after the eighth brick, which will 4448 | only require cutting through the bricks in the third and fifth row. 4449 | 4450 | Given an input consisting of brick lengths for each row such as the one above, 4451 | return the fewest number of bricks that must be cut to create a vertical line. 4452 | 4453 | 4454 | -------------------------------------------------------------------------------- 4455 | 4456 | ## Problem-282:waxing_crescent_moon: 4457 | 4458 | 4459 | > This problem was asked by Netflix. 4460 | 4461 | Given an array of integers, determine whether it contains a Pythagorean triplet. 4462 | Recall that a Pythogorean triplet (a, b, c) is defined by the equation a2+ b2= c 4463 | 2. 4464 | 4465 | 4466 | -------------------------------------------------------------------------------- 4467 | 4468 | ## Problem-283:waxing_crescent_moon: 4469 | 4470 | 4471 | > This problem was asked by Google. 4472 | 4473 | A regular number in mathematics is defined as one which evenly divides some 4474 | power of 60. Equivalently, we can say that a regular number is one whose only 4475 | prime divisors are 2, 3, and 5. 4476 | 4477 | These numbers have had many applications, from helping ancient Babylonians keep 4478 | time to tuning instruments according to the diatonic scale. 4479 | 4480 | Given an integer N, write a program that returns, in order, the first N regular 4481 | numbers. 4482 | 4483 | 4484 | -------------------------------------------------------------------------------- 4485 | 4486 | ## Problem-284:waxing_crescent_moon: 4487 | 4488 | 4489 | > This problem was asked by Yext. 4490 | 4491 | Two nodes in a binary tree can be called cousins if they are on the same level 4492 | of the tree but have different parents. For example, in the following diagram 4 4493 | and 6 are cousins. 4494 | 4495 | 1 4496 | / \ 4497 | 2 3 4498 | / \ \ 4499 | 4 5 6 4500 | 4501 | 4502 | Given a binary tree and a particular node, find all cousins of that node. 4503 | 4504 | 4505 | -------------------------------------------------------------------------------- 4506 | 4507 | ## Problem-285:waxing_crescent_moon: 4508 | 4509 | 4510 | > This problem was asked by Mailchimp. 4511 | 4512 | You are given an array representing the heights of neighboring buildings on a 4513 | city street, from east to west. The city assessor would like you to write an 4514 | algorithm that returns how many of these buildings have a view of the setting 4515 | sun, in order to properly value the street. 4516 | 4517 | For example, given the array [3, 7, 8, 3, 6, 1], you should return 3, since the 4518 | top floors of the buildings with heights 8, 6, and 1 all have an unobstructed 4519 | view to the west. 4520 | 4521 | Can you do this using just one forward pass through the array? 4522 | 4523 | 4524 | -------------------------------------------------------------------------------- 4525 | 4526 | ## Problem-286:waxing_crescent_moon: 4527 | 4528 | 4529 | > This problem was asked by VMware. 4530 | 4531 | The skyline of a city is composed of several buildings of various widths and 4532 | heights, possibly overlapping one another when viewed from a distance. We can 4533 | represent the buildings using an array of (left, right, height) tuples, which 4534 | tell us where on an imaginary x-axis a building begins and ends, and how tall it 4535 | is. The skyline itself can be described by a list of (x, height) tuples, giving 4536 | the locations at which the height visible to a distant observer changes, and 4537 | each new height. 4538 | 4539 | Given an array of buildings as described above, create a function that returns 4540 | the skyline. 4541 | 4542 | For example, suppose the input consists of the buildings [(0, 15, 3), (4, 11, 4543 | 5), (19, 23, 4)]. In aggregate, these buildings would create a skyline that 4544 | looks like the one below. 4545 | 4546 | ______ 4547 | | | ___ 4548 | ___| |___ | | 4549 | | | B | | | C | 4550 | | A | | A | | | 4551 | | | | | | | 4552 | ------------------------ 4553 | 4554 | 4555 | As a result, your function should return [(0, 3), (4, 5), (11, 3), (15, 0), (19, 4556 | 4), (23, 0)]. 4557 | 4558 | 4559 | -------------------------------------------------------------------------------- 4560 | 4561 | ## Problem-287:waxing_crescent_moon: 4562 | 4563 | 4564 | > This problem was asked by Quora. 4565 | 4566 | You are given a list of (website, user) pairs that represent users visiting 4567 | websites. Come up with a program that identifies the top k pairs of websites 4568 | with the greatest similarity. 4569 | 4570 | For example, suppose k = 1, and the list of tuples is: 4571 | 4572 | [('a', 1), ('a', 3), ('a', 5), 4573 | ('b', 2), ('b', 6), 4574 | ('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5) 4575 | ('d', 4), ('d', 5), ('d', 6), ('d', 7), 4576 | ('e', 1), ('e', 3), ('e': 5), ('e', 6)] 4577 | 4578 | 4579 | Then a reasonable similarity metric would most likely conclude that a and e are 4580 | the most similar, so your program should return [('a', 'e')]. 4581 | 4582 | 4583 | -------------------------------------------------------------------------------- 4584 | 4585 | ## Problem-288:waxing_crescent_moon: 4586 | 4587 | 4588 | > This problem was asked by Salesforce. 4589 | 4590 | The number 6174 is known as Kaprekar's contant, after the mathematician who 4591 | discovered an associated property: for all four-digit numbers with at least two 4592 | distinct digits, repeatedly applying a simple procedure eventually results in 4593 | this value. The procedure is as follows: 4594 | 4595 | * For a given input x, create two new numbers that consist of the digits in x 4596 | in ascending and descending order. 4597 | * Subtract the smaller number from the larger number. 4598 | 4599 | For example, this algorithm terminates in three steps when starting from 1234: 4600 | 4601 | * 4321 - 1234 = 3087 4602 | * 8730 - 0378 = 8352 4603 | * 8532 - 2358 = 6174 4604 | 4605 | Write a function that returns how many steps this will take for a given input N. 4606 | 4607 | 4608 | -------------------------------------------------------------------------------- 4609 | 4610 | ## Problem-289:waxing_crescent_moon: 4611 | 4612 | 4613 | > This problem was asked by Google. 4614 | 4615 | The game of Nim is played as follows. Starting with three heaps, each containing 4616 | a variable number of items, two players take turns removing one or more items 4617 | from a single pile. The player who eventually is forced to take the last stone 4618 | loses. For example, if the initial heap sizes are 3, 4, and 5, a game could be 4619 | played as shown below: 4620 | 4621 | A | B | C 4622 | ----------------- 4623 | 3 | 4 | 5 4624 | 3 | 1 | 3 4625 | 3 | 1 | 3 4626 | 0 | 1 | 3 4627 | 0 | 1 | 0 4628 | 0 | 0 | 0 4629 | 4630 | 4631 | In other words, to start, the first player takes three items from pile B. The 4632 | second player responds by removing two stones from pile C. The game continues in 4633 | this way until player one takes last stone and loses. 4634 | 4635 | Given a list of non-zero starting values [a, b, c], and assuming optimal play, 4636 | determine whether the first player has a forced win. 4637 | 4638 | 4639 | -------------------------------------------------------------------------------- 4640 | 4641 | ## Problem-290:waxing_crescent_moon: 4642 | 4643 | 4644 | > This problem was asked by Facebook. 4645 | 4646 | On a mysterious island there are creatures known as Quxes which come in three 4647 | colors: red, green, and blue. One power of the Qux is that if two of them are 4648 | standing next to each other, they can transform into a single creature of the 4649 | third color. 4650 | 4651 | Given N Quxes standing in a line, determine the smallest number of them 4652 | remaining after any possible sequence of such transformations. 4653 | 4654 | For example, given the input ['R', 'G', 'B', 'G', 'B'], it is possible to end up 4655 | with a single Qux through the following steps: 4656 | 4657 | Arrangement | Change 4658 | ---------------------------------------- 4659 | ['R', 'G', 'B', 'G', 'B'] | (R, G) -> B 4660 | ['B', 'B', 'G', 'B'] | (B, G) -> R 4661 | ['B', 'R', 'B'] | (R, B) -> G 4662 | ['B', 'G'] | (B, G) -> R 4663 | ['R'] | 4664 | 4665 | 4666 | 4667 | -------------------------------------------------------------------------------- 4668 | 4669 | ## Problem-291:waxing_crescent_moon: 4670 | 4671 | 4672 | > This problem was asked by Glassdoor. 4673 | 4674 | An imminent hurricane threatens the coastal town of Codeville. If at most two 4675 | people can fit in a rescue boat, and the maximum weight limit for a given boat 4676 | is k, determine how many boats will be needed to save everyone. 4677 | 4678 | For example, given a population with weights [100, 200, 150, 80] and a boat 4679 | limit of 200, the smallest number of boats required will be three. 4680 | 4681 | 4682 | -------------------------------------------------------------------------------- 4683 | 4684 | ## Problem-292:waxing_crescent_moon: 4685 | 4686 | 4687 | > This problem was asked by Twitter. 4688 | 4689 | A teacher must divide a class of students into two teams to play dodgeball. 4690 | Unfortunately, not all the kids get along, and several refuse to be put on the 4691 | same team as that of their enemies. 4692 | 4693 | Given an adjacency list of students and their enemies, write an algorithm that 4694 | finds a satisfactory pair of teams, or returns False if none exists. 4695 | 4696 | For example, given the following enemy graph you should return the teams {0, 1, 4697 | 4, 5} and {2, 3}. 4698 | 4699 | students = { 4700 | 0: [3], 4701 | 1: [2], 4702 | 2: [1, 4], 4703 | 3: [0, 4, 5], 4704 | 4: [2, 3], 4705 | 5: [3] 4706 | } 4707 | 4708 | 4709 | On the other hand, given the input below, you should return False. 4710 | 4711 | students = { 4712 | 0: [3], 4713 | 1: [2], 4714 | 2: [1, 3, 4], 4715 | 3: [0, 2, 4, 5], 4716 | 4: [2, 3], 4717 | 5: [3] 4718 | } 4719 | 4720 | 4721 | 4722 | -------------------------------------------------------------------------------- 4723 | 4724 | ## Problem-293:waxing_crescent_moon: 4725 | 4726 | 4727 | > This problem was asked by Uber. 4728 | 4729 | You have N stones in a row, and would like to create from them a pyramid. This 4730 | pyramid should be constructed such that the height of each stone increases by 4731 | one until reaching the tallest stone, after which the heights decrease by one. 4732 | In addition, the start and end stones of the pyramid should each be one stone 4733 | high. 4734 | 4735 | You can change the height of any stone by paying a cost of 1 unit to lower its 4736 | height by 1, as many times as necessary. Given this information, determine the 4737 | lowest cost method to produce this pyramid. 4738 | 4739 | For example, given the stones [1, 1, 3, 3, 2, 1], the optimal solution is to pay 4740 | 2 to create [0, 1, 2, 3, 2, 1]. 4741 | 4742 | 4743 | -------------------------------------------------------------------------------- 4744 | 4745 | ## Problem-294:waxing_crescent_moon: 4746 | 4747 | 4748 | > This problem was asked by Square. 4749 | 4750 | A competitive runner would like to create a route that starts and ends at his 4751 | house, with the condition that the route goes entirely uphill at first, and then 4752 | entirely downhill. 4753 | 4754 | Given a dictionary of places of the form {location: elevation}, and a dictionary 4755 | mapping paths between some of these locations to their corresponding distances, 4756 | find the length of the shortest route satisfying the condition above. Assume the 4757 | runner's home is location 0. 4758 | 4759 | For example, suppose you are given the following input: 4760 | 4761 | elevations = {0: 5, 1: 25, 2: 15, 3: 20, 4: 10} 4762 | paths = { 4763 | (0, 1): 10, 4764 | (0, 2): 8, 4765 | (0, 3): 15, 4766 | (1, 3): 12, 4767 | (2, 4): 10, 4768 | (3, 4): 5, 4769 | (3, 0): 17, 4770 | (4, 0): 10 4771 | } 4772 | 4773 | 4774 | In this case, the shortest valid path would be 0 -> 2 -> 4 -> 0, with a distance 4775 | of 28. 4776 | 4777 | 4778 | -------------------------------------------------------------------------------- 4779 | 4780 | ## Problem-295:waxing_crescent_moon: 4781 | 4782 | 4783 | > This problem was asked by Stitch Fix. 4784 | 4785 | Pascal's triangle is a triangular array of integers constructed with the 4786 | following formula: 4787 | 4788 | * The first row consists of the number 1. 4789 | * For each subsequent row, each element is the sum of the numbers directly 4790 | above it, on either side. 4791 | 4792 | For example, here are the first few rows: 4793 | 4794 | 1 4795 | 1 1 4796 | 1 2 1 4797 | 1 3 3 1 4798 | 1 4 6 4 1 4799 | 4800 | 4801 | Given an input k, return the kth row of Pascal's triangle. 4802 | 4803 | Bonus: Can you do this using only O(k) space? 4804 | 4805 | 4806 | -------------------------------------------------------------------------------- 4807 | 4808 | ## Problem-296:waxing_crescent_moon: 4809 | 4810 | 4811 | > This problem was asked by Etsy. 4812 | 4813 | Given a sorted array, convert it into a height-balanced binary search tree. 4814 | 4815 | 4816 | -------------------------------------------------------------------------------- 4817 | 4818 | ## Problem-297:waxing_crescent_moon: 4819 | 4820 | 4821 | > This problem was asked by Amazon. 4822 | 4823 | At a popular bar, each customer has a set of favorite drinks, and will happily 4824 | accept any drink among this set. For example, in the following situation, 4825 | customer 0 will be satisfied with drinks 0, 1, 3, or 6. 4826 | 4827 | preferences = { 4828 | 0: [0, 1, 3, 6], 4829 | 1: [1, 4, 7], 4830 | 2: [2, 4, 7, 5], 4831 | 3: [3, 2, 5], 4832 | 4: [5, 8] 4833 | } 4834 | 4835 | 4836 | A lazy bartender working at this bar is trying to reduce his effort by limiting 4837 | the drink recipes he must memorize. Given a dictionary input such as the one 4838 | above, return the fewest number of drinks he must learn in order to satisfy all 4839 | customers. 4840 | 4841 | For the input above, the answer would be 2, as drinks 1 and 5 will satisfy 4842 | everyone. 4843 | 4844 | 4845 | -------------------------------------------------------------------------------- 4846 | 4847 | ## Problem-298:waxing_crescent_moon: 4848 | 4849 | 4850 | > This problem was asked by Google. 4851 | 4852 | A girl is walking along an apple orchard with a bag in each hand. She likes to 4853 | pick apples from each tree as she goes along, but is meticulous about not 4854 | putting different kinds of apples in the same bag. 4855 | 4856 | Given an input describing the types of apples she will pass on her path, in 4857 | order, determine the length of the longest portion of her path that consists of 4858 | just two types of apple trees. 4859 | 4860 | For example, given the input [2, 1, 2, 3, 3, 1, 3, 5], the longest portion will 4861 | involve types 1 and 3, with a length of four. 4862 | 4863 | 4864 | -------------------------------------------------------------------------------- 4865 | 4866 | ## Problem-299:waxing_crescent_moon: 4867 | 4868 | 4869 | > This problem was asked by Samsung. 4870 | 4871 | A group of houses is connected to the main water plant by means of a set of 4872 | pipes. A house can either be connected by a set of pipes extending directly to 4873 | the plant, or indirectly by a pipe to a nearby house which is otherwise 4874 | connected. 4875 | 4876 | For example, here is a possible configuration, where A, B, and C are houses, and 4877 | arrows represent pipes: 4878 | 4879 | A <--> B <--> C <--> plant 4880 | 4881 | 4882 | Each pipe has an associated cost, which the utility company would like to 4883 | minimize. Given an undirected graph of pipe connections, return the lowest cost 4884 | configuration of pipes such that each house has access to water. 4885 | 4886 | In the following setup, for example, we can remove all but the pipes from plant 4887 | to A, plant to B, and B to C, for a total cost of 16. 4888 | 4889 | pipes = { 4890 | 'plant': {'A': 1, 'B': 5, 'C': 20}, 4891 | 'A': {'C': 15}, 4892 | 'B': {'C': 10}, 4893 | 'C': {} 4894 | } 4895 | 4896 | 4897 | 4898 | -------------------------------------------------------------------------------- 4899 | 4900 | ## Problem-300:waxing_crescent_moon: 4901 | 4902 | 4903 | > This problem was asked by Uber. 4904 | 4905 | On election day, a voting machine writes data in the form (voter_id, 4906 | candidate_id) to a text file. Write a program that reads this file as a stream 4907 | and returns the top 3 candidates at any given time. If you find a voter voting 4908 | more than once, report this as fraud. 4909 | 4910 | 4911 | -------------------------------------------------------------------------------- 4912 | 4913 | ## Problem-301:waxing_crescent_moon: 4914 | 4915 | 4916 | > This problem was asked by Triplebyte. 4917 | 4918 | Implement a data structure which carries out the following operations without 4919 | resizing the underlying array: 4920 | 4921 | * add(value): Add a value to the set of values. 4922 | * check(value): Check whether a value is in the set. 4923 | 4924 | The check method may return occasional false positives (in other words, 4925 | incorrectly identifying an element as part of the set), but should always 4926 | correctly identify a true element. 4927 | 4928 | 4929 | -------------------------------------------------------------------------------- 4930 | 4931 | ## Problem-302:waxing_crescent_moon: 4932 | 4933 | 4934 | > This problem was asked by Uber. 4935 | 4936 | You are given a 2-d matrix where each cell consists of either /, \, or an empty 4937 | space. Write an algorithm that determines into how many regions the slashes 4938 | divide the space. 4939 | 4940 | For example, suppose the input for a three-by-six grid is the following: 4941 | 4942 | \ / 4943 | \ / 4944 | \/ 4945 | 4946 | 4947 | Considering the edges of the matrix as boundaries, this divides the grid into 4948 | three triangles, so you should return 3. 4949 | 4950 | 4951 | -------------------------------------------------------------------------------- 4952 | 4953 | ## Problem-303:waxing_crescent_moon: 4954 | 4955 | 4956 | > This problem was asked by Microsoft. 4957 | 4958 | Given a clock time in hh:mm format, determine, to the nearest degree, the angle 4959 | between the hour and the minute hands. 4960 | 4961 | Bonus: When, during the course of a day, will the angle be zero? 4962 | 4963 | 4964 | -------------------------------------------------------------------------------- 4965 | 4966 | ## Problem-304:waxing_crescent_moon: 4967 | 4968 | 4969 | > This problem was asked by Two Sigma. 4970 | 4971 | A knight is placed on a given square on an 8 x 8 chessboard. It is then moved 4972 | randomly several times, where each move is a standard knight move 4973 | [https://en.wikipedia.org/wiki/Knight_(chess)#Movement]. If the knight jumps off 4974 | the board at any point, however, it is not allowed to jump back on. 4975 | 4976 | After k moves, what is the probability that the knight remains on the board? 4977 | 4978 | 4979 | -------------------------------------------------------------------------------- 4980 | 4981 | ## Problem-305:waxing_crescent_moon: 4982 | 4983 | 4984 | > This problem was asked by Amazon. 4985 | 4986 | Given a linked list, remove all consecutive nodes that sum to zero. Print out 4987 | the remaining nodes. 4988 | 4989 | For example, suppose you are given the input 3 -> 4 -> -7 -> 5 -> -6 -> 6. In 4990 | this case, you should first remove 3 -> 4 -> -7, then -6 -> 6, leaving only 5. 4991 | 4992 | 4993 | -------------------------------------------------------------------------------- 4994 | 4995 | ## Problem-306:waxing_crescent_moon: 4996 | 4997 | 4998 | > This problem was asked by Palantir. 4999 | 5000 | You are given a list of N numbers, in which each number is located at most k 5001 | places away from its sorted position. For example, if k = 1, a given element at 5002 | index 4 might end up at indices 3, 4, or 5. 5003 | 5004 | Come up with an algorithm that sorts this list in O(N log k) time. 5005 | 5006 | 5007 | -------------------------------------------------------------------------------- 5008 | 5009 | ## Problem-307:waxing_crescent_moon: 5010 | 5011 | 5012 | > This problem was asked by Oracle. 5013 | 5014 | Given a binary search tree, find the floor and ceiling of a given integer. The 5015 | floor is the highest element in the tree less than or equal to an integer, while 5016 | the ceiling is the lowest element in the tree greater than or equal to an 5017 | integer. 5018 | 5019 | If either value does not exist, return None. 5020 | 5021 | 5022 | -------------------------------------------------------------------------------- 5023 | 5024 | ## Problem-308:waxing_crescent_moon: 5025 | 5026 | 5027 | > This problem was asked by Quantcast. 5028 | 5029 | You are presented with an array representing a Boolean expression. The elements 5030 | are of two kinds: 5031 | 5032 | * T and F, representing the values True and False. 5033 | * &, |, and ^, representing the bitwise operators for AND, OR, and XOR. 5034 | 5035 | Determine the number of ways to group the array elements using parentheses so 5036 | that the entire expression evaluates to True. 5037 | 5038 | For example, suppose the input is ['F', '|', 'T', '&', 'T']. In this case, there 5039 | are two acceptable groupings: (F | T) & T and F | (T & T). 5040 | 5041 | 5042 | -------------------------------------------------------------------------------- 5043 | 5044 | ## Problem-309:waxing_crescent_moon: 5045 | 5046 | 5047 | > This problem was asked by Walmart Labs. 5048 | 5049 | There are M people sitting in a row of N seats, where M < N. Your task is to 5050 | redistribute people such that there are no gaps between any of them, while 5051 | keeping overall movement to a minimum. 5052 | 5053 | For example, suppose you are faced with an input of [0, 1, 1, 0, 1, 0, 0, 0, 1], 5054 | where 0 represents an empty seat and 1 represents a person. In this case, one 5055 | solution would be to place the person on the right in the fourth seat. We can 5056 | consider the cost of a solution to be the sum of the absolute distance each 5057 | person must move, so that the cost here would be five. 5058 | 5059 | Given an input such as the one above, return the lowest possible cost of moving 5060 | people to remove all gaps. 5061 | 5062 | 5063 | -------------------------------------------------------------------------------- 5064 | 5065 | ## Problem-310:waxing_crescent_moon: 5066 | 5067 | 5068 | > This problem was asked by Pivotal. 5069 | 5070 | Write an algorithm that finds the total number of set bits in all integers 5071 | between 1 and N. 5072 | 5073 | 5074 | -------------------------------------------------------------------------------- 5075 | 5076 | ## Problem-311:waxing_crescent_moon: 5077 | 5078 | 5079 | > This problem was asked by Sumo Logic. 5080 | 5081 | Given an unsorted array, in which all elements are distinct, find a "peak" 5082 | element in O(log N) time. 5083 | 5084 | An element is considered a peak if it is greater than both its left and right 5085 | neighbors. It is guaranteed that the first and last elements are lower than all 5086 | others. 5087 | 5088 | 5089 | -------------------------------------------------------------------------------- 5090 | 5091 | ## Problem-312:waxing_crescent_moon: 5092 | 5093 | 5094 | > This problem was asked by Wayfair. 5095 | 5096 | You are given a 2 x N board, and instructed to completely cover the board with 5097 | the following shapes: 5098 | 5099 | * Dominoes, or 2 x 1 rectangles. 5100 | * Trominoes, or L-shapes. 5101 | 5102 | For example, if N = 4, here is one possible configuration, where A is a domino, 5103 | and B and C are trominoes. 5104 | 5105 | A B B C 5106 | A B C C 5107 | 5108 | 5109 | Given an integer N, determine in how many ways this task is possible. 5110 | 5111 | 5112 | -------------------------------------------------------------------------------- 5113 | 5114 | ## Problem-313:waxing_crescent_moon: 5115 | 5116 | 5117 | > This problem was asked by Citrix. 5118 | 5119 | You are given a circular lock with three wheels, each of which display the 5120 | numbers 0 through 9 in order. Each of these wheels rotate clockwise and 5121 | counterclockwise. 5122 | 5123 | In addition, the lock has a certain number of "dead ends", meaning that if you 5124 | turn the wheels to one of these combinations, the lock becomes stuck in that 5125 | state and cannot be opened. 5126 | 5127 | Let us consider a "move" to be a rotation of a single wheel by one digit, in 5128 | either direction. Given a lock initially set to 000, a target combination, and a 5129 | list of dead ends, write a function that returns the minimum number of moves 5130 | required to reach the target state, or None if this is impossible. 5131 | 5132 | 5133 | -------------------------------------------------------------------------------- 5134 | 5135 | ## Problem-314:waxing_crescent_moon: 5136 | 5137 | 5138 | > This problem was asked by Spotify. 5139 | 5140 | You are the technical director of WSPT radio, serving listeners nationwide. For 5141 | simplicity's sake we can consider each listener to live along a horizontal line 5142 | stretching from 0 (west) to 1000 (east). 5143 | 5144 | Given a list of N listeners, and a list of M radio towers, each placed at 5145 | various locations along this line, determine what the minimum broadcast range 5146 | would have to be in order for each listener's home to be covered. 5147 | 5148 | For example, suppose listeners = [1, 5, 11, 20], and towers = [4, 8, 15]. In 5149 | this case the minimum range would be 5, since that would be required for the 5150 | tower at position 15 to reach the listener at position 20. 5151 | 5152 | 5153 | -------------------------------------------------------------------------------- 5154 | 5155 | ## Problem-315:waxing_crescent_moon: 5156 | 5157 | 5158 | > This problem was asked by Google. 5159 | 5160 | In linear algebra, a Toeplitz matrix is one in which the elements on any given 5161 | diagonal from top left to bottom right are identical. 5162 | 5163 | Here is an example: 5164 | 5165 | 1 2 3 4 8 5166 | 5 1 2 3 4 5167 | 4 5 1 2 3 5168 | 7 4 5 1 2 5169 | 5170 | 5171 | Write a program to determine whether a given input is a Toeplitz matrix. 5172 | 5173 | 5174 | -------------------------------------------------------------------------------- 5175 | 5176 | ## Problem-316:waxing_crescent_moon: 5177 | 5178 | 5179 | > This problem was asked by Snapchat. 5180 | 5181 | You are given an array of length N, where each element i represents the number 5182 | of ways we can produce i units of change. For example, [1, 0, 1, 1, 2] would 5183 | indicate that there is only one way to make 0, 2, or 3 units, and two ways of 5184 | making 4 units. 5185 | 5186 | Given such an array, determine the denominations that must be in use. In the 5187 | case above, for example, there must be coins with value 2, 3, and 4. 5188 | 5189 | 5190 | -------------------------------------------------------------------------------- 5191 | 5192 | ## Problem-317:waxing_crescent_moon: 5193 | 5194 | 5195 | > This problem was asked by Yahoo. 5196 | 5197 | Write a function that returns the bitwise AND of all integers between M and N, 5198 | inclusive. 5199 | 5200 | 5201 | -------------------------------------------------------------------------------- 5202 | 5203 | ## Problem-318:waxing_crescent_moon: 5204 | 5205 | 5206 | > This problem was asked by Apple. 5207 | 5208 | You are going on a road trip, and would like to create a suitable music 5209 | playlist. The trip will require N songs, though you only have M songs 5210 | downloaded, where M < N. A valid playlist should select each song at least once, 5211 | and guarantee a buffer of B songs between repeats. 5212 | 5213 | Given N, M, and B, determine the number of valid playlists. 5214 | 5215 | 5216 | -------------------------------------------------------------------------------- 5217 | 5218 | ## Problem-319:waxing_crescent_moon: 5219 | 5220 | 5221 | > This problem was asked by Airbnb. 5222 | 5223 | An 8-puzzle is a game played on a 3 x 3 board of tiles, with the ninth tile 5224 | missing. The remaining tiles are labeled 1 through 8 but shuffled randomly. 5225 | Tiles may slide horizontally or vertically into an empty space, but may not be 5226 | removed from the board. 5227 | 5228 | Design a class to represent the board, and find a series of steps to bring the 5229 | board to the state [[1, 2, 3], [4, 5, 6], [7, 8, None]]. 5230 | 5231 | 5232 | -------------------------------------------------------------------------------- 5233 | 5234 | ## Problem-320:waxing_crescent_moon: 5235 | 5236 | 5237 | > This problem was asked by Amazon. 5238 | 5239 | Given a string, find the length of the smallest window that contains every 5240 | distinct character. Characters may appear more than once in the window. 5241 | 5242 | For example, given "jiujitsu", you should return 5, corresponding to the final 5243 | five letters. 5244 | 5245 | 5246 | -------------------------------------------------------------------------------- 5247 | 5248 | ## Problem-321:waxing_crescent_moon: 5249 | 5250 | 5251 | > This problem was asked by PagerDuty. 5252 | 5253 | Given a positive integer N, find the smallest number of steps it will take to 5254 | reach 1. 5255 | 5256 | There are two kinds of permitted steps: 5257 | 5258 | * You may decrement N to N - 1. 5259 | * If a * b = N, you may decrement N to the larger of a and b. 5260 | 5261 | For example, given 100, you can reach 1 in five steps with the following route: 5262 | 100 -> 10 -> 9 -> 3 -> 2 -> 1. 5263 | 5264 | 5265 | -------------------------------------------------------------------------------- 5266 | 5267 | ## Problem-322:waxing_crescent_moon: 5268 | 5269 | 5270 | > This problem was asked by Flipkart. 5271 | 5272 | Starting from 0 on a number line, you would like to make a series of jumps that 5273 | lead to the integer N. 5274 | 5275 | On the ith jump, you may move exactly i places to the left or right. 5276 | 5277 | Find a path with the fewest number of jumps required to get from 0 to N. 5278 | 5279 | 5280 | -------------------------------------------------------------------------------- 5281 | 5282 | ## Problem-323:waxing_crescent_moon: 5283 | 5284 | 5285 | > This problem was asked by Dropbox. 5286 | 5287 | Create an algorithm to efficiently compute the approximate median of a list of 5288 | numbers. 5289 | 5290 | More precisely, given an unordered list of N numbers, find an element whose rank 5291 | is between N / 4 and 3 * N / 4, with a high level of certainty, in less than 5292 | O(N) time. 5293 | 5294 | 5295 | -------------------------------------------------------------------------------- 5296 | 5297 | ## Problem-324:waxing_crescent_moon: 5298 | 5299 | 5300 | > This problem was asked by Amazon. 5301 | 5302 | Consider the following scenario: there are N mice and N holes placed at integer 5303 | points along a line. Given this, find a method that maps mice to holes such that 5304 | the largest number of steps any mouse takes is minimized. 5305 | 5306 | Each move consists of moving one mouse one unit to the left or right, and only 5307 | one mouse can fit inside each hole. 5308 | 5309 | For example, suppose the mice are positioned at [1, 4, 9, 15], and the holes are 5310 | located at [10, -5, 0, 16]. In this case, the best pairing would require us to 5311 | send the mouse at 1 to the hole at -5, so our function should return 6. 5312 | 5313 | 5314 | -------------------------------------------------------------------------------- 5315 | 5316 | ## Problem-325:waxing_crescent_moon: 5317 | 5318 | 5319 | > This problem was asked by Jane Street. 5320 | 5321 | The United States uses the imperial system of weights and measures, which means 5322 | that there are many different, seemingly arbitrary units to measure distance. 5323 | There are 12 inches in a foot, 3 feet in a yard, 22 yards in a chain, and so on 5324 | [https://en.wikipedia.org/wiki/Imperial_units#Length]. 5325 | 5326 | Create a data structure that can efficiently convert a certain quantity of one 5327 | unit to the correct amount of any other unit. You should also allow for 5328 | additional units to be added to the system. 5329 | 5330 | 5331 | -------------------------------------------------------------------------------- 5332 | 5333 | ## Problem-326:waxing_crescent_moon: 5334 | 5335 | 5336 | > This problem was asked by Netflix. 5337 | 5338 | A Cartesian tree with sequence S is a binary tree defined by the following two 5339 | properties: 5340 | 5341 | * It is heap-ordered, so that each parent value is strictly less than that of 5342 | its children. 5343 | * An in-order traversal of the tree produces nodes with values that correspond 5344 | exactly to S. 5345 | 5346 | For example, given the sequence [3, 2, 6, 1, 9], the resulting Cartesian tree 5347 | would be: 5348 | 5349 | 1 5350 | / \ 5351 | 2 9 5352 | / \ 5353 | 3 6 5354 | 5355 | 5356 | Given a sequence S, construct the corresponding Cartesian tree. 5357 | 5358 | 5359 | -------------------------------------------------------------------------------- 5360 | 5361 | ## Problem-327:waxing_crescent_moon: 5362 | 5363 | 5364 | > This problem was asked by Salesforce. 5365 | 5366 | Write a program to merge two binary trees. Each node in the new tree should hold 5367 | a value equal to the sum of the values of the corresponding nodes of the input 5368 | trees. 5369 | 5370 | If only one input tree has a node in a given position, the corresponding node in 5371 | the new tree should match that input node. 5372 | 5373 | 5374 | -------------------------------------------------------------------------------- 5375 | 5376 | ## Problem-328:waxing_crescent_moon: 5377 | 5378 | 5379 | > This problem was asked by Facebook. 5380 | 5381 | In chess, the Elo rating system is used to calculate player strengths based on 5382 | game results. 5383 | 5384 | A simplified description of the Elo system is as follows. Every player begins at 5385 | the same score. For each subsequent game, the loser transfers some points to the 5386 | winner, where the amount of points transferred depends on how unlikely the win 5387 | is. For example, a 1200-ranked player should gain much more points for beating a 5388 | 2000-ranked player than for beating a 1300-ranked player. 5389 | 5390 | Implement this system. 5391 | 5392 | 5393 | -------------------------------------------------------------------------------- 5394 | 5395 | ## Problem-329:waxing_crescent_moon: 5396 | 5397 | 5398 | > This problem was asked by Amazon. 5399 | 5400 | The stable marriage problem 5401 | [https://en.wikipedia.org/wiki/Stable_marriage_problem] is defined as follows: 5402 | 5403 | Suppose you have N men and N women, and each person has ranked their prospective 5404 | opposite-sex partners in order of preference. 5405 | 5406 | For example, if N = 3, the input could be something like this: 5407 | 5408 | guy_preferences = { 5409 | 'andrew': ['caroline', 'abigail', 'betty'], 5410 | 'bill': ['caroline', 'betty', 'abigail'], 5411 | 'chester': ['betty', 'caroline', 'abigail'], 5412 | } 5413 | 5414 | gal_preferences = { 5415 | 'abigail': ['andrew', 'bill', 'chester'], 5416 | 'betty': ['bill', 'andrew', 'chester'], 5417 | 'caroline': ['bill', 'chester', 'andrew'] 5418 | } 5419 | 5420 | 5421 | Write an algorithm that pairs the men and women together in such a way that no 5422 | two people of opposite sex would both rather be with each other than with their 5423 | current partners. 5424 | 5425 | 5426 | -------------------------------------------------------------------------------- 5427 | 5428 | ## Problem-330:waxing_crescent_moon: 5429 | 5430 | 5431 | > This problem was asked by Dropbox. 5432 | 5433 | A Boolean formula can be said to be satisfiable if there is a way to assign 5434 | truth values to each variable such that the entire formula evaluates to true. 5435 | 5436 | For example, suppose we have the following formula, where the symbol ¬ is used 5437 | to denote negation: 5438 | 5439 | (¬c OR b) AND (b OR c) AND (¬b OR c) AND (¬c OR ¬a) 5440 | 5441 | One way to satisfy this formula would be to let a = False, b = True, and c = 5442 | True. 5443 | 5444 | This type of formula, with AND statements joining tuples containing exactly one 5445 | OR, is known as 2-CNF. 5446 | 5447 | Given a 2-CNF formula, find a way to assign truth values to satisfy it, or 5448 | return False if this is impossible. 5449 | 5450 | 5451 | -------------------------------------------------------------------------------- 5452 | 5453 | ## Problem-331:waxing_crescent_moon: 5454 | 5455 | 5456 | > This problem was asked by LinkedIn. 5457 | 5458 | You are given a string consisting of the letters x and y, such as xyxxxyxyy. In 5459 | addition, you have an operation called flip, which changes a single x to y or 5460 | vice versa. 5461 | 5462 | Determine how many times you would need to apply this operation to ensure that 5463 | all x's come before all y's. In the preceding example, it suffices to flip the 5464 | second and sixth characters, so you should return 2. 5465 | 5466 | 5467 | -------------------------------------------------------------------------------- 5468 | 5469 | ## Problem-332:waxing_crescent_moon: 5470 | 5471 | 5472 | > This problem was asked by Jane Street. 5473 | 5474 | Given integers M and N, write a program that counts how many positive integer 5475 | pairs (a, b) satisfy the following conditions: 5476 | 5477 | * a + b = M 5478 | * a XOR b = N 5479 | 5480 | 5481 | -------------------------------------------------------------------------------- 5482 | 5483 | ## Problem-333:waxing_crescent_moon: 5484 | 5485 | 5486 | > This problem was asked by Pinterest. 5487 | 5488 | At a party, there is a single person who everyone knows, but who does not know 5489 | anyone in return (the "celebrity"). To help figure out who this is, you have 5490 | access to an O(1) method called knows(a, b), which returns True if person a 5491 | knows person b, else False. 5492 | 5493 | Given a list of N people and the above operation, find a way to identify the 5494 | celebrity in O(N) time. 5495 | 5496 | 5497 | -------------------------------------------------------------------------------- 5498 | 5499 | ## Problem-334:waxing_crescent_moon: 5500 | 5501 | 5502 | > This problem was asked by Twitter. 5503 | 5504 | The 24 game is played as follows. You are given a list of four integers, each 5505 | between 1 and 9, in a fixed order. By placing the operators +, -, *, and / 5506 | between the numbers, and grouping them with parentheses, determine whether it is 5507 | possible to reach the value 24. 5508 | 5509 | For example, given the input [5, 2, 7, 8], you should return True, since (5 * 2 5510 | - 7) * 8 = 24. 5511 | 5512 | Write a function that plays the 24 game. 5513 | 5514 | 5515 | -------------------------------------------------------------------------------- 5516 | 5517 | ## Problem-335:waxing_crescent_moon: 5518 | 5519 | 5520 | > This problem was asked by Google. 5521 | 5522 | PageRank is an algorithm used by Google to rank the importance of different 5523 | websites. While there have been changes over the years, the central idea is to 5524 | assign each site a score based on the importance of other pages that link to 5525 | that page. 5526 | 5527 | More mathematically, suppose there are N sites, and each site i has a certain 5528 | count Ci of outgoing links. Then the score for a particular site Sj is defined 5529 | as : 5530 | 5531 | score(Sj) = (1 - d) / N + d * (score(Sx) / Cx+ score(Sy) / Cy+ ... + score(Sz) / 5532 | Cz)) 5533 | 5534 | Here, Sx, Sy, ..., Sz denote the scores of all the other sites that have 5535 | outgoing links to Sj, and d is a damping factor, usually set to around 0.85, 5536 | used to model the probability that a user will stop searching. 5537 | 5538 | Given a directed graph of links between various websites, write a program that 5539 | calculates each site's page rank. 5540 | 5541 | 5542 | -------------------------------------------------------------------------------- 5543 | 5544 | ## Problem-336:waxing_crescent_moon: 5545 | 5546 | 5547 | > This problem was asked by Microsoft. 5548 | 5549 | Write a program to determine how many distinct ways there are to create a max 5550 | heap from a list of N given integers. 5551 | 5552 | For example, if N = 3, and our integers are [1, 2, 3], there are two ways, shown 5553 | below. 5554 | 5555 | 3 3 5556 | / \ / \ 5557 | 1 2 2 1 5558 | 5559 | 5560 | 5561 | -------------------------------------------------------------------------------- 5562 | 5563 | ## Problem-337:waxing_crescent_moon: 5564 | 5565 | 5566 | > This problem was asked by Apple. 5567 | 5568 | Given a linked list, uniformly shuffle the nodes. What if we want to prioritize 5569 | space over time? 5570 | 5571 | 5572 | -------------------------------------------------------------------------------- 5573 | 5574 | ## Problem-338:waxing_crescent_moon: 5575 | 5576 | 5577 | > This problem was asked by Facebook. 5578 | 5579 | Given an integer n, find the next biggest integer with the same number of 1-bits 5580 | on. For example, given the number 6 (0110 in binary), return 9 (1001). 5581 | 5582 | 5583 | -------------------------------------------------------------------------------- 5584 | 5585 | ## Problem-339:waxing_crescent_moon: 5586 | 5587 | 5588 | > This problem was asked by Microsoft. 5589 | 5590 | Given an array of numbers and a number k, determine if there are three entries 5591 | in the array which add up to the specified number k. For example, given [20, 5592 | 303, 3, 4, 25] and k = 49, returntrue as 20 + 4 + 25 = 49. 5593 | 5594 | 5595 | -------------------------------------------------------------------------------- 5596 | 5597 | ## Problem-340:waxing_crescent_moon: 5598 | 5599 | 5600 | > This problem was asked by Google. 5601 | 5602 | Given a set of points (x, y) on a 2D cartesian plane, find the two closest 5603 | points. For example, given the points [(1, 1), (-1, -1), (3, 4), (6, 1), (-1, 5604 | -6), (-4, -3)], return [(-1, -1), (1, 1)]. 5605 | 5606 | 5607 | -------------------------------------------------------------------------------- 5608 | 5609 | ## Problem-341:waxing_crescent_moon: 5610 | 5611 | 5612 | > This problem was asked by Google. 5613 | 5614 | You are given an N by N matrix of random letters and a dictionary of words. Find 5615 | the maximum number of words that can be packed on the board from the given 5616 | dictionary. 5617 | 5618 | A word is considered to be able to be packed on the board if: 5619 | 5620 | * It can be found in the dictionary 5621 | * It can be constructed from untaken letters by other words found so far on the 5622 | board 5623 | * The letters are adjacent to each other (vertically and horizontally, not 5624 | diagonally). 5625 | 5626 | Each tile can be visited only once by any word. 5627 | 5628 | For example, given the following dictionary: 5629 | 5630 | { 'eat', 'rain', 'in', 'rat' } 5631 | 5632 | 5633 | and matrix: 5634 | 5635 | [['e', 'a', 'n'], 5636 | ['t', 't', 'i'], 5637 | ['a', 'r', 'a']] 5638 | 5639 | 5640 | Your function should return 3, since we can make the words 'eat', 'in', and 5641 | 'rat' without them touching each other. We could have alternatively made 'eat' 5642 | and 'rain', but that would be incorrect since that's only 2 words. 5643 | 5644 | 5645 | -------------------------------------------------------------------------------- 5646 | 5647 | ## Problem-342:waxing_crescent_moon: 5648 | 5649 | 5650 | > This problem was asked by Stripe. 5651 | 5652 | reduce (also known as fold) is a function that takes in an array, a combining 5653 | function, and an initial value and builds up a result by calling the combining 5654 | function on each element of the array, left to right. For example, we can write 5655 | sum() in terms of reduce: 5656 | 5657 | def add(a, b): 5658 | return a + b 5659 | 5660 | def sum(lst): 5661 | return reduce(lst, add, 0) 5662 | 5663 | 5664 | This should call add on the initial value with the first element of the array, 5665 | and then the result of that with the second element of the array, and so on 5666 | until we reach the end, when we return the sum of the array. 5667 | 5668 | Implement your own version of reduce. 5669 | 5670 | 5671 | -------------------------------------------------------------------------------- 5672 | 5673 | ## Problem-343:waxing_crescent_moon: 5674 | 5675 | 5676 | > This problem was asked by Google. 5677 | 5678 | Given a binary search tree and a range [a, b] (inclusive), return the sum of the 5679 | elements of the binary search tree within the range. 5680 | 5681 | For example, given the following tree: 5682 | 5683 | 5 5684 | / \ 5685 | 3 8 5686 | / \ / \ 5687 | 2 4 6 10 5688 | 5689 | 5690 | and the range [4, 9], return 23 (5 + 4 + 6 + 8). 5691 | 5692 | 5693 | -------------------------------------------------------------------------------- 5694 | 5695 | ## Problem-344:waxing_crescent_moon: 5696 | 5697 | 5698 | > This problem was asked by Adobe. 5699 | 5700 | You are given a tree with an even number of nodes. Consider each connection 5701 | between a parent and child node to be an "edge". You would like to remove some 5702 | of these edges, such that the disconnected subtrees that remain each have an 5703 | even number of nodes. 5704 | 5705 | For example, suppose your input was the following tree: 5706 | 5707 | 1 5708 | / \ 5709 | 2 3 5710 | / \ 5711 | 4 5 5712 | / | \ 5713 | 6 7 8 5714 | 5715 | 5716 | In this case, removing the edge (3, 4) satisfies our requirement. 5717 | 5718 | Write a function that returns the maximum number of edges you can remove while 5719 | still satisfying this requirement. 5720 | 5721 | 5722 | -------------------------------------------------------------------------------- 5723 | 5724 | ## Problem-345:waxing_crescent_moon: 5725 | 5726 | 5727 | > This problem was asked by Google. 5728 | 5729 | You are given a set of synonyms, such as (big, large) and (eat, consume). Using 5730 | this set, determine if two sentences with the same number of words are 5731 | equivalent. 5732 | 5733 | For example, the following two sentences are equivalent: 5734 | 5735 | * "He wants to eat food." 5736 | * "He wants to consume food." 5737 | 5738 | Note that the synonyms (a, b) and (a, c) do not necessarily imply (b, c): 5739 | consider the case of (coach, bus) and (coach, teacher). 5740 | 5741 | Follow-up: what if we can assume that (a, b) and (a, c) do in fact imply (b, c)? 5742 | 5743 | 5744 | -------------------------------------------------------------------------------- 5745 | 5746 | ## Problem-346:waxing_crescent_moon: 5747 | 5748 | 5749 | > This problem was asked by Airbnb. 5750 | 5751 | You are given a huge list of airline ticket prices between different cities 5752 | around the world on a given day. These are all direct flights. Each element in 5753 | the list has the format (source_city, destination, price). 5754 | 5755 | Consider a user who is willing to take up to k connections from their origin 5756 | city A to their destination B. Find the cheapest fare possible for this journey 5757 | and print the itinerary for that journey. 5758 | 5759 | For example, our traveler wants to go from JFK to LAX with up to 3 connections, 5760 | and our input flights are as follows: 5761 | 5762 | [ 5763 | ('JFK', 'ATL', 150), 5764 | ('ATL', 'SFO', 400), 5765 | ('ORD', 'LAX', 200), 5766 | ('LAX', 'DFW', 80), 5767 | ('JFK', 'HKG', 800), 5768 | ('ATL', 'ORD', 90), 5769 | ('JFK', 'LAX', 500), 5770 | ] 5771 | 5772 | 5773 | Due to some improbably low flight prices, the cheapest itinerary would be JFK -> 5774 | ATL -> ORD -> LAX, costing $440. 5775 | 5776 | 5777 | -------------------------------------------------------------------------------- 5778 | 5779 | ## Problem-347:waxing_crescent_moon: 5780 | 5781 | 5782 | > This problem was asked by Yahoo. 5783 | 5784 | You are given a string of length N and a parameter k. The string can be 5785 | manipulated by taking one of the first k letters and moving it to the end. 5786 | 5787 | Write a program to determine the lexicographically smallest string that can be 5788 | created after an unlimited number of moves. 5789 | 5790 | For example, suppose we are given the string daily and k = 1. The best we can 5791 | create in this case is ailyd. 5792 | 5793 | 5794 | -------------------------------------------------------------------------------- 5795 | 5796 | ## Problem-348:waxing_crescent_moon: 5797 | 5798 | 5799 | > This problem was asked by Zillow. 5800 | 5801 | A ternary search tree is a trie-like data structure where each node may have up 5802 | to three children. Here is an example which represents the words code, cob, be, 5803 | ax, war, and we. 5804 | 5805 | c 5806 | / | \ 5807 | b o w 5808 | / | | | 5809 | a e d a 5810 | | / | | \ 5811 | x b e r e 5812 | 5813 | 5814 | The tree is structured according to the following rules: 5815 | 5816 | * left child nodes link to words lexicographically earlier than the parent 5817 | prefix 5818 | * right child nodes link to words lexicographically later than the parent 5819 | prefix 5820 | * middle child nodes continue the current word 5821 | 5822 | For instance, since code is the first word inserted in the tree, and cob 5823 | lexicographically precedes cod, cob is represented as a left child extending 5824 | from cod. 5825 | 5826 | Implement insertion and search functions for a ternary search tree. 5827 | 5828 | 5829 | -------------------------------------------------------------------------------- 5830 | 5831 | ## Problem-349:waxing_crescent_moon: 5832 | 5833 | 5834 | > This problem was asked by Grammarly. 5835 | 5836 | Soundex [https://en.wikipedia.org/wiki/Soundex] is an algorithm used to 5837 | categorize phonetically, such that two names that sound alike but are spelled 5838 | differently have the same representation. 5839 | 5840 | Soundex maps every name to a string consisting of one letter and three numbers, 5841 | like M460. 5842 | 5843 | One version of the algorithm is as follows: 5844 | 5845 | 1. Remove consecutive consonants with the same sound (for example, change ck -> 5846 | c). 5847 | 2. Keep the first letter. The remaining steps only apply to the rest of the 5848 | string. 5849 | 3. Remove all vowels, including y, w, and h. 5850 | 4. Replace all consonants with the following digits: * b, f, p, v → 1 5851 | * c, g, j, k, q, s, x, z → 5852 | 2 5853 | * d, t → 3 5854 | * l → 4 5855 | * m, n → 5 5856 | * r → 6 5857 | 5858 | 5859 | 5. If you don't have three numbers yet, append zeros until you do. Keep the 5860 | first three numbers. 5861 | 5862 | Using this scheme, Jackson and Jaxen both map to J250. 5863 | 5864 | Implement Soundex. 5865 | 5866 | 5867 | -------------------------------------------------------------------------------- 5868 | 5869 | ## Problem-350:waxing_crescent_moon: 5870 | 5871 | 5872 | > This problem was asked by Uber. 5873 | 5874 | Write a program that determines the smallest number of perfect squares that sum 5875 | up to N. 5876 | 5877 | Here are a few examples: 5878 | 5879 | * Given N = 4, return 1 (4) 5880 | * Given N = 17, return 2 (16 + 1) 5881 | * Given N = 18, return 2 (9 + 9) 5882 | 5883 | 5884 | -------------------------------------------------------------------------------- 5885 | 5886 | ## Problem-351:waxing_crescent_moon: 5887 | 5888 | 5889 | > This problem was asked by Quora. 5890 | 5891 | Word sense disambiguation is the problem of determining which sense a word takes 5892 | on in a particular setting, if that word has multiple meanings. For example, in 5893 | the sentence "I went to get money from the bank", bank probably means the place 5894 | where people deposit money, not the land beside a river or lake. 5895 | 5896 | Suppose you are given a list of meanings for several words, formatted like so: 5897 | 5898 | { 5899 | "word_1": ["meaning one", "meaning two", ...], 5900 | ... 5901 | "word_n": ["meaning one", "meaning two", ...] 5902 | } 5903 | 5904 | 5905 | Given a sentence, most of whose words are contained in the meaning list above, 5906 | create an algorithm that determines the likely sense of each possibly ambiguous 5907 | word. 5908 | 5909 | 5910 | -------------------------------------------------------------------------------- 5911 | 5912 | ## Problem-352:waxing_crescent_moon: 5913 | 5914 | 5915 | > This problem was asked by Palantir. 5916 | 5917 | A typical American-style crossword puzzle grid is an N x N matrix with black and 5918 | white squares, which obeys the following rules: 5919 | 5920 | * Every white square must be part of an "across" word and a "down" word. 5921 | * No word can be fewer than three letters long. 5922 | * Every white square must be reachable from every other white square. 5923 | * The grid is rotationally symmetric (for example, the colors of the top left 5924 | and bottom right squares must match). 5925 | 5926 | Write a program to determine whether a given matrix qualifies as a crossword 5927 | grid. 5928 | 5929 | 5930 | -------------------------------------------------------------------------------- 5931 | 5932 | ## Problem-353:waxing_crescent_moon: 5933 | 5934 | 5935 | > This problem was asked by Square. 5936 | 5937 | You are given a histogram consisting of rectangles of different heights. These 5938 | heights are represented in an input list, such that [1, 3, 2, 5] corresponds to 5939 | the following diagram: 5940 | 5941 | x 5942 | x 5943 | x x 5944 | x x x 5945 | x x x x 5946 | 5947 | 5948 | Determine the area of the largest rectangle that can be formed only from the 5949 | bars of the histogram. For the diagram above, for example, this would be six, 5950 | representing the 2 x 3 area at the bottom right. 5951 | 5952 | 5953 | -------------------------------------------------------------------------------- 5954 | 5955 | ## Problem-354:waxing_crescent_moon: 5956 | 5957 | 5958 | > This problem was asked by Google. 5959 | 5960 | Design a system to crawl and copy all of Wikipedia using a distributed network 5961 | of machines. 5962 | 5963 | More specifically, suppose your server has access to a set of client machines. 5964 | Your client machines can execute code you have written to access Wikipedia 5965 | pages, download and parse their data, and write the results to a database. 5966 | 5967 | Some questions you may want to consider as part of your solution are: 5968 | 5969 | * How will you reach as many pages as possible? 5970 | * How can you keep track of pages that have already been visited? 5971 | * How will you deal with your client machines being blacklisted? 5972 | * How can you update your database when Wikipedia pages are added or updated? 5973 | 5974 | 5975 | -------------------------------------------------------------------------------- 5976 | 5977 | ## Problem-355:waxing_crescent_moon: 5978 | 5979 | 5980 | > This problem was asked by Airbnb. 5981 | 5982 | You are given an array X of floating-point numbers x1, x2, ... xn. These can be 5983 | rounded up or down to create a corresponding array Y of integers y1, y2, ... yn. 5984 | 5985 | Write an algorithm that finds an appropriate Y array with the following 5986 | properties: 5987 | 5988 | * The rounded sums of both arrays should be equal. 5989 | * The absolute pairwise difference between elements is minimized. In other 5990 | words, |x1- y1| + |x2- y2| + ... + |xn- yn| should be as small as possible. 5991 | 5992 | For example, suppose your input is [1.3, 2.3, 4.4]. In this case you cannot do 5993 | better than [1, 2, 5], which has an absolute difference of |1.3 - 1| + |2.3 - 2| 5994 | + |4.4 - 5| = 1. 5995 | 5996 | 5997 | -------------------------------------------------------------------------------- 5998 | 5999 | ## Problem-356:waxing_crescent_moon: 6000 | 6001 | 6002 | > This problem was asked by Netflix. 6003 | 6004 | Implement a queue using a set of fixed-length arrays. 6005 | 6006 | The queue should support enqueue, dequeue, and get_size operations. 6007 | 6008 | 6009 | -------------------------------------------------------------------------------- 6010 | 6011 | ## Problem-357:waxing_crescent_moon: 6012 | 6013 | 6014 | > This problem was asked by LinkedIn. 6015 | 6016 | You are given a binary tree in a peculiar string representation. Each node is 6017 | written in the form (lr), where l corresponds to the left child and r 6018 | corresponds to the right child. 6019 | 6020 | If either l or r is null, it will be represented as a zero. Otherwise, it will 6021 | be represented by a new (lr) pair. 6022 | 6023 | Here are a few examples: 6024 | 6025 | * A root node with no children: (00) 6026 | * A root node with two children: ((00)(00)) 6027 | * An unbalanced tree with three consecutive left children: ((((00)0)0)0) 6028 | 6029 | Given this representation, determine the depth of the tree. 6030 | 6031 | 6032 | -------------------------------------------------------------------------------- 6033 | 6034 | ## Problem-358:waxing_crescent_moon: 6035 | 6036 | 6037 | > This problem was asked by Dropbox. 6038 | 6039 | Create a data structure that performs all the following operations in O(1) time: 6040 | 6041 | * plus: Add a key with value 1. If the key already exists, increment its value 6042 | by one. 6043 | * minus: Decrement the value of a key. If the key's value is currently 1, 6044 | remove it. 6045 | * get_max: Return a key with the highest value. 6046 | * get_min: Return a key with the lowest value. 6047 | 6048 | 6049 | -------------------------------------------------------------------------------- 6050 | 6051 | ## Problem-359:waxing_crescent_moon: 6052 | 6053 | 6054 | > This problem was asked by Slack. 6055 | 6056 | You are given a string formed by concatenating several words corresponding to 6057 | the integers zero through nine and then anagramming. 6058 | 6059 | For example, the input could be 'niesevehrtfeev', which is an anagram of 6060 | 'threefiveseven'. Note that there can be multiple instances of each integer. 6061 | 6062 | Given this string, return the original integers in sorted order. In the example 6063 | above, this would be 357. 6064 | 6065 | 6066 | -------------------------------------------------------------------------------- 6067 | 6068 | ## Problem-360:waxing_crescent_moon: 6069 | 6070 | 6071 | > This problem was asked by Spotify. 6072 | 6073 | You have access to ranked lists of songs for various users. Each song is 6074 | represented as an integer, and more preferred songs appear earlier in each list. 6075 | For example, the list [4, 1, 7] indicates that a user likes song 4 the best, 6076 | followed by songs 1 and 7. 6077 | 6078 | Given a set of these ranked lists, interleave them to create a playlist that 6079 | satisfies everyone's priorities. 6080 | 6081 | For example, suppose your input is {[1, 7, 3], [2, 1, 6, 7, 9], [3, 9, 5]}. In 6082 | this case a satisfactory playlist could be [2, 1, 6, 7, 3, 9, 5]. 6083 | 6084 | 6085 | -------------------------------------------------------------------------------- 6086 | 6087 | ## Problem-361:waxing_crescent_moon: 6088 | 6089 | 6090 | > This problem was asked by Facebook. 6091 | 6092 | Mastermind is a two-player game in which the first player attempts to guess the 6093 | secret code of the second. In this version, the code may be any six-digit number 6094 | with all distinct digits. 6095 | 6096 | Each turn the first player guesses some number, and the second player responds 6097 | by saying how many digits in this number correctly matched their location in the 6098 | secret code. For example, if the secret code were 123456, then a guess of 175286 6099 | would score two, since 1 and 6 were correctly placed. 6100 | 6101 | Write an algorithm which, given a sequence of guesses and their scores, 6102 | determines whether there exists some secret code that could have produced them. 6103 | 6104 | For example, for the following scores you should return True, since they 6105 | correspond to the secret code 123456: 6106 | 6107 | {175286: 2, 293416: 3, 654321: 0} 6108 | 6109 | 6110 | However, it is impossible for any key to result in the following scores, so in 6111 | this case you should return False: 6112 | 6113 | {123456: 4, 345678: 4, 567890: 4} 6114 | 6115 | 6116 | 6117 | -------------------------------------------------------------------------------- 6118 | 6119 | ## Problem-362:waxing_crescent_moon: 6120 | 6121 | 6122 | > This problem was asked by Twitter. 6123 | 6124 | A strobogrammatic number is a positive number that appears the same after being 6125 | rotated 180 degrees. For example, 16891 is strobogrammatic. 6126 | 6127 | Create a program that finds all strobogrammatic numbers with N digits. 6128 | 6129 | 6130 | -------------------------------------------------------------------------------- 6131 | 6132 | ## Problem-363:waxing_crescent_moon: 6133 | 6134 | 6135 | > This problem was asked by Squarespace. 6136 | 6137 | Write a function, add_subtract, which alternately adds and subtracts curried 6138 | arguments. Here are some sample operations: 6139 | 6140 | add_subtract(7) -> 7 6141 | 6142 | add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0 6143 | 6144 | add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11 6145 | 6146 | 6147 | 6148 | -------------------------------------------------------------------------------- 6149 | 6150 | ## Problem-364:waxing_crescent_moon: 6151 | 6152 | 6153 | > This problem was asked by Facebook. 6154 | 6155 | Describe an algorithm to compute the longest increasing subsequence of an array 6156 | of numbers in O(n log n) time. 6157 | 6158 | 6159 | -------------------------------------------------------------------------------- 6160 | 6161 | ## Problem-365:waxing_crescent_moon: 6162 | 6163 | 6164 | > This problem was asked by Google. 6165 | 6166 | A quack is a data structure combining properties of both stacks and queues. It 6167 | can be viewed as a list of elements written left to right such that three 6168 | operations are possible: 6169 | 6170 | * push(x): add a new item x to the left end of the list 6171 | * pop(): remove and return the item on the left end of the list 6172 | * pull(): remove the item on the right end of the list. 6173 | 6174 | Implement a quack using three stacks and O(1) additional memory, so that the 6175 | amortized time for any push, pop, or pull operation is O(1). 6176 | 6177 | 6178 | -------------------------------------------------------------------------------- 6179 | --------------------------------------------------------------------------------