├── 01_120_Python_Basics_Interview_Questions.ipynb ├── 02_Predictive_Modeling.ipynb ├── 03_Programming.ipynb ├── 04_Probability.ipynb ├── 05_Statistical_Inference.ipynb ├── 06_Data_Analysis.ipynb ├── 07_Product_Metrics.ipynb ├── 08_Communication.ipynb ├── 09_Coding.ipynb ├── 10_Linkedin_Skill_Assessment_Python.ipynb ├── DataScience_Interview_Questions.pdf ├── LICENSE ├── README.md └── img └── dnld_rep.png /01_120_Python_Basics_Interview_Questions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Python Basics ➞ 120 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. What is Python?\n", 24 | "\n", 25 | "Solution\n", 26 | "\n", 27 | "- Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages." 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "### 2. Name some of the features of Python.\n", 35 | "\n", 36 | "Solution\n", 37 | "\n", 38 | "Following are some of the salient features of python −\n", 39 | "\n", 40 | "* It supports functional and structured programming methods as well as OOP.\n", 41 | "\n", 42 | "* It can be used as a scripting language or can be compiled to byte-code for building large applications.\n", 43 | "\n", 44 | "* It provides very high-level dynamic data types and supports dynamic type checking.\n", 45 | "\n", 46 | "* It supports automatic garbage collection.\n", 47 | "\n", 48 | "* It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java." 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "### 3. What is the purpose of PYTHONPATH environment variable?\n", 56 | "\n", 57 | "Solution\n", 58 | "\n", 59 | "- PYTHONPATH - It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer." 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "### 4. What is the purpose of PYTHONSTARTUP environment variable?\n", 67 | "\n", 68 | "Solution\n", 69 | "\n", 70 | "- PYTHONSTARTUP - It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH." 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "### 5. What is the purpose of PYTHONCASEOK environment variable?\n", 78 | "\n", 79 | "Solution\n", 80 | "\n", 81 | "- PYTHONCASEOK − It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "### 6. What is the purpose of PYTHONHOME environment variable?\n", 89 | "\n", 90 | "Solution\n", 91 | "\n", 92 | "- PYTHONHOME − It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy." 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "### 7. Is python a case sensitive language?\n", 100 | "\n", 101 | "Solution\n", 102 | "\n", 103 | "- Yes! Python is a case sensitive programming language." 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "### 8. What are the supported data types in Python?\n", 111 | "\n", 112 | "Solution\n", 113 | "\n", 114 | "- Python has five standard data types:\n", 115 | " 1. Numbers\n", 116 | " 2. String\n", 117 | " 3. List\n", 118 | " 4. Tuple\n", 119 | " 5. Dictionary" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "### 9. What is the output of print `str` if `str = 'Hello World!'`?\n", 127 | "\n", 128 | "Solution\n", 129 | "\n", 130 | "- It will print complete string. \n", 131 | "- Output would be `Hello World!`" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "### 10. What is the output of print `str[0]` if `str = 'Hello World!'`?\n", 139 | "\n", 140 | "Solution\n", 141 | "\n", 142 | "- It will print first character of the string. Output would be H." 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "### 11. What is the output of print `str[2:5]` if `str = 'Hello World!'`?\n", 150 | "\n", 151 | "Solution\n", 152 | "\n", 153 | "- It will print characters starting from 3rd to 5th. \n", 154 | "- Output would be `llo`" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "### 12. What is the output of print `str[2:]` if `str = 'Hello World!'`?\n", 162 | "\n", 163 | "Solution\n", 164 | "\n", 165 | "- It will print characters starting from 3rd character. \n", 166 | "- Output would be `llo World!`" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "### 13. What is the output of print `str * 2` if `str = 'Hello World!'`?\n", 174 | "\n", 175 | "Solution\n", 176 | "\n", 177 | "- It will print string two times. \n", 178 | "- Output would be `Hello World!Hello World!`" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "### 14. What is the output of print `str + \"TEST\"` if `str = 'Hello World!'`?\n", 186 | "\n", 187 | "Solution\n", 188 | "\n", 189 | "- It will print concatenated string. \n", 190 | "- Output would be `Hello World!TEST`" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "### 15. What is the output of print `list` if `list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]`?\n", 198 | "\n", 199 | "Solution\n", 200 | "\n", 201 | "- It will print complete list. \n", 202 | "- Output would be `['abcd', 786, 2.23, 'john', 70.200000000000003]`" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "### 16. What is the output of print `list[0]` if `list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]`?\n", 210 | "\n", 211 | "Solution\n", 212 | "\n", 213 | "- It will print first element of the list. \n", 214 | "- Output would be `abcd`" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "### 17. What is the output of print `list[1:3]` if `list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]`?\n", 222 | "\n", 223 | "Solution\n", 224 | "\n", 225 | "- It will print elements starting from 2nd till 3rd. \n", 226 | "- Output would be `[786, 2.23]`" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "### 18. What is the output of print `list[2:]` if `list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]`?\n", 234 | "\n", 235 | "Solution\n", 236 | "\n", 237 | "- It will print elements starting from 3rd element. \n", 238 | "- Output would be `[2.23, 'john', 70.200000000000003]`" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "### 19. What is the output of print `tinylist * 2` if `tinylist = [123, 'john']`?\n", 246 | "\n", 247 | "Solution\n", 248 | "\n", 249 | "- It will print list two times. \n", 250 | "- Output would be `[123, 'john', 123, 'john']`" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "### 20. What is the output of print `list1 + list2`, if `list1 = [ 'abcd', 786 , 2.23, 'john', 70.2 ] and ist2 = [123, 'john']`?\n", 258 | "\n", 259 | "Solution\n", 260 | "\n", 261 | "- It will print concatenated lists. \n", 262 | "- Output would be `['abcd', 786, 2.23, 'john', 70.2, 123, 'john']`" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "### 21. What are tuples in Python?\n", 270 | "\n", 271 | "Solution\n", 272 | "\n", 273 | "- A tuple is another sequence data type that is similar to the list. \n", 274 | "- A tuple consists of a number of values separated by commas. \n", 275 | "- Unlike lists, however, tuples are enclosed within parentheses." 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### 22. What is the difference between tuples and lists in Python?\n", 283 | "\n", 284 | "Solution\n", 285 | "\n", 286 | "- The main differences between lists and tuples are: \n", 287 | " - Lists are enclosed in brackets `[ ]` and their elements and size can be changed, while tuples are enclosed in parentheses `( )` and cannot be updated. \n", 288 | " - Tuples can be thought of as read-only lists." 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "### 23. What is the output of print `tuple` if `tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )`?\n", 296 | "\n", 297 | "Solution\n", 298 | "\n", 299 | "- It will print complete tuple. \n", 300 | "- Output would be `('abcd', 786, 2.23, 'john', 70.200000000000003)`" 301 | ] 302 | }, 303 | { 304 | "cell_type": "markdown", 305 | "metadata": {}, 306 | "source": [ 307 | "### 24. What is the output of print `tuple[0]` if `tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )`?\n", 308 | "\n", 309 | "Solution\n", 310 | "\n", 311 | "- It will print first element of the tuple. \n", 312 | "- Output would be `abcd`" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "### 25. What is the output of print `tuple[1:3]` if `tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )`?\n", 320 | "\n", 321 | "Solution\n", 322 | "\n", 323 | "- It will print elements starting from 2nd till 3rd. \n", 324 | "- Output would be `(786, 2.23)`" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "### 26. What is the output of print `tuple[2:]` if `tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )`?\n", 332 | "\n", 333 | "Solution\n", 334 | "\n", 335 | "- It will print elements starting from 3rd element. \n", 336 | "- Output would be `(2.23, 'john', 70.200000000000003)`" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "### 27. What is the output of print `tinytuple * 2` if `tinytuple = (123, 'john')`?\n", 344 | "\n", 345 | "Solution\n", 346 | "\n", 347 | "- It will print tuple two times. \n", 348 | "- Output would be `(123, 'john', 123, 'john')`" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "### 28. What is the output of print `tuple + tinytuple` if `tuple = ( 'abcd', 786, 2.23, 'john', 70.2 )` and `tinytuple = (123, 'john')`?\n", 356 | "\n", 357 | "Solution\n", 358 | "\n", 359 | "- It will print concatenated tuples. \n", 360 | "- Output would be `('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')`" 361 | ] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "metadata": {}, 366 | "source": [ 367 | "### 29. What are Python's dictionaries?\n", 368 | "\n", 369 | "Solution\n", 370 | "\n", 371 | "- Python's dictionaries are kind of hash table type. \n", 372 | "- They work like associative arrays or hashes found in Perl and consist of key-value pairs. \n", 373 | "- A dictionary key can be almost any Python type, but are usually numbers or strings. \n", 374 | "- Values, on the other hand, can be any arbitrary Python object." 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "### 30. How will you create a dictionary in python?\n", 382 | "\n", 383 | "Solution\n", 384 | "\n", 385 | "- Dictionaries are enclosed by curly braces `{ }` and values can be assigned and accessed using square braces `[]`.\n", 386 | "\n", 387 | "```python\n", 388 | "dict = {}\n", 389 | "dict['one'] = \"This is one\"\n", 390 | "dict[2] = \"This is two\"\n", 391 | "tinydict = {'name': 'john','code':6734, 'dept': 'sales'}\n", 392 | "```" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "### 31. How will you get all the keys from the dictionary?\n", 400 | "\n", 401 | "Solution\n", 402 | "\n", 403 | "- Using `dictionary.keys()` function, we can get all the keys from the dictionary object.\n", 404 | "\n", 405 | "```python\n", 406 | "print dict.keys() # Prints all the keys\n", 407 | "```" 408 | ] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": {}, 413 | "source": [ 414 | "### 32. How will you get all the values from the dictionary?\n", 415 | "\n", 416 | "Solution\n", 417 | "\n", 418 | "- Using `dictionary.values()` function, we can get all the values from the dictionary object.\n", 419 | "\n", 420 | "```python\n", 421 | "print dict.values() # Prints all the values\n", 422 | "```" 423 | ] 424 | }, 425 | { 426 | "cell_type": "markdown", 427 | "metadata": {}, 428 | "source": [ 429 | "### 33. How will you convert a string to an int in python?\n", 430 | "\n", 431 | "Solution\n", 432 | "\n", 433 | "- `int(x [,base])` - Converts `x` to an integer. `base` specifies the base if `x` is a string." 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": {}, 439 | "source": [ 440 | "### 34. How will you convert a string to a long in python?\n", 441 | "\n", 442 | "Solution\n", 443 | "\n", 444 | "- `long(x [,base] )` - Converts `x` to a long integer. `base` specifies the base if `x` is a string." 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "### 35. How will you convert a string to a float in python?\n", 452 | "\n", 453 | "Solution\n", 454 | "\n", 455 | "- `float(x)` − Converts `x` to a floating-point number." 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": {}, 461 | "source": [ 462 | "### 36. How will you convert a object to a string in python?\n", 463 | "\n", 464 | "Solution\n", 465 | "\n", 466 | "- `str(x)` − Converts object `x` to a string representation." 467 | ] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "metadata": {}, 472 | "source": [ 473 | "### 37. How will you convert a object to a regular expression in python?\n", 474 | "\n", 475 | "Solution\n", 476 | "\n", 477 | "- `repr(x)` − Converts object `x` to an expression string." 478 | ] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "metadata": {}, 483 | "source": [ 484 | "### 38. How will you convert a String to an object in python?\n", 485 | "\n", 486 | "Solution\n", 487 | "\n", 488 | "- `eval(str)` − Evaluates a string and returns an object." 489 | ] 490 | }, 491 | { 492 | "cell_type": "markdown", 493 | "metadata": {}, 494 | "source": [ 495 | "### 39. How will you convert a string to a tuple in python?\n", 496 | "\n", 497 | "Solution\n", 498 | "\n", 499 | "- `tuple(s)` − Converts `s` to a tuple." 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "### 40. How will you convert a string to a list in python?\n", 507 | "\n", 508 | "Solution\n", 509 | "\n", 510 | "- `list(s)` − Converts `s` to a list." 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "### 41. How will you convert a string to a set in python?\n", 518 | "\n", 519 | "Solution\n", 520 | "\n", 521 | "- `set(s)` − Converts `s` to a set." 522 | ] 523 | }, 524 | { 525 | "cell_type": "markdown", 526 | "metadata": {}, 527 | "source": [ 528 | "### 42. How will you create a dictionary using tuples in python?\n", 529 | "\n", 530 | "Solution\n", 531 | "\n", 532 | "- `dict(d)` − Creates a dictionary. `d` must be a sequence of (key,value) tuples." 533 | ] 534 | }, 535 | { 536 | "cell_type": "markdown", 537 | "metadata": {}, 538 | "source": [ 539 | "### 43. How will you convert a string to a frozen set in python?\n", 540 | "\n", 541 | "Solution\n", 542 | "\n", 543 | "- `frozenset(s)` − Converts `s` to a frozen set." 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "metadata": {}, 549 | "source": [ 550 | "### 44. How will you convert an integer to a character in python?\n", 551 | "\n", 552 | "Solution\n", 553 | "\n", 554 | "- `chr(x)` − Converts an integer to a character." 555 | ] 556 | }, 557 | { 558 | "cell_type": "markdown", 559 | "metadata": {}, 560 | "source": [ 561 | "### 45. How will you convert an integer to an unicode character in python?\n", 562 | "\n", 563 | "Solution\n", 564 | "\n", 565 | "- `unichr(x)` − Converts an integer to a Unicode character." 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "### 46. How will you convert a single character to its integer value in python?\n", 573 | "\n", 574 | "Solution\n", 575 | "\n", 576 | "- `ord(x)` − Converts a single character to its integer value." 577 | ] 578 | }, 579 | { 580 | "cell_type": "markdown", 581 | "metadata": {}, 582 | "source": [ 583 | "### 47. How will you convert an integer to hexadecimal string in python?\n", 584 | "\n", 585 | "Solution\n", 586 | "\n", 587 | "- `hex(x)` − Converts an integer to a hexadecimal string." 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "metadata": {}, 593 | "source": [ 594 | "### 48. How will you convert an integer to octal string in python?\n", 595 | "\n", 596 | "Solution\n", 597 | "\n", 598 | "- `oct(x)` − Converts an integer to an octal string." 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": {}, 604 | "source": [ 605 | "### 49. What is the purpose of `**` operator?\n", 606 | "\n", 607 | "Solution\n", 608 | "\n", 609 | "- `**` Exponent − Performs exponential (power) calculation on operators. \n", 610 | "- `a**b` = 10 to the power 20 if `a = 10` and `b = 20`" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "### 50. What is the purpose of `//` operator?\n", 618 | "\n", 619 | "Solution\n", 620 | "\n", 621 | "- `//` Floor Division − The division of operands where the result is the quotient in which the digits after the decimal point are removed." 622 | ] 623 | }, 624 | { 625 | "cell_type": "markdown", 626 | "metadata": {}, 627 | "source": [ 628 | "### 51. What is the purpose of `is` operator?\n", 629 | "\n", 630 | "Solution\n", 631 | "\n", 632 | "- `is` − Evaluates to `True` if the variables on either side of the operator point to the same object and false otherwise. `x` is `y`, here is results in 1 if `id(x)` equals `id(y)`." 633 | ] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "metadata": {}, 638 | "source": [ 639 | "### 52. What is the purpose of `not in` operator?\n", 640 | "\n", 641 | "Solution\n", 642 | "\n", 643 | "- `not in` − Evaluates to `True` if it does not finds a variable in the specified sequence and false otherwise. `x` not in `y`, here not in results in a 1 if `x` is not a member of sequence `y`." 644 | ] 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "metadata": {}, 649 | "source": [ 650 | "### 53. What is the purpose `break` statement in python?\n", 651 | "\n", 652 | "Solution\n", 653 | "\n", 654 | "- `break` statement − Terminates the loop statement and transfers execution to the statement immediately following the loop." 655 | ] 656 | }, 657 | { 658 | "cell_type": "markdown", 659 | "metadata": {}, 660 | "source": [ 661 | "### 54. What is the purpose `continue` statement in python?\n", 662 | "\n", 663 | "Solution\n", 664 | "\n", 665 | "- `continue` statement − Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating." 666 | ] 667 | }, 668 | { 669 | "cell_type": "markdown", 670 | "metadata": {}, 671 | "source": [ 672 | "### 55. What is the purpose `pass` statement in python?\n", 673 | "\n", 674 | "Solution\n", 675 | "\n", 676 | "- `pass` statement − The `pass` statement in Python is used when a statement is required syntactically but you do not want any command or code to execute." 677 | ] 678 | }, 679 | { 680 | "cell_type": "markdown", 681 | "metadata": {}, 682 | "source": [ 683 | "### 56. How can you pick a random item from a list or tuple?\n", 684 | "\n", 685 | "Solution\n", 686 | "\n", 687 | "- `choice(seq)` − Returns a random item from a list, tuple, or string." 688 | ] 689 | }, 690 | { 691 | "cell_type": "markdown", 692 | "metadata": {}, 693 | "source": [ 694 | "### 57. How can you pick a random item from a range?\n", 695 | "\n", 696 | "Solution\n", 697 | "\n", 698 | "- `randrange ([start,] stop [,step])` − returns a randomly selected element from range(start, stop, step)." 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "### 58. How can you get a random number in python?\n", 706 | "\n", 707 | "Solution\n", 708 | "\n", 709 | "- `random()` − returns a random float `r`, such that 0 is less than or equal to `r` and `r` is less than 1." 710 | ] 711 | }, 712 | { 713 | "cell_type": "markdown", 714 | "metadata": {}, 715 | "source": [ 716 | "### 59. How will you set the starting value in generating random numbers?\n", 717 | "\n", 718 | "Solution\n", 719 | "\n", 720 | "- `seed([x])` − Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns `None`." 721 | ] 722 | }, 723 | { 724 | "cell_type": "markdown", 725 | "metadata": {}, 726 | "source": [ 727 | "### 60. How will you randomizes the items of a list in place?\n", 728 | "\n", 729 | "Solution\n", 730 | "\n", 731 | "- `shuffle(lst)` − Randomizes the items of a list in place. Returns `None`." 732 | ] 733 | }, 734 | { 735 | "cell_type": "markdown", 736 | "metadata": {}, 737 | "source": [ 738 | "### 61. How will you capitalizes first letter of string?\n", 739 | "\n", 740 | "Solution\n", 741 | "\n", 742 | "- `capitalize()` − Capitalizes first letter of string." 743 | ] 744 | }, 745 | { 746 | "cell_type": "markdown", 747 | "metadata": {}, 748 | "source": [ 749 | "### 62. How will you check in a string that all characters are alphanumeric?\n", 750 | "\n", 751 | "Solution\n", 752 | "\n", 753 | "- `isalnum()` − Returns `True` if string has at least 1 character and all characters are alphanumeric and `False` otherwise." 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "### 63. How will you check in a string that all characters are digits?\n", 761 | "\n", 762 | "Solution\n", 763 | "\n", 764 | "- `isdigit()` − Returns `True` if string contains only digits and `False` otherwise." 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": {}, 770 | "source": [ 771 | "### 64. How will you check in a string that all characters are in lowercase?\n", 772 | "\n", 773 | "Solution\n", 774 | "\n", 775 | "- `islower()` − Returns `True` if string has at least 1 cased character and all cased characters are in lowercase and `False` otherwise." 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "### 65. How will you check in a string that all characters are numerics?\n", 783 | "\n", 784 | "Solution\n", 785 | "\n", 786 | "- `isnumeric()` − Returns `True` if a unicode string contains only numeric characters and `False` otherwise." 787 | ] 788 | }, 789 | { 790 | "cell_type": "markdown", 791 | "metadata": {}, 792 | "source": [ 793 | "### 66. How will you check in a string that all characters are whitespaces?\n", 794 | "\n", 795 | "Solution\n", 796 | "\n", 797 | "- `isspace()` − Returns `True` if string contains only whitespace characters and `False` otherwise." 798 | ] 799 | }, 800 | { 801 | "cell_type": "markdown", 802 | "metadata": {}, 803 | "source": [ 804 | "### 67. How will you check in a string that it is properly titlecased?\n", 805 | "\n", 806 | "Solution\n", 807 | "\n", 808 | "- `istitle()` − Returns `True` if string is properly \"titlecased\" and `False` otherwise." 809 | ] 810 | }, 811 | { 812 | "cell_type": "markdown", 813 | "metadata": {}, 814 | "source": [ 815 | "### 68. How will you check in a string that all characters are in uppercase?\n", 816 | "\n", 817 | "Solution\n", 818 | "\n", 819 | "- `isupper()` − Returns `True` if string has at least one cased character and all cased characters are in uppercase and `False` otherwise." 820 | ] 821 | }, 822 | { 823 | "cell_type": "markdown", 824 | "metadata": {}, 825 | "source": [ 826 | "### 69. How will you merge elements in a sequence?\n", 827 | "\n", 828 | "Solution\n", 829 | "\n", 830 | "- `join(seq)` − Merges (concatenates) the string representations of elements in sequence `seq` into a string, with separator string." 831 | ] 832 | }, 833 | { 834 | "cell_type": "markdown", 835 | "metadata": {}, 836 | "source": [ 837 | "### 70. How will you get the length of the string?\n", 838 | "\n", 839 | "Solution\n", 840 | "\n", 841 | "- `len(string)` − Returns the length of the string." 842 | ] 843 | }, 844 | { 845 | "cell_type": "markdown", 846 | "metadata": {}, 847 | "source": [ 848 | "### 71. How will you get a space-padded string with the original string left-justified to a total of width columns?\n", 849 | "\n", 850 | "Solution\n", 851 | "\n", 852 | "- `ljust(width[, fillchar])` − Returns a space-padded string with the original string left-justified to a total of width columns." 853 | ] 854 | }, 855 | { 856 | "cell_type": "markdown", 857 | "metadata": {}, 858 | "source": [ 859 | "### 72. How will you convert a string to all lowercase?\n", 860 | "\n", 861 | "Solution\n", 862 | "\n", 863 | "- `lower()` − Converts all uppercase letters in string to lowercase." 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "### 73. How will you remove all leading whitespace in string?\n", 871 | "\n", 872 | "Solution\n", 873 | "\n", 874 | "- `lstrip()` − Removes all leading whitespace in string." 875 | ] 876 | }, 877 | { 878 | "cell_type": "markdown", 879 | "metadata": {}, 880 | "source": [ 881 | "### 74. How will you get the max alphabetical character from the string?\n", 882 | "\n", 883 | "Solution\n", 884 | "\n", 885 | "- `max(str)` − Returns the `max` alphabetical character from the string `str`." 886 | ] 887 | }, 888 | { 889 | "cell_type": "markdown", 890 | "metadata": {}, 891 | "source": [ 892 | "### 75. How will you get the min alphabetical character from the string?\n", 893 | "\n", 894 | "Solution\n", 895 | "\n", 896 | "- ``min(str)` − Returns the `min` alphabetical character from the string `str`." 897 | ] 898 | }, 899 | { 900 | "cell_type": "markdown", 901 | "metadata": {}, 902 | "source": [ 903 | "### 76. How will you replaces all occurrences of old substring in string with new string?\n", 904 | "\n", 905 | "Solution\n", 906 | "\n", 907 | "- `replace(old, new [, max])` − Replaces all occurrences of old in string with new or at most max occurrences if `max` given." 908 | ] 909 | }, 910 | { 911 | "cell_type": "markdown", 912 | "metadata": {}, 913 | "source": [ 914 | "### 77. How will you remove all leading and trailing whitespace in string?\n", 915 | "\n", 916 | "Solution\n", 917 | "\n", 918 | "- `strip([chars])` − Performs both `lstrip()` and `rstrip()` on string." 919 | ] 920 | }, 921 | { 922 | "cell_type": "markdown", 923 | "metadata": {}, 924 | "source": [ 925 | "### 78. How will you change case for all letters in string?\n", 926 | "\n", 927 | "Solution\n", 928 | "\n", 929 | "- `swapcase()` − Inverts case for all letters in string." 930 | ] 931 | }, 932 | { 933 | "cell_type": "markdown", 934 | "metadata": {}, 935 | "source": [ 936 | "### 79. How will you get titlecased version of string?\n", 937 | "\n", 938 | "Solution\n", 939 | "\n", 940 | "- `title()` − Returns \"titlecased\" version of string, that is, all words begin with uppercase and the rest are lowercase." 941 | ] 942 | }, 943 | { 944 | "cell_type": "markdown", 945 | "metadata": {}, 946 | "source": [ 947 | "### 80. How will you convert a string to all uppercase?\n", 948 | "\n", 949 | "Solution\n", 950 | "\n", 951 | "- `upper()` − Converts all lowercase letters in string to uppercase." 952 | ] 953 | }, 954 | { 955 | "cell_type": "markdown", 956 | "metadata": {}, 957 | "source": [ 958 | "### 81. How will you check in a string that all characters are decimal?\n", 959 | "\n", 960 | "Solution\n", 961 | "\n", 962 | "- `isdecimal()` − Returns `True` if a unicode string contains only decimal characters and `False` otherwise." 963 | ] 964 | }, 965 | { 966 | "cell_type": "markdown", 967 | "metadata": {}, 968 | "source": [ 969 | "### 82. What is the difference between `del()` and `remove()` methods of list?\n", 970 | "\n", 971 | "Solution\n", 972 | "\n", 973 | "- To remove a list element, you can use either the `del` statement if you know exactly which element(s) you are deleting or the `remove()` method if you do not know." 974 | ] 975 | }, 976 | { 977 | "cell_type": "markdown", 978 | "metadata": {}, 979 | "source": [ 980 | "### 83. What is the output of `len([1, 2, 3])`?\n", 981 | "\n", 982 | "Solution\n", 983 | "\n", 984 | "- `3`" 985 | ] 986 | }, 987 | { 988 | "cell_type": "markdown", 989 | "metadata": {}, 990 | "source": [ 991 | "### 84. What is the output of `[1, 2, 3] + [4, 5, 6]`?\n", 992 | "\n", 993 | "Solution\n", 994 | "\n", 995 | "- `[1, 2, 3, 4, 5, 6]`" 996 | ] 997 | }, 998 | { 999 | "cell_type": "markdown", 1000 | "metadata": {}, 1001 | "source": [ 1002 | "### 85. What is the output of `['Hi!'] * 4`?\n", 1003 | "\n", 1004 | "Solution\n", 1005 | "\n", 1006 | "- `['Hi!', 'Hi!', 'Hi!', 'Hi!']`" 1007 | ] 1008 | }, 1009 | { 1010 | "cell_type": "markdown", 1011 | "metadata": {}, 1012 | "source": [ 1013 | "### 86. What is the output of 3 in `[1, 2, 3]`?\n", 1014 | "\n", 1015 | "Solution\n", 1016 | "\n", 1017 | "- `True`" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "markdown", 1022 | "metadata": {}, 1023 | "source": [ 1024 | "### 87. What is the output of for `x in [1, 2, 3]: print x`?\n", 1025 | "\n", 1026 | "Solution\n", 1027 | "\n", 1028 | "```python\n", 1029 | "1\n", 1030 | "2\n", 1031 | "3\n", 1032 | "```" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "markdown", 1037 | "metadata": {}, 1038 | "source": [ 1039 | "### 88. What is the output of `L[2]` if `L = [1,2,3]`?\n", 1040 | "\n", 1041 | "Solution\n", 1042 | "\n", 1043 | "- `3`, Offsets start at zero." 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "markdown", 1048 | "metadata": {}, 1049 | "source": [ 1050 | "### 89. What is the output of `L[-2]` if `L = [1,2,3]`?\n", 1051 | "\n", 1052 | "Solution\n", 1053 | "\n", 1054 | "- `1`, Negative: count from the right." 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "markdown", 1059 | "metadata": {}, 1060 | "source": [ 1061 | "### 90. What is the output of `L[1:]` if `L = [1,2,3]`?\n", 1062 | "\n", 1063 | "Solution\n", 1064 | "\n", 1065 | "- `2, 3`, Slicing fetches sections." 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "markdown", 1070 | "metadata": {}, 1071 | "source": [ 1072 | "### 91. How will you compare two lists?\n", 1073 | "\n", 1074 | "Solution\n", 1075 | "\n", 1076 | "- `cmp(list1, list2)` − Compares elements of both lists." 1077 | ] 1078 | }, 1079 | { 1080 | "cell_type": "markdown", 1081 | "metadata": {}, 1082 | "source": [ 1083 | "### 92. How will you get the length of a list?\n", 1084 | "\n", 1085 | "Solution\n", 1086 | "\n", 1087 | "- `len(list)` − Gives the total length of the list." 1088 | ] 1089 | }, 1090 | { 1091 | "cell_type": "markdown", 1092 | "metadata": {}, 1093 | "source": [ 1094 | "### 93. How will you get the max valued item of a list?\n", 1095 | "\n", 1096 | "Solution\n", 1097 | "\n", 1098 | "- `max(list)` − Returns item from the list with max value." 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "markdown", 1103 | "metadata": {}, 1104 | "source": [ 1105 | "### 94. How will you get the min valued item of a list?\n", 1106 | "\n", 1107 | "Solution\n", 1108 | "\n", 1109 | "- `min(list)` − Returns item from the list with min value." 1110 | ] 1111 | }, 1112 | { 1113 | "cell_type": "markdown", 1114 | "metadata": {}, 1115 | "source": [ 1116 | "### 95. How will you get the index of an object in a list?\n", 1117 | "\n", 1118 | "Solution\n", 1119 | "\n", 1120 | "- `list.index(obj)` − Returns the lowest index in list that `obj` appears." 1121 | ] 1122 | }, 1123 | { 1124 | "cell_type": "markdown", 1125 | "metadata": {}, 1126 | "source": [ 1127 | "### 96. How will you insert an object at given index in a list?\n", 1128 | "\n", 1129 | "Solution\n", 1130 | "\n", 1131 | "- `list.insert(index, obj)` − Inserts object `obj` into list at offset index." 1132 | ] 1133 | }, 1134 | { 1135 | "cell_type": "markdown", 1136 | "metadata": {}, 1137 | "source": [ 1138 | "### 97. How will you remove last object from a list?\n", 1139 | "\n", 1140 | "Solution\n", 1141 | "\n", 1142 | "`list.pop(obj=list[-1])` − Removes and returns last object or obj from list." 1143 | ] 1144 | }, 1145 | { 1146 | "cell_type": "markdown", 1147 | "metadata": {}, 1148 | "source": [ 1149 | "### 98. How will you remove an object from a list?\n", 1150 | "\n", 1151 | "Solution\n", 1152 | "\n", 1153 | "- `list.remove(obj)` − Removes object `obj` from list." 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "markdown", 1158 | "metadata": {}, 1159 | "source": [ 1160 | "### 99. How will you reverse a list?\n", 1161 | "\n", 1162 | "Solution\n", 1163 | "\n", 1164 | "- `list.reverse()` − Reverses objects of list in place." 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "markdown", 1169 | "metadata": {}, 1170 | "source": [ 1171 | "### 100. How will you sort a list?\n", 1172 | "\n", 1173 | "Solution\n", 1174 | "\n", 1175 | "- `list.sort([func])` − Sorts objects of list, use compare `func` if given." 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "markdown", 1180 | "metadata": {}, 1181 | "source": [ 1182 | "### 101. What is lambda function in python?\n", 1183 | "\n", 1184 | "Solution\n", 1185 | "\n", 1186 | "- `‘lambda’` is a keyword in python which creates an anonymous function. Lambda does not contain block of statements. It does not contain return statements." 1187 | ] 1188 | }, 1189 | { 1190 | "cell_type": "markdown", 1191 | "metadata": {}, 1192 | "source": [ 1193 | "### 102. What we call a function which is incomplete version of a function?\n", 1194 | "\n", 1195 | "Solution\n", 1196 | "\n", 1197 | "- `Stub`." 1198 | ] 1199 | }, 1200 | { 1201 | "cell_type": "markdown", 1202 | "metadata": {}, 1203 | "source": [ 1204 | "### 103. When a function is defined then the system stores parameters and local variables in an area of memory. What this memory is known as?\n", 1205 | "\n", 1206 | "Solution\n", 1207 | "\n", 1208 | "- `Stack`." 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "markdown", 1213 | "metadata": {}, 1214 | "source": [ 1215 | "### 104. A canvas can have a foreground color? (Yes/No)\n", 1216 | "\n", 1217 | "Solution\n", 1218 | "\n", 1219 | "- `Yes`." 1220 | ] 1221 | }, 1222 | { 1223 | "cell_type": "markdown", 1224 | "metadata": {}, 1225 | "source": [ 1226 | "### 105. Is Python platform independent?\n", 1227 | "\n", 1228 | "Solution\n", 1229 | "\n", 1230 | "- No. There are some modules and functions in python that can only run on certain platforms." 1231 | ] 1232 | }, 1233 | { 1234 | "cell_type": "markdown", 1235 | "metadata": {}, 1236 | "source": [ 1237 | "### 106. Do you think Python has a complier?\n", 1238 | "\n", 1239 | "Solution\n", 1240 | "\n", 1241 | "- Yes. Python complier which works automatically so we don’t notice the compiler of python." 1242 | ] 1243 | }, 1244 | { 1245 | "cell_type": "markdown", 1246 | "metadata": {}, 1247 | "source": [ 1248 | "### 107. What are the applications of Python?\n", 1249 | "\n", 1250 | "Solution\n", 1251 | "\n", 1252 | "1. Django (Web framework of Python).\n", 1253 | "\n", 1254 | "2. Micro Frame work such as Flask and Bottle.\n", 1255 | "\n", 1256 | "3. Plone and Django CMS for advanced content Management." 1257 | ] 1258 | }, 1259 | { 1260 | "cell_type": "markdown", 1261 | "metadata": {}, 1262 | "source": [ 1263 | "### 108. What is the basic difference between Python ver 2 and Python ver 3?\n", 1264 | "\n", 1265 | "Solution\n", 1266 | "\n", 1267 | "- Table below explains the difference between Python version 2 and Python version 3.\n", 1268 | "\n", 1269 | "| S.No | Section | Python Version 2 | Python Version 3 | \n", 1270 | "|:-------|:---------------| :------ |:--------|\n", 1271 | "| 1. | Print Function | Print command can be used without parentheses. | Python 3 needs parentheses to print any string. It will raise error without parentheses. | \n", 1272 | "| 2. | Unicode | ASCII str() types and separate Unicode() but there is no byte type code in Python 2. | Unicode (utf-8) and it has two byte classes − Byte, Bytearray S. |\n", 1273 | "| 3. | Exceptions | Python 2 accepts both new and old notations of syntax. | Python 3 raises a SyntaxError in turn when we don’t enclose the exception argument in parentheses. |\n", 1274 | "| 4. | Comparing Unorderable | It does not raise any error. | It raises ‘TypeError’ as warning if we try to compare unorderable types. |" 1275 | ] 1276 | }, 1277 | { 1278 | "cell_type": "markdown", 1279 | "metadata": {}, 1280 | "source": [ 1281 | "### 109. Which programming Language is an implementation of Python programming language designed to run on Java Platform?\n", 1282 | "\n", 1283 | "Solution\n", 1284 | "\n", 1285 | "- `Jython`. (Jython is successor of Jpython.)" 1286 | ] 1287 | }, 1288 | { 1289 | "cell_type": "markdown", 1290 | "metadata": {}, 1291 | "source": [ 1292 | "### 110. Is there any double data type in Python?\n", 1293 | "\n", 1294 | "Solution\n", 1295 | "\n", 1296 | "- `No`." 1297 | ] 1298 | }, 1299 | { 1300 | "cell_type": "markdown", 1301 | "metadata": {}, 1302 | "source": [ 1303 | "### 111. Is String in Python are immutable? (Yes/No)\n", 1304 | "\n", 1305 | "Solution\n", 1306 | "\n", 1307 | "- `Yes`." 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "markdown", 1312 | "metadata": {}, 1313 | "source": [ 1314 | "### 112. Can `True = False` be possible in Python?\n", 1315 | "\n", 1316 | "Solution\n", 1317 | "\n", 1318 | "- `No`." 1319 | ] 1320 | }, 1321 | { 1322 | "cell_type": "markdown", 1323 | "metadata": {}, 1324 | "source": [ 1325 | "### 113. Which module of python is used to apply the methods related to OS.?\n", 1326 | "\n", 1327 | "Solution\n", 1328 | "\n", 1329 | "- `OS`." 1330 | ] 1331 | }, 1332 | { 1333 | "cell_type": "markdown", 1334 | "metadata": {}, 1335 | "source": [ 1336 | "### 114. When does a new block begin in python?\n", 1337 | "\n", 1338 | "Solution\n", 1339 | "\n", 1340 | "- A block begins when the line is intended by 4 spaces." 1341 | ] 1342 | }, 1343 | { 1344 | "cell_type": "markdown", 1345 | "metadata": {}, 1346 | "source": [ 1347 | "### 115. Write a function in python which detects whether the given two strings are anagrams or not.\n", 1348 | "\n", 1349 | "Solution" 1350 | ] 1351 | }, 1352 | { 1353 | "cell_type": "code", 1354 | "execution_count": 1, 1355 | "metadata": { 1356 | "ExecuteTime": { 1357 | "end_time": "2021-09-22T07:53:06.172548Z", 1358 | "start_time": "2021-09-22T07:53:06.155950Z" 1359 | } 1360 | }, 1361 | "outputs": [], 1362 | "source": [ 1363 | "def check(a,b):\n", 1364 | " if(len(a)!=len(b)):\n", 1365 | " return False\n", 1366 | " else:\n", 1367 | " if(sorted(list(a)) == sorted(list(b))):\n", 1368 | " return True\n", 1369 | " else:\n", 1370 | " return False" 1371 | ] 1372 | }, 1373 | { 1374 | "cell_type": "markdown", 1375 | "metadata": {}, 1376 | "source": [ 1377 | "### 116. Name the python Library used for Machine learning.\n", 1378 | "\n", 1379 | "Solution\n", 1380 | "\n", 1381 | "- Scikit-learn python Library used for Machine learning" 1382 | ] 1383 | }, 1384 | { 1385 | "cell_type": "markdown", 1386 | "metadata": {}, 1387 | "source": [ 1388 | "### 117. What does `pass` operation do?\n", 1389 | "\n", 1390 | "Solution\n", 1391 | "\n", 1392 | "- `pass` indicates that nothing is to be done i.e., it signifies a no operation." 1393 | ] 1394 | }, 1395 | { 1396 | "cell_type": "markdown", 1397 | "metadata": {}, 1398 | "source": [ 1399 | "### 118. Name the tools which python uses to find bugs (if any).\n", 1400 | "\n", 1401 | "Solution\n", 1402 | "\n", 1403 | "- `Pylint` and `pychecker`." 1404 | ] 1405 | }, 1406 | { 1407 | "cell_type": "markdown", 1408 | "metadata": {}, 1409 | "source": [ 1410 | "### 119. Write a function to give the sum of all the numbers in list?\n", 1411 | "\n", 1412 | "Solution\n", 1413 | "\n", 1414 | "Sample list − (100, 200, 300, 400, 0, 500)\n", 1415 | "\n", 1416 | "Expected output − 1500" 1417 | ] 1418 | }, 1419 | { 1420 | "cell_type": "code", 1421 | "execution_count": 2, 1422 | "metadata": { 1423 | "ExecuteTime": { 1424 | "end_time": "2021-09-22T07:53:06.492373Z", 1425 | "start_time": "2021-09-22T07:53:06.180366Z" 1426 | } 1427 | }, 1428 | "outputs": [ 1429 | { 1430 | "name": "stdout", 1431 | "output_type": "stream", 1432 | "text": [ 1433 | "Sum of the numbers: 1500\n" 1434 | ] 1435 | } 1436 | ], 1437 | "source": [ 1438 | "# Program for sum of all the numbers in list is −\n", 1439 | "\n", 1440 | "def sum(numbers):\n", 1441 | " total = 0\n", 1442 | " for num in numbers:\n", 1443 | " total+=num\n", 1444 | " print(\"Sum of the numbers: \", total)\n", 1445 | "sum((100, 200, 300, 400, 0, 500))\n", 1446 | "\n", 1447 | "# We define a function ‘sum’ with numbers as parameter. \n", 1448 | "#The in for loop we store the sum of all the values of list." 1449 | ] 1450 | }, 1451 | { 1452 | "cell_type": "markdown", 1453 | "metadata": {}, 1454 | "source": [ 1455 | "### 120. Write a program in Python to reverse a string without using inbuilt function reverse string?\n", 1456 | "\n", 1457 | "Solution\n" 1458 | ] 1459 | }, 1460 | { 1461 | "cell_type": "code", 1462 | "execution_count": 3, 1463 | "metadata": { 1464 | "ExecuteTime": { 1465 | "end_time": "2021-09-22T07:53:06.631533Z", 1466 | "start_time": "2021-09-22T07:53:06.498233Z" 1467 | }, 1468 | "scrolled": true 1469 | }, 1470 | "outputs": [ 1471 | { 1472 | "name": "stdout", 1473 | "output_type": "stream", 1474 | "text": [ 1475 | "The length of string is: 6\n", 1476 | "point1\n" 1477 | ] 1478 | } 1479 | ], 1480 | "source": [ 1481 | "# Reverse a string without using reverse() function\n", 1482 | "\n", 1483 | "def string_reverse(string):\n", 1484 | " i = len(string) - 1\n", 1485 | " print (\"The length of string is: \", len(string))\n", 1486 | " sNew = ''\n", 1487 | " while i >= 0:\n", 1488 | " sNew = sNew + str(string[i])\n", 1489 | " i = i -1\n", 1490 | " return sNew\n", 1491 | "print(string_reverse(\"1tniop\"))\n", 1492 | "\n", 1493 | "# First we declare a variable to store the reverse string. \n", 1494 | "# Then using while loop and indexing of string (index is calculated by string length) \n", 1495 | "# we reverse the string. While loop starts when index is greater than zero. \n", 1496 | "# Index is reduced to value 1 each time. When index reaches zero we obtain the reverse of string." 1497 | ] 1498 | }, 1499 | { 1500 | "cell_type": "markdown", 1501 | "metadata": {}, 1502 | "source": [ 1503 | "### 121. Write a program to test whether the number is in the defined range or not?\n", 1504 | "\n", 1505 | "Solution" 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "code", 1510 | "execution_count": 4, 1511 | "metadata": { 1512 | "ExecuteTime": { 1513 | "end_time": "2021-09-22T07:53:06.735537Z", 1514 | "start_time": "2021-09-22T07:53:06.643256Z" 1515 | }, 1516 | "scrolled": true 1517 | }, 1518 | "outputs": [ 1519 | { 1520 | "name": "stdout", 1521 | "output_type": "stream", 1522 | "text": [ 1523 | "99 is in range\n" 1524 | ] 1525 | } 1526 | ], 1527 | "source": [ 1528 | "# Program is −\n", 1529 | "\n", 1530 | "def test_range(num):\n", 1531 | " if num in range(0, 101):\n", 1532 | " print(\"%s is in range\"%str(num))\n", 1533 | " else:\n", 1534 | " print(\"%s is not in range\"%str(num))\n", 1535 | "# print(\"The number is outside the given range.\")\n", 1536 | " \n", 1537 | "test_range(99)\n", 1538 | "\n", 1539 | "# To test any number in a particular range we make use of the method ‘if..in’ and else condition." 1540 | ] 1541 | }, 1542 | { 1543 | "cell_type": "markdown", 1544 | "metadata": {}, 1545 | "source": [ 1546 | "### 122. Write a program to calculate number of upper case letters and number of lower case letters?\n", 1547 | "\n", 1548 | "Solution\n", 1549 | "\n", 1550 | "Test on String: 'The quick Brown Fox'" 1551 | ] 1552 | }, 1553 | { 1554 | "cell_type": "code", 1555 | "execution_count": 5, 1556 | "metadata": { 1557 | "ExecuteTime": { 1558 | "end_time": "2021-09-22T07:53:06.848818Z", 1559 | "start_time": "2021-09-22T07:53:06.745304Z" 1560 | }, 1561 | "scrolled": true 1562 | }, 1563 | "outputs": [ 1564 | { 1565 | "name": "stdout", 1566 | "output_type": "stream", 1567 | "text": [ 1568 | "String in testing is: The quick Brown Fox\n", 1569 | "Number of Lower Case characters in String: 3\n", 1570 | "Number of Upper Case characters in String: 13\n" 1571 | ] 1572 | } 1573 | ], 1574 | "source": [ 1575 | "# Program is −\n", 1576 | "\n", 1577 | "def string_test(s):\n", 1578 | " d={\"UPPER_CASE\":0, \"LOWER_CASE\":0}\n", 1579 | " for c in s:\n", 1580 | " if c.isupper():\n", 1581 | " d[\"UPPER_CASE\"]+=1\n", 1582 | " elif c.islower():\n", 1583 | " d[\"LOWER_CASE\"]+=1\n", 1584 | " else:\n", 1585 | " pass\n", 1586 | " print (\"String in testing is: \", s)\n", 1587 | " print (\"Number of Lower Case characters in String: \", d[\"UPPER_CASE\"])\n", 1588 | " print (\"Number of Upper Case characters in String: \", d[\"LOWER_CASE\"])\n", 1589 | "\n", 1590 | "string_test('The quick Brown Fox')\n", 1591 | "\n", 1592 | "# We make use of the methods .isupper() and .islower(). We initialise the count for lower and upper. \n", 1593 | "# Using if and else condition we calculate total number of lower and upper case characters." 1594 | ] 1595 | }, 1596 | { 1597 | "cell_type": "markdown", 1598 | "metadata": {}, 1599 | "source": [ 1600 | "###" 1601 | ] 1602 | }, 1603 | { 1604 | "cell_type": "code", 1605 | "execution_count": null, 1606 | "metadata": {}, 1607 | "outputs": [], 1608 | "source": [] 1609 | } 1610 | ], 1611 | "metadata": { 1612 | "hide_input": false, 1613 | "kernelspec": { 1614 | "display_name": "Python 3", 1615 | "language": "python", 1616 | "name": "python3" 1617 | }, 1618 | "language_info": { 1619 | "codemirror_mode": { 1620 | "name": "ipython", 1621 | "version": 3 1622 | }, 1623 | "file_extension": ".py", 1624 | "mimetype": "text/x-python", 1625 | "name": "python", 1626 | "nbconvert_exporter": "python", 1627 | "pygments_lexer": "ipython3", 1628 | "version": "3.8.8" 1629 | }, 1630 | "toc": { 1631 | "base_numbering": 1, 1632 | "nav_menu": {}, 1633 | "number_sections": true, 1634 | "sideBar": true, 1635 | "skip_h1_title": false, 1636 | "title_cell": "Table of Contents", 1637 | "title_sidebar": "Contents", 1638 | "toc_cell": false, 1639 | "toc_position": {}, 1640 | "toc_section_display": true, 1641 | "toc_window_display": false 1642 | }, 1643 | "varInspector": { 1644 | "cols": { 1645 | "lenName": 16, 1646 | "lenType": 16, 1647 | "lenVar": 40 1648 | }, 1649 | "kernels_config": { 1650 | "python": { 1651 | "delete_cmd_postfix": "", 1652 | "delete_cmd_prefix": "del ", 1653 | "library": "var_list.py", 1654 | "varRefreshCmd": "print(var_dic_list())" 1655 | }, 1656 | "r": { 1657 | "delete_cmd_postfix": ") ", 1658 | "delete_cmd_prefix": "rm(", 1659 | "library": "var_list.r", 1660 | "varRefreshCmd": "cat(var_dic_list()) " 1661 | } 1662 | }, 1663 | "types_to_exclude": [ 1664 | "module", 1665 | "function", 1666 | "builtin_function_or_method", 1667 | "instance", 1668 | "_Feature" 1669 | ], 1670 | "window_display": false 1671 | } 1672 | }, 1673 | "nbformat": 4, 1674 | "nbformat_minor": 4 1675 | } 1676 | -------------------------------------------------------------------------------- /02_Predictive_Modeling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Predictive Modeling ➞ 19 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. (Given a Dataset) Analyze this dataset and give me a model that can predict this response variable." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | "- Problem Determination ➞ Data Cleaning ➞ Feature Engineering ➞ Modeling\n", 38 | "\n", 39 | "- Benchmark Models\n", 40 | " - Linear Regression (Ridge or Lasso) for regression\n", 41 | " - Logistic Regression for Classification\n", 42 | " \n", 43 | "- Advanced Models\n", 44 | " - Random Forest, Boosting Trees, and so on\n", 45 | " - Scikit-Learn, XGBoost, LightGBM, CatBoost\n", 46 | " \n", 47 | "- Determine if the problem is classification or regression.\n", 48 | "\n", 49 | "- Plot and visualize the data.\n", 50 | "\n", 51 | "- Start by fitting a simple model (multivariate regression, logistic regression), do some feature engineering accordingly, and then try some complicated models. Always split the dataset into train, validation, test dataset and use cross validation to check their performance.\n", 52 | "\n", 53 | "- Favor simple models that run quickly and you can easily explain.\n", 54 | "\n", 55 | "- Mention cross validation as a means to evaluate the model." 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "### 2. What could be some issues if the distribution of the test data is significantly different than the distribution of the training data?" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Solution\n", 70 | "\n", 71 | "- The model that has high training accuracy might have low test accuracy. Without further knowledge, it is hard to know which dataset represents the population data and thus the generalizability of the algorithm is hard to measure. This should be mitigated by repeated splitting of train vs. test dataset (as in cross validation).\n", 72 | "- When there is a change in data distribution, this is called the dataset shift. If the train and test data has a different distribution, then the classifier would likely overfit to the train data.\n", 73 | "- This issue can be overcome by using a more general learning method.\n", 74 | "- This can occur when:\n", 75 | " - $P(y|x)$ are the same but $P(x)$ are different. (covariate shift)\n", 76 | " - $P(y|x)$ are different. (concept shift)\n", 77 | "- The causes can be:\n", 78 | " - Training samples are obtained in a biased way. (sample selection bias)\n", 79 | " - Train is different from test because of temporal, spatial changes. (non-stationary environments)\n", 80 | "- Solution to covariate shift\n", 81 | " - importance weighted cv" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "### 3. What are some ways I can make my model more robust to outliers?" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "Solution\n", 96 | "\n", 97 | "- We can have regularization such as L1 or L2 to reduce variance (increase bias).\n", 98 | "- Changes to the algorithm:\n", 99 | " - Use tree-based methods instead of regression methods as they are more resistant to outliers. For statistical tests, use non parametric tests instead of parametric ones.\n", 100 | " - Use robust error metrics such as MAE or Huber Loss instead of MSE.\n", 101 | "- Changes to the data:\n", 102 | " - Winsorizing the data\n", 103 | " - Transforming the data (e.g. log)\n", 104 | " - Remove them only if you’re certain they’re anomalies and not worth predicting" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "### What are some differences you would expect in a model that minimizes squared error, versus a model that minimizes absolute error? In which cases would each error metric be appropriate?" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "Solution\n", 119 | "\n", 120 | "- MSE is more strict to having outliers. MAE is more robust in that sense, but is harder to fit the model for because it cannot be numerically optimized. So when there are less variability in the model and the model is computationally easy to fit, we should use MAE, and if that’s not the case, we should use MSE.\n", 121 | "- MSE: easier to compute the gradient, MAE: linear programming needed to compute the gradient\n", 122 | "- MAE more robust to outliers. If the consequences of large errors are great, use MSE\n", 123 | "- MSE corresponds to maximizing likelihood of Gaussian random variables" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "### 5. What error metric would you use to evaluate how good a binary classifier is? What if the classes are imbalanced? What if there are more than 2 groups?" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Solution\n", 138 | "\n", 139 | "- Accuracy: proportion of instances you predict correctly.\n", 140 | " - Pros: intuitive, easy to explain\n", 141 | " - Cons: works poorly when the class labels are imbalanced and the signal from the data is weak\n", 142 | "- ROC curve and AUC: plot false-positive-rate (fpr) on the x axis and true-positive-rate (tpr) on the y axis for different threshold. Given a random positive instance and a random negative instance, the AUC is the probability that you can identify who's who.\n", 143 | " - Pros: Works well when testing the ability of distinguishing the two classes.\n", 144 | " - Cons: can’t interpret predictions as probabilities (because AUC is determined by rankings), so can’t explain the uncertainty of the model, and it doesn't work for multi-class case.\n", 145 | "- logloss/deviance/cross entropy:\n", 146 | " - Pros: error metric based on probabilities\n", 147 | " - Cons: very sensitive to false positives, negatives\n", 148 | "- When there are more than 2 groups, we can have k binary classifications and add them up for logloss. Some metrics like AUC is only applicable in the binary case." 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "### 6. What are various ways to predict a binary response variable? Can you compare two of them and tell me when one would be more appropriate? What’s the difference between these? (SVM, Logistic Regression, Naive Bayes, Decision Tree, etc.)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "Solution\n", 163 | "\n", 164 | "- Things to look at: N, P, linearly separable, features independent, likely to overfit, speed, performance, memory usage and so on.\n", 165 | "- Logistic Regression\n", 166 | " - features roughly linear, problem roughly linearly separable\n", 167 | " - robust to noise, use l1,l2 regularization for model selection, avoid overfitting\n", 168 | " - the output come as probabilities\n", 169 | " - efficient and the computation can be distributed\n", 170 | " - can be used as a baseline for other algorithms\n", 171 | " - (-) can hardly handle categorical features\n", 172 | "- SVM\n", 173 | " - with a nonlinear kernel, can deal with problems that are not linearly separable\n", 174 | " - (-) slow to train, for most industry scale applications, not really efficient\n", 175 | "- Naive Bayes\n", 176 | " - computationally efficient when P is large by alleviating the curse of dimensionality\n", 177 | " - works surprisingly well for some cases even if the condition doesn’t hold\n", 178 | " - with word frequencies as features, the independence assumption can be seen reasonable. So the algorithm can be used in text categorization\n", 179 | " - (-) conditional independence of every other feature should be met\n", 180 | "- Tree Ensembles\n", 181 | " - good for large N and large P, can deal with categorical features very well\n", 182 | " - non parametric, so no need to worry about outliers\n", 183 | " - GBT’s work better but the parameters are harder to tune\n", 184 | " - RF works out of the box, but usually performs worse than GBT\n", 185 | "- Deep Learning\n", 186 | " - works well for some classification tasks (e.g. image)\n", 187 | " - used to squeeze something out of the problem" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "### 7. What is regularization and where might it be helpful? What is an example of using regularization in a model?" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "Solution\n", 202 | "\n", 203 | "- Regularization is useful for reducing variance in the model, meaning avoiding overfitting.\n", 204 | "- For example, we can use L1 regularization in Lasso regression to penalize large coefficients and automatically select features, or we can also use L2 regularization for Ridge regression to penalize the feature coefficients." 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "### 8. Why might it be preferable to include fewer predictors over many?" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "Solution\n", 219 | "\n", 220 | "- When we add irrelevant features, it increases model's tendency to overfit because those features introduce more noise. When two variables are correlated, they might be harder to interpret in case of regression, etc.\n", 221 | "- curse of dimensionality\n", 222 | "- adding random noise makes the model more complicated but useless\n", 223 | "- computational cost\n", 224 | "- Ask someone for more details." 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### 9. Given training data on tweets and their retweets, how would you predict the number of retweets of a given tweet after 7 days after only observing 2 days worth of data?" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Solution\n", 239 | "\n", 240 | "- Build a time series model with the training data with a seven day cycle and then use that for a new data with only 2 days data.\n", 241 | "- Ask someone for more details.\n", 242 | "- Build a regression function to estimate the number of retweets as a function of time t\n", 243 | "- to determine if one regression function can be built, see if there are clusters in terms of the trends in the number of retweets\n", 244 | "- if not, we have to add features to the regression function\n", 245 | "- features + # of retweets on the first and the second day ➞ predict the seventh day\n", 246 | "- https://en.wikipedia.org/wiki/Dynamic_time_warping" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "### 10. How could you collect and analyze data to use social media to predict the weather?" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "Solution\n", 261 | "\n", 262 | "- We can collect social media data using twitter, Facebook, instagram API’s.\n", 263 | "- Then, for example, for twitter, we can construct features from each tweet, e.g. the tweeted date, number of favorites, retweets, and of course, the features created from the tweeted content itself.\n", 264 | "- Then use a multivariate time series model to predict the weather.\n", 265 | "- Ask someone for more details." 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "### 11. How would you construct a feed to show relevant content for a site that involves user interactions with items?" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "Solution\n", 280 | "\n", 281 | "- We can do so using building a recommendation engine.\n", 282 | "- The easiest we can do is to show contents that are popular other users, which is still a valid strategy if for example the contents are news articles.\n", 283 | "- To be more accurate, we can build a content based filtering or collaborative filtering. If there’s enough user usage data, we can try collaborative filtering and recommend contents other similar users have consumed. If there isn’t, we can recommend similar items based on vectorization of items (content based filtering)." 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "### 12. How would you design the people you may know feature on LinkedIn or Facebook?" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "Solution\n", 298 | "\n", 299 | "- Find strong unconnected people in weighted connection graph\n", 300 | " - Define similarity as how strong the two people are connected\n", 301 | " - Given a certain feature, we can calculate the similarity based on\n", 302 | " - friend connections (neighbors)\n", 303 | " - Check-in’s people being at the same location all the time.\n", 304 | " - same college, workplace\n", 305 | " - Have randomly dropped graphs test the performance of the algorithm\n", 306 | "- Ref. News Feed Optimization\n", 307 | " - Affinity score: how close the content creator and the users are\n", 308 | " - Weight: weight for the edge type (comment, like, tag, etc.). Emphasis on features the company wants to promote\n", 309 | " - Time decay: the older the less important" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "### 13. How would you predict who someone may want to send a Snapchat or Gmail to?" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "Solution\n", 324 | "\n", 325 | "- for each user, assign a score of how likely someone would send an email to\n", 326 | "- the rest is feature engineering:\n", 327 | " - number of past emails, how many responses, the last time they exchanged an email, whether the last email ends with a question mark, features about the other users, etc.\n", 328 | "- Ask someone for more details.\n", 329 | "- People who someone sent emails the most in the past, conditioning on time decay." 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "### 14. How would you suggest to a franchise where to open a new store?" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "Solution\n", 344 | "\n", 345 | "- build a master dataset with local demographic information available for each location.\n", 346 | " - local income levels, proximity to traffic, weather, population density, proximity to other businesses\n", 347 | " - a reference dataset on local, regional, and national macroeconomic conditions (e.g. unemployment, inflation, prime interest rate, etc.)\n", 348 | " - any data on the local franchise owner-operators, to the degree the manager\n", 349 | "- identify a set of KPIs acceptable to the management that had requested the analysis concerning the most desirable factors surrounding a franchise\n", 350 | " - quarterly operating profit, ROI, EVA, pay-down rate, etc.\n", 351 | "- run econometric models to understand the relative significance of each variable\n", 352 | "- run machine learning algorithms to predict the performance of each location candidate" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "### 15. In a search engine, given partial data on what the user has typed, how would you predict the user’s eventual search query?" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [ 366 | "Solution\n", 367 | "\n", 368 | "- Based on the past frequencies of words shown up given a sequence of words, we can construct conditional probabilities of the set of next sequences of words that can show up (n-gram). The sequences with highest conditional probabilities can show up as top candidates.\n", 369 | "- To further improve this algorithm,\n", 370 | " - we can put more weight on past sequences which showed up more recently and near your location to account for trends\n", 371 | " - show your recent searches given partial data\n", 372 | "- Personalize and localize the search\n", 373 | " - Use the user's historical search data\n", 374 | " - Use the historical data from the local region" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "### 16. Given a database of all previous alumni donations to your university, how would you predict which recent alumni are most likely to donate?" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "Solution\n", 389 | "\n", 390 | "- Based on frequency and amount of donations, graduation year, major, etc, construct a supervised regression (or binary classification) algorithm." 391 | ] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "### 17. You’re Uber and you want to design a heatmap to recommend to drivers where to wait for a passenger. How would you approach this?" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": {}, 403 | "source": [ 404 | "Solution\n", 405 | "\n", 406 | "- Based on the past pickup location of passengers around the same time of the day, day of the week (month, year), construct\n", 407 | "- Ask someone for more details.\n", 408 | "- Based on the number of past pickups\n", 409 | " - account for periodicity (seasonal, monthly, weekly, daily, hourly)\n", 410 | " - special events (concerts, festivals, etc.) from tweets" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": {}, 416 | "source": [ 417 | "### 18. How would you build a model to predict a March Madness bracket?" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "Solution\n", 425 | "\n", 426 | "- One vector each for team A and B. Take the difference of the two vectors and use that as an input to predict the probability that team A would win by training the model. Train the models using past tournament data and make a prediction for the new tournament by running the trained model for each round of the tournament\n", 427 | "- Some extensions:\n", 428 | " - Experiment with different ways of consolidating the 2 team vectors into one (e.g concantenating, averaging, etc)\n", 429 | " - Consider using a RNN type model that looks at time series data." 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "### 19. You want to run a regression to predict the probability of a flight delay, but there are flights with delays of up to 12 hours that are really messing up your model. How can you address this?" 437 | ] 438 | }, 439 | { 440 | "cell_type": "markdown", 441 | "metadata": {}, 442 | "source": [ 443 | "Solution\n", 444 | "\n", 445 | "- This is equivalent to making the model more robust to outliers.\n", 446 | "- See **Question 3**." 447 | ] 448 | } 449 | ], 450 | "metadata": { 451 | "hide_input": false, 452 | "kernelspec": { 453 | "display_name": "Python 3", 454 | "language": "python", 455 | "name": "python3" 456 | }, 457 | "language_info": { 458 | "codemirror_mode": { 459 | "name": "ipython", 460 | "version": 3 461 | }, 462 | "file_extension": ".py", 463 | "mimetype": "text/x-python", 464 | "name": "python", 465 | "nbconvert_exporter": "python", 466 | "pygments_lexer": "ipython3", 467 | "version": "3.8.8" 468 | }, 469 | "toc": { 470 | "base_numbering": 1, 471 | "nav_menu": {}, 472 | "number_sections": true, 473 | "sideBar": true, 474 | "skip_h1_title": false, 475 | "title_cell": "Table of Contents", 476 | "title_sidebar": "Contents", 477 | "toc_cell": false, 478 | "toc_position": {}, 479 | "toc_section_display": true, 480 | "toc_window_display": false 481 | }, 482 | "varInspector": { 483 | "cols": { 484 | "lenName": 16, 485 | "lenType": 16, 486 | "lenVar": 40 487 | }, 488 | "kernels_config": { 489 | "python": { 490 | "delete_cmd_postfix": "", 491 | "delete_cmd_prefix": "del ", 492 | "library": "var_list.py", 493 | "varRefreshCmd": "print(var_dic_list())" 494 | }, 495 | "r": { 496 | "delete_cmd_postfix": ") ", 497 | "delete_cmd_prefix": "rm(", 498 | "library": "var_list.r", 499 | "varRefreshCmd": "cat(var_dic_list()) " 500 | } 501 | }, 502 | "types_to_exclude": [ 503 | "module", 504 | "function", 505 | "builtin_function_or_method", 506 | "instance", 507 | "_Feature" 508 | ], 509 | "window_display": false 510 | } 511 | }, 512 | "nbformat": 4, 513 | "nbformat_minor": 2 514 | } 515 | -------------------------------------------------------------------------------- /03_Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Programming ➞ 14 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. Write a function to calculate all possible assignment vectors of `2n` users, where `n` users are assigned to group 0 (control), and `n` users are assigned to group 1 (treatment)." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | " - Recursive programming (sol in code)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": { 44 | "ExecuteTime": { 45 | "end_time": "2021-09-21T16:10:40.786976Z", 46 | "start_time": "2021-09-21T16:10:40.771303Z" 47 | } 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 0, 1], [0, 0, 1, 1]]\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "def n_choose_k(n, k):\n", 60 | " \"\"\" function to choose k from n \"\"\"\n", 61 | " if k == 1:\n", 62 | " ans = []\n", 63 | " for i in range(n):\n", 64 | " tmp = [0] * n\n", 65 | " tmp[i] = 1\n", 66 | " ans.append(tmp)\n", 67 | " return ans\n", 68 | "\n", 69 | " if k == n:\n", 70 | " return [[1] * n]\n", 71 | "\n", 72 | " ans = []\n", 73 | " space = n - k + 1\n", 74 | " for i in range(space):\n", 75 | " assignment = [0] * (i + 1)\n", 76 | " assignment[i] = 1\n", 77 | " for c in n_choose_k(n - i - 1, k - 1):\n", 78 | " ans.append(assignment + c)\n", 79 | " return ans\n", 80 | "\n", 81 | "# test: choose 2 from 4\n", 82 | "print(n_choose_k(4, 2))" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "### 2. Given a list of tweets, determine the top 10 most used hashtags." 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "Solution\n", 97 | "\n", 98 | " - Store all the hashtags in a dictionary and use priority queue to solve the top-k problem\n", 99 | " - An extension will be top-k problem using Hadoop/MapReduce" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "### 3. Program an algorithm to find the best approximate solution to the knapsack problem in a given time." 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "Solution\n", 114 | "\n", 115 | " - **[https://en.wikipedia.org/wiki/Knapsack_problem](https://en.wikipedia.org/wiki/Knapsack_problem)**\n", 116 | " - Greedy solution (add the best v/w as much as possible and move on to the next)\n", 117 | " - Dynamic programming" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "### 4. Program an algorithm to find the best approximate solution to the traveling salesman problem in a given time." 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Solution\n", 132 | "\n", 133 | " - **[https://en.wikipedia.org/wiki/Travelling_salesman_problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem)**\n", 134 | " - Greedy\n", 135 | " - Dynamic programming" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "### 5. You have a stream of data coming in of size n, but you don’t know what n is ahead of time. Write an algorithm that will take a random sample of `k` elements. Can you write one that takes `O(k)` space?" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "Solution\n", 150 | "\n", 151 | " - **[Reservoir sampling](https://en.wikipedia.org/wiki/Reservoir_sampling)**" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "### 6. Write an algorithm that can calculate the square root of a number." 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "Solution\n", 166 | "\n", 167 | " - Binary search or Newton's method" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "### 7. Given a list of numbers, can you return the outliers?" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Solution\n", 182 | "\n", 183 | " - Sort then select the highest and the lowest 2.5%\n", 184 | " - Visualization can helps a lot" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "### 8. When can parallelism make your algorithms run faster? When could it make your algorithms run slower?" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "Solution\n", 199 | "\n", 200 | " - Ask someone for more details.\n", 201 | " - compute in parallel when communication cost < computation cost\n", 202 | " - ensemble trees\n", 203 | " - minibatch\n", 204 | " - cross validation\n", 205 | " - forward propagation\n", 206 | " - minibatch\n", 207 | " - not suitable for online learning" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "### 9. What are the different types of joins? What are the differences between them?" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "Solution\n", 222 | "\n", 223 | " - Inner Join, Left Join, Right Join, Outer Join, Self Join" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "### 10. Why might a join on a subquery be slow? How might you speed it up?" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "Solution\n", 238 | "\n", 239 | " - Change the subquery to a join.\n", 240 | " - **[Stack Overflow Answers](https://stackoverflow.com/questions/31724903/why-might-a-join-on-a-subquery-be-slow-what-could-be-done-to-make-it-faster-s)**" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "### 11. Describe the difference between primary keys and foreign keys in a SQL database." 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "Solution\n", 255 | "\n", 256 | " - Primary keys are columns whose value combinations must be unique in a specific table so that each row can be referenced uniquely.\n", 257 | " - Foreign keys are columns that references columns (often primary keys) in other tables." 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "### 12. Given a **COURSES** table with columns **course_id** and **course_name**, a **FACULTY** table with columns **faculty_id** and **faculty_name**, and a **COURSE_FACULTY** table with columns **faculty_id** and **course_id**, how would you return a list of faculty who teach a course given the name of a course?" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "Solution\n", 272 | "\n", 273 | "```SQL\n", 274 | "SELECT f.faculty_name\n", 275 | "FROM COURSES c\n", 276 | "JOIN COURSE_FACULTY cf\n", 277 | " ON c.course_id = cf.course_id\n", 278 | "JOIN FACULTY\n", 279 | " ON f.faculty_id = cf.faculty_id\n", 280 | "WHERE c.course_name = xxx;\n", 281 | "```" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "### 13. Given a **IMPRESSIONS** table with **ad_id**, **click** (an indicator that the ad was clicked), and **date**, write a SQL query that will tell me the click-through-rate of each ad by month." 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "Solution\n", 296 | "\n", 297 | "```SQL\n", 298 | "SELECT ad_id, MONTH(date), AVG(click)\n", 299 | "FROM IMPRESSIONS\n", 300 | "GROUP BY ad_id, MONTH(date);\n", 301 | "```" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "### 14. Write a query that returns the name of each department and a count of the number of employees in each: \n", 309 | "\n", 310 | "- **EMPLOYEES** containing: **Emp_ID** (Primary key) and **Emp_Name** \n", 311 | "- **EMPLOYEE_DEPT** containing: **Emp_ID** (Foreign key) and **Dept_ID** (Foreign key) \n", 312 | "- **DEPTS** containing: **Dept_ID** (Primary key) and **Dept_Name**" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "Solution\n", 320 | "\n", 321 | "```SQL\n", 322 | "SELECT d.Dept_Name, COUNT(*)\n", 323 | "FROM DEPTS d\n", 324 | "LEFT JOIN EMPLOYEE_DEPT ed\n", 325 | " ON d.Dept_ID = ed.Dept_ID\n", 326 | "GROUP BY d.Dept_Name;\n", 327 | "```" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [] 336 | } 337 | ], 338 | "metadata": { 339 | "hide_input": false, 340 | "kernelspec": { 341 | "display_name": "Python 3", 342 | "language": "python", 343 | "name": "python3" 344 | }, 345 | "language_info": { 346 | "codemirror_mode": { 347 | "name": "ipython", 348 | "version": 3 349 | }, 350 | "file_extension": ".py", 351 | "mimetype": "text/x-python", 352 | "name": "python", 353 | "nbconvert_exporter": "python", 354 | "pygments_lexer": "ipython3", 355 | "version": "3.8.8" 356 | }, 357 | "toc": { 358 | "base_numbering": 1, 359 | "nav_menu": {}, 360 | "number_sections": true, 361 | "sideBar": true, 362 | "skip_h1_title": false, 363 | "title_cell": "Table of Contents", 364 | "title_sidebar": "Contents", 365 | "toc_cell": false, 366 | "toc_position": {}, 367 | "toc_section_display": true, 368 | "toc_window_display": false 369 | }, 370 | "varInspector": { 371 | "cols": { 372 | "lenName": 16, 373 | "lenType": 16, 374 | "lenVar": 40 375 | }, 376 | "kernels_config": { 377 | "python": { 378 | "delete_cmd_postfix": "", 379 | "delete_cmd_prefix": "del ", 380 | "library": "var_list.py", 381 | "varRefreshCmd": "print(var_dic_list())" 382 | }, 383 | "r": { 384 | "delete_cmd_postfix": ") ", 385 | "delete_cmd_prefix": "rm(", 386 | "library": "var_list.r", 387 | "varRefreshCmd": "cat(var_dic_list()) " 388 | } 389 | }, 390 | "types_to_exclude": [ 391 | "module", 392 | "function", 393 | "builtin_function_or_method", 394 | "instance", 395 | "_Feature" 396 | ], 397 | "window_display": false 398 | } 399 | }, 400 | "nbformat": 4, 401 | "nbformat_minor": 2 402 | } 403 | -------------------------------------------------------------------------------- /04_Probability.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Probability ➞ 20 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. Bobo the amoeba has a 25%, 25%, and 50% chance of producing 0, 1, or 2 o spring, respectively. Each of Bobo’s descendants also have the same probabilities. What is the probability that Bobo’s lineage dies out?" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | " - $p=1/4+1/4*p+1/2*p^2$ \n", 38 | " - $p=1/2$" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "### 2. In any 15-minute interval, there is a 20% probability that you will see at least one shooting star. What is the probability that you see at least one shooting star in the period of an hour?" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "Solution\n", 53 | "\n", 54 | " - $1-(0.8)^4 = 0.5904$\n", 55 | " - Or, we can use Poisson processes" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "### 3. How can you generate a random number between 1 - 7 with only a die?" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Solution\n", 70 | "\n", 71 | " - **[Quora Answer](https://www.quora.com/How-can-you-generate-a-random-number-between-1-7-with-only-a-die-1)**" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### 4. How can you get a fair coin toss if someone hands you a coin that is weighted to come up heads more often than tails?" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Solution\n", 86 | "\n", 87 | " - Flip twice:\n", 88 | " - HT ➞ H\n", 89 | " - TH ➞ T\n", 90 | " - If HH or TT, repeat." 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### 5. You have an 50-50 mixture of two normal distributions with the same standard deviation. How far apart do the means need to be in order for this distribution to be bimodal?" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Solution\n", 105 | "\n", 106 | " - more than two standard deviations" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "### 6. Given draws from a normal distribution with known parameters, how can you simulate draws from a uniform distribution?" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Solution\n", 121 | "\n", 122 | " - Plug in the value to the CDF of the same random variable" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### 7. A certain couple tells you that they have two children, at least one of which is a girl. What is the probability that they have two girls?" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "Solution\n", 137 | "\n", 138 | " - gg, gb, bg ➞ 1/3" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "### 8. You have a group of couples that decide to have children until they have their first girl, after which they stop having children. What is the expected gender ratio of the children that are born? What is the expected number of children each couple will have?" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "Solution\n", 153 | "\n", 154 | " - Geometric distribution with $p = 0.5$\n", 155 | " - gender ratio is $1:1$. Expected number of children is 2.\n", 156 | " - let X be the number of children until getting a female (happens with prob 1/2). this follows a geometric distribution with probability 1/2" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "### 9. How many ways can you split 12 people into 3 teams of 4?" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "Solution\n", 171 | "\n", 172 | " - the outcome follows a multinomial distribution with $n=12$ and $k=3$. but the classes are indistinguishable\n", 173 | " - $(12, 8) * (8, 4) * (4, 4) / (3, 3)$\n", 174 | " - $12! / (4!)^3 / 3!$" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "### 10. Your hash function assigns each object to a number between 1:10, each with equal probability. With 10 objects, what is the probability of a hash collision? What is the expected number of hash collisions? What is the expected number of hashes that are unused." 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "Solution\n", 189 | "\n", 190 | " - the probability of a hash collision ➞ $1-(10!/10^{10})$\n", 191 | " - the expected number of hash collisions ➞ $10(1 - (1-1/10)^{10})$\n", 192 | " - **[Quora Reference](https://www.quora.com/Your-hash-function-assigns-each-object-to-a-number-between-1-10-each-with-equal-probability-With-10-objects-what-is-the-probability-of-a-hash-collision-What-is-the-expected-number-of-hash-collisions-What-is-the-expected-number-of-hashes-that-are-unused)**\n", 193 | " - the expected number of hashes that are unused ➞ $10*(9/10)^{10}$" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "### 11. You call 2 UberX’s and 3 Lyfts. If the time that each takes to reach you is IID, what is the probability that all the Lyfts arrive first? What is the probability that all the UberX’s arrive first?" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Solution\n", 208 | "\n", 209 | " - Lyfts arrive first ➞ $2! * 3! / 5!$\n", 210 | " - Ubers arrive first ➞ same" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "### 12. I write a program should print out all the numbers from 1 to 300, but prints out Fizz instead if the number is divisible by 3, Buzz instead if the number is divisible by 5, and FizzBuzz if the number is divisible by 3 and 5. What is the total number of numbers that is either Fizzed, Buzzed, or FizzBuzzed?" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "Solution\n", 225 | "\n", 226 | " - $100+60-20=140$" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "### 13. On a dating site, users can select 5 out of 24 adjectives to describe themselves. A match is declared between two users if they match on at least 4 adjectives. If Alice and Bob randomly pick adjectives, what is the probability that they form a match?" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "Solution\n", 241 | "\n", 242 | " - $= 24C5*(1+5(24-5))/24C5*24C5$ \n", 243 | " - $= 4/1771$" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "### 14. A lazy high school senior types up application and envelopes to `n` different colleges, but puts the applications randomly into the envelopes. What is the expected number of applications that went to the right college?" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "Solution\n", 258 | "\n", 259 | " - 1" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "### 15. Let’s say you have a very tall father. On average, what would you expect the height of his son to be? Taller, equal, or shorter? What if you had a very short father?" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": {}, 272 | "source": [ 273 | "Solution\n", 274 | "\n", 275 | " - Shorter. Regression to the mean" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### 16. What’s the expected number of coin flips until you get two heads in a row? What’s the expected number of coin flips until you get two tails in a row?" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "Solution\n", 290 | "\n", 291 | " - $x = 0.25 * 2 + 0.25 * (x + 2) + 0.5 * (x + 1)$ \n", 292 | " - $x = 6$\n", 293 | " - **[Quora Reference](https://www.quora.com/What-is-the-expected-number-of-coin-flips-until-you-get-two-heads-in-a-row)**" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "### 17. Let’s say we play a game where I keep flipping a coin until I get heads. If the first time I get heads is on the nth coin, then I pay you `2n-1` dollars. How much would you pay me to play this game?" 301 | ] 302 | }, 303 | { 304 | "cell_type": "markdown", 305 | "metadata": {}, 306 | "source": [ 307 | "Solution\n", 308 | "\n", 309 | " - less than $3\n", 310 | " - **[Quora reference](https://www.quora.com/I-will-flip-a-coin-until-I-get-my-first-heads-I-will-then-pay-you-2-n-1-where-n-is-the-total-number-of-coins-I-flipped-How-much-would-you-pay-me-to-play-this-game-You-can-only-play-once)**" 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": {}, 316 | "source": [ 317 | "### 18. You have two coins, one of which is fair and comes up heads with a probability 1/2, and the other which is biased and comes up heads with probability 3/4. You randomly pick coin and flip it twice, and get heads both times. What is the probability that you picked the fair coin?" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "Solution\n", 325 | "\n", 326 | " - 4/13\n", 327 | " - Bayesian method" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "### 19. You have a 0.1% chance of picking up a coin with both heads, and a 99.9% chance that you pick up a fair coin. You flip your coin and it comes up heads 10 times. What’s the chance that you picked up the fair coin, given the information that you observed?" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "Solution\n", 342 | "\n", 343 | " - Bayesian method" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "### 20. What is a P-Value ?" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": {}, 356 | "source": [ 357 | "Solution\n", 358 | "\n", 359 | "- https://en.wikipedia.org/wiki/P-value" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": null, 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [] 368 | } 369 | ], 370 | "metadata": { 371 | "hide_input": false, 372 | "kernelspec": { 373 | "display_name": "Python 3", 374 | "language": "python", 375 | "name": "python3" 376 | }, 377 | "language_info": { 378 | "codemirror_mode": { 379 | "name": "ipython", 380 | "version": 3 381 | }, 382 | "file_extension": ".py", 383 | "mimetype": "text/x-python", 384 | "name": "python", 385 | "nbconvert_exporter": "python", 386 | "pygments_lexer": "ipython3", 387 | "version": "3.8.8" 388 | }, 389 | "toc": { 390 | "base_numbering": 1, 391 | "nav_menu": {}, 392 | "number_sections": true, 393 | "sideBar": true, 394 | "skip_h1_title": false, 395 | "title_cell": "Table of Contents", 396 | "title_sidebar": "Contents", 397 | "toc_cell": false, 398 | "toc_position": {}, 399 | "toc_section_display": true, 400 | "toc_window_display": false 401 | }, 402 | "varInspector": { 403 | "cols": { 404 | "lenName": 16, 405 | "lenType": 16, 406 | "lenVar": 40 407 | }, 408 | "kernels_config": { 409 | "python": { 410 | "delete_cmd_postfix": "", 411 | "delete_cmd_prefix": "del ", 412 | "library": "var_list.py", 413 | "varRefreshCmd": "print(var_dic_list())" 414 | }, 415 | "r": { 416 | "delete_cmd_postfix": ") ", 417 | "delete_cmd_prefix": "rm(", 418 | "library": "var_list.r", 419 | "varRefreshCmd": "cat(var_dic_list()) " 420 | } 421 | }, 422 | "types_to_exclude": [ 423 | "module", 424 | "function", 425 | "builtin_function_or_method", 426 | "instance", 427 | "_Feature" 428 | ], 429 | "window_display": false 430 | } 431 | }, 432 | "nbformat": 4, 433 | "nbformat_minor": 2 434 | } 435 | -------------------------------------------------------------------------------- /05_Statistical_Inference.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Statistical Inference ➞ 15 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. In an A/B test, how can you check if assignment to the various buckets was truly random?" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | " - Plot the distributions of multiple features for both A and B and make sure that they have the same shape. More rigorously, we can conduct a permutation test to see if the distributions are the same.\n", 38 | " - MANOVA to compare different means" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "### 2. What might be the benefits of running an A/A test, where you have two buckets who are exposed to the exact same product?" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "Solution\n", 53 | "\n", 54 | " - Verify the sampling algorithm is random." 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "### 3. What would be the hazards of letting users sneak a peek at the other bucket in an A/B test?" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "Solution\n", 69 | "\n", 70 | " - The user might not act the same suppose had they not seen the other bucket. You are essentially adding additional variables of whether the user peeked the other bucket, which are not random across groups." 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "### 4. What would be some issues if blogs decide to cover one of your experimental groups?" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "Solution\n", 85 | "\n", 86 | " - Same as the previous question. The above problem can happen in larger scale." 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### 5. How would you conduct an A/B test on an opt-in feature? " 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Solution\n", 101 | "\n", 102 | " - Ask someone for more details." 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "### 6. How would you run an A/B test for many variants, say 20 or more?" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Solution\n", 117 | "\n", 118 | " - one control, 20 treatment, if the sample size for each group is big enough.\n", 119 | " - Ways to attempt to correct for this include changing your confidence level (e.g. Bonferroni Correction) or doing family-wide tests before you dive in to the individual metrics (e.g. Fisher's Protected LSD)." 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "### 7. How would you run an A/B test if the observations are extremely right-skewed?" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Solution\n", 134 | "\n", 135 | " - lower the variability by modifying the KPI\n", 136 | " - cap values\n", 137 | " - percentile metrics\n", 138 | " - log transform\n", 139 | " - " 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "### 8. I have two different experiments that both change the sign-up button to my website. I want to test them at the same time. What kinds of things should I keep in mind?" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "Solution\n", 154 | "\n", 155 | " - exclusive ➞ ok" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "### 9. What is a p-value? What is the difference between type-1 and type-2 error?" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "Solution\n", 170 | "\n", 171 | " - **[en.wikipedia.org/wiki/P-value](https://en.wikipedia.org/wiki/P-value)**\n", 172 | " - type-1 error: rejecting Ho when Ho is true\n", 173 | " - type-2 error: not rejecting Ho when Ha is true" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "### 10. You are AirBnB and you want to test the hypothesis that a greater number of photographs increases the chances that a buyer selects the listing. How would you test this hypothesis?" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Solution\n", 188 | "\n", 189 | " - For randomly selected listings with more than 1 pictures, hide 1 random picture for group A, and show all for group B. Compare the booking rate for the two groups.\n", 190 | " - Ask someone for more details." 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "### 11. How would you design an experiment to determine the impact of latency on user engagement?" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "Solution\n", 205 | "\n", 206 | " - The best way I know to quantify the impact of performance is to isolate just that factor using a slowdown experiment, i.e., add a delay in an A/B test." 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "### 12. 12. What is maximum likelihood estimation? Could there be any case where it doesn’t exist?" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "Solution\n", 221 | "\n", 222 | " - A method for parameter optimization (fitting a model). We choose parameters so as to maximize the likelihood function (how likely the outcome would happen given the current data and our model).\n", 223 | " - maximum likelihood estimation (MLE) is a method of **[estimating](https://en.wikipedia.org/wiki/Estimator \"Estimator\")** the **[parameters](https://en.wikipedia.org/wiki/Statistical_parameter \"Statistical parameter\")** of a **[statistical model](https://en.wikipedia.org/wiki/Statistical_model \"Statistical model\")** given observations, by finding the parameter values that maximize the **[likelihood](https://en.wikipedia.org/wiki/Likelihood \"Likelihood\")** of making the observations given the parameters. MLE can be seen as a special case of the **[maximum a posteriori estimation](https://en.wikipedia.org/wiki/Maximum_a_posteriori_estimation \"Maximum a posteriori estimation\")** (MAP) that assumes a **[uniform](https://en.wikipedia.org/wiki/Uniform_distribution_\\(continuous\\) \"Uniform distribution \\(continuous\\)\")** **[prior distribution](https://en.wikipedia.org/wiki/Prior_probability \"Prior probability\")** of the parameters, or as a variant of the MAP that ignores the prior and which therefore is **[unregularized](https://en.wikipedia.org/wiki/Regularization_\\(mathematics\\) \"Regularization \\(mathematics\\)\")**.\n", 224 | " - for gaussian mixtures, non parametric models, it doesn’t exist" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### 13. What’s the difference between a MAP, MOM, MLE estimator? In which cases would you want to use each?" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "Solution\n", 239 | "\n", 240 | " - MAP estimates the posterior distribution given the prior distribution and data which maximizes the likelihood function. MLE is a special case of MAP where the prior is uninformative uniform distribution.\n", 241 | " - MOM sets moment values and solves for the parameters. MOM is not used much anymore because maximum likelihood estimators have higher probability of being close to the quantities to be estimated and are more often unbiased." 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "### 14. What is a confidence interval and how do you interpret it?" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "Solution\n", 256 | "\n", 257 | " - For example, 95% confidence interval is an interval that when constructed for a set of samples each sampled in the same way, the constructed intervals include the true mean 95% of the time.\n", 258 | " - if confidence intervals are constructed using a given confidence level in an infinite number of independent experiments, the proportion of those intervals that contain the true value of the parameter will match the confidence level." 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "### 15. What is unbiasedness as a property of an estimator? Is this always a desirable property when performing inference? What about in data analysis or predictive modeling?" 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "Solution\n", 273 | "\n", 274 | " - Unbiasedness means that the expectation of the estimator is equal to the population value we are estimating. This is desirable in inference because the goal is to explain the dataset as accurately as possible. However, this is not always desirable for data analysis or predictive modeling as there is the bias variance tradeoff. We sometimes want to prioritize the generalizability and avoid overfitting by reducing variance and thus increasing bias." 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [] 283 | } 284 | ], 285 | "metadata": { 286 | "hide_input": false, 287 | "kernelspec": { 288 | "display_name": "Python 3", 289 | "language": "python", 290 | "name": "python3" 291 | }, 292 | "language_info": { 293 | "codemirror_mode": { 294 | "name": "ipython", 295 | "version": 3 296 | }, 297 | "file_extension": ".py", 298 | "mimetype": "text/x-python", 299 | "name": "python", 300 | "nbconvert_exporter": "python", 301 | "pygments_lexer": "ipython3", 302 | "version": "3.8.8" 303 | }, 304 | "toc": { 305 | "base_numbering": 1, 306 | "nav_menu": {}, 307 | "number_sections": true, 308 | "sideBar": true, 309 | "skip_h1_title": false, 310 | "title_cell": "Table of Contents", 311 | "title_sidebar": "Contents", 312 | "toc_cell": false, 313 | "toc_position": {}, 314 | "toc_section_display": true, 315 | "toc_window_display": false 316 | }, 317 | "varInspector": { 318 | "cols": { 319 | "lenName": 16, 320 | "lenType": 16, 321 | "lenVar": 40 322 | }, 323 | "kernels_config": { 324 | "python": { 325 | "delete_cmd_postfix": "", 326 | "delete_cmd_prefix": "del ", 327 | "library": "var_list.py", 328 | "varRefreshCmd": "print(var_dic_list())" 329 | }, 330 | "r": { 331 | "delete_cmd_postfix": ") ", 332 | "delete_cmd_prefix": "rm(", 333 | "library": "var_list.r", 334 | "varRefreshCmd": "cat(var_dic_list()) " 335 | } 336 | }, 337 | "types_to_exclude": [ 338 | "module", 339 | "function", 340 | "builtin_function_or_method", 341 | "instance", 342 | "_Feature" 343 | ], 344 | "window_display": false 345 | } 346 | }, 347 | "nbformat": 4, 348 | "nbformat_minor": 2 349 | } 350 | -------------------------------------------------------------------------------- /06_Data_Analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Data Analysis ➞ 27 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. (Given a Dataset) Analyze this dataset and tell me what you can learn from it." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | "- Typical data cleaning and visualization." 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "### 2. What is `R2`? What are some other metrics that could be better than `R2` and why?" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Solution\n", 52 | "\n", 53 | "- goodness of fit measure. variance explained by the regression / total variance.\n", 54 | " \n", 55 | " - the more predictors you add, the higher $R^2$ becomes.\n", 56 | " - hence use adjusted $R^2$ which adjusts for the degrees of freedom. \n", 57 | " - or train error metrics." 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### 3. What is the curse of dimensionality?" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "Solution\n", 72 | "\n", 73 | "- High dimensionality makes clustering hard, because having lots of dimensions means that everything is **\"far away\"** from each other.\n", 74 | " - For example, to cover a fraction of the volume of the data we need to capture a very wide range for each variable as the number of variables increases.\n", 75 | " - All samples are close to the edge of the sample. And this is a bad news because prediction is much more difficult near the edges of the training sample.\n", 76 | " - The sampling density decreases exponentially as p increases and hence the data becomes much more sparse without significantly more data. \n", 77 | " - We should conduct PCA to reduce dimensionality." 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "### 4. Is more data always better?" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Solution\n", 92 | "\n", 93 | "- **Statistically**\n", 94 | " - It depends on the quality of your data, for example, if your data is biased, just getting more data won’t help.\n", 95 | " - It depends on your model. If your model suffers from high bias, getting more data won’t improve your test results beyond a point. You’d need to add more features, etc.\n", 96 | " \n", 97 | " - **Practically**\n", 98 | " - More data usually benefit the models.\n", 99 | " - Also there’s a tradeoff between having more data and the additional storage, computational power, memory it requires. Hence, always think about the cost of having more data." 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "### 5. What are advantages of plotting your data before performing analysis?" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "Solution\n", 114 | "\n", 115 | "- Data sets have errors. You won't find them all but you might find some. That 212 year old man. That 9 foot tall woman. \n", 116 | " - Variables can have skewness, outliers, etc. Then the arithmetic mean might not be useful, which means the standard deviation isn't useful.\n", 117 | " - Variables can be multimodal! If a variable is multimodal then anything based on its mean or median is going to be suspect." 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "### 6. How can you make sure that you don’t analyze something that ends up meaningless?" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Solution\n", 132 | "\n", 133 | "- Proper exploratory data analysis. \n", 134 | " - In every data analysis task, there's the exploratory phase where you're just graphing things, testing things on small sets of the data, summarizing simple statistics, and getting rough ideas of what hypotheses you might want to pursue further. \n", 135 | " - Then there's the exploratory phase, where you look deeply into a set of hypotheses.  \n", 136 | " - The exploratory phase will generate lots of possible hypotheses, and the exploratory phase will let you really understand a few of them. Balance the two and you'll prevent yourself from wasting time on many things that end up meaningless, although not all." 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "### 7. What is the role of trial and error in data analysis? What is the the role of making a hypothesis before diving in?" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "Solution\n", 151 | "\n", 152 | "- data analysis is a repetition of setting up a new hypothesis and trying to refute the null hypothesis.\n", 153 | " - The scientific method is eminently inductive: we elaborate a hypothesis, test it and refute it or not. As a result, we come up with new hypotheses which are in turn tested and so on. This is an iterative process, as science always is." 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "### 8. How can you determine which features are the most important in your model?" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "Solution\n", 168 | "\n", 169 | "- Linear regression can use p-value\n", 170 | " - run the features though a Gradient Boosting Machine or Random Forest to generate plots of relative importance and information gain for each feature in the ensembles.\n", 171 | " - Look at the variables added in forward variable selection. " 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### 9. How do you deal with some of your predictors being missing?" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "Solution\n", 186 | "\n", 187 | "- Remove rows with missing values - This works well if\n", 188 | " - the values are missing randomly (see [Vinay Prabhu's answer](https://www.quora.com/How-can-I-deal-with-missing-values-in-a-predictive-model/answer/Vinay-Prabhu-7) for more details on this)\n", 189 | " - if you don't lose too much of the dataset after doing so.\n", 190 | " - Build another predictive model to predict the missing values.\n", 191 | " - This could be a whole project in itself, so simple techniques are usually used here.\n", 192 | " - Use a model that can incorporate missing data. \n", 193 | " - Like a random forest, or any tree-based method." 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "### 10. You have several variables that are positively correlated with your response, and you think combining all of the variables could give you a good prediction of your response. However, you see that in the multiple linear regression, one of the weights on the predictors is negative. What could be the issue?" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Solution\n", 208 | "\n", 209 | " - Multicollinearity refers to a situation in which two or more explanatory variables in a [multiple regression](https://en.wikipedia.org/wiki/Multiple_regression \"Multiple regression\") model are highly linearly related. \n", 210 | " - Leave the model as is, despite multicollinearity. The presence of multicollinearity doesn't affect the efficiency of extrapolating the fitted model to new data provided that the predictor variables follow the same pattern of multicollinearity in the new data as in the data on which the regression model is based.\n", 211 | " - principal component regression" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "### 11. Let’s say you’re given an unfeasible amount of predictors in a predictive modeling task. What are some ways to make the prediction more feasible?" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "Solution\n", 226 | "\n", 227 | " - PCA" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "### 12. Now you have a feasible amount of predictors, but you’re fairly sure that you don’t need all of them. How would you perform feature selection on the dataset?" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "Solution\n", 242 | "\n", 243 | " - ridge / lasso / elastic net regression.\n", 244 | " - Univariate Feature Selection where a statistical test is applied to each feature individually. You retain only the best features according to the test outcome scores.\n", 245 | " - Recursive Feature Elimination: \n", 246 | " - First, train a model with all the feature and evaluate its performance on held out data.\n", 247 | " - Then drop let say the 10% weakest features (e.g. the feature with least absolute coefficients in a linear model) and retrain on the remaining features.\n", 248 | " - Iterate until you observe a sharp drop in the predictive accuracy of the model." 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "### 13. Your linear regression didn’t run and communicates that there are an infinite number of best estimates for the regression coefficients. What could be wrong?" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "Solution\n", 263 | "\n", 264 | " - p > n.\n", 265 | " - If some of the explanatory variables are perfectly correlated (positively or negatively) then the coefficients would not be unique. " 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": {}, 271 | "source": [ 272 | "### 14. You run your regression on different subsets of your data, and find that in each subset, the beta value for a certain variable varies wildly. What could be the issue here?" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "Solution\n", 280 | "\n", 281 | " - The dataset might be heterogeneous. In which case, it is recommended to cluster datasets into different subsets wisely, and then draw different models for different subsets. Or, use models like non parametric models (trees) which can deal with heterogeneity quite nicely." 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "### 15. What is the main idea behind ensemble learning? If I had many different models that predicted the same response variable, what might I want to do to incorporate all of the models? Would you expect this to perform better than an individual model or worse?" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "Solution\n", 296 | "\n", 297 | " - The assumption is that a group of weak learners can be combined to form a strong learner.\n", 298 | " - Hence the combined model is expected to perform better than an individual model.\n", 299 | " - Assumptions:\n", 300 | " - average out biases\n", 301 | " - reduce variance\n", 302 | " - Bagging works because some underlying learning algorithms are unstable: slightly different inputs leads to very different outputs. If you can take advantage of this instability by running multiple instances, it can be shown that the reduced instability leads to lower error. If you want to understand why, the original bagging paper( [http://www.springerlink.com/](http://www.springerlink.com/content/l4780124w2874025/)) has a section called \"why bagging works\"\n", 303 | " - Boosting works because of the focus on better defining the \"decision edge\". By re-weighting examples near the margin (the positive and negative examples) you get a reduced error (see http://citeseerx.ist.psu.edu/vie...)\n", 304 | " - Use the outputs of your models as inputs to a meta-model.  \n", 305 | "\n", 306 | "**For example:** if you're doing binary classification, you can use all the probability outputs of your individual models as inputs to a final logistic regression (or any model, really) that can combine the probability estimates. \n", 307 | "\n", 308 | "One very important point is to make sure that the output of your models are out-of-sample predictions. This means that the predicted value for any row in your data-frame should NOT depend on the actual value for that row." 309 | ] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": {}, 314 | "source": [ 315 | "### 16. Given that you have wi-fi data in your office, how would you determine which rooms and areas are underutilized and over-utilized?" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "Solution\n", 323 | "\n", 324 | " - If the data is more used in one room, then that one is over utilized!\n", 325 | " - Maybe account for the room capacity and normalize the data." 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "### 17. How could you use GPS data from a car to determine the quality of a driver?" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "Solution\n", 340 | "\n", 341 | " - Speed\n", 342 | " - Driving paths" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "### 18. Given accelerometer, altitude, and fuel usage data from a car, how would you determine the optimum acceleration pattern to drive over hills?" 350 | ] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "Solution\n", 357 | "\n", 358 | " - Historical data?" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "### 19. Given position data of NBA players in a season’s games, how would you evaluate a basketball player’s defensive ability?" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "Solution\n", 373 | "\n", 374 | " - Evaluate his positions in the court." 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "### 20. How would you quantify the influence of a Twitter user?" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "Solution\n", 389 | "\n", 390 | " - like page rank with each user corresponding to the webpages and linking to the page equivalent to following." 391 | ] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "### 21. Given location data of golf balls in games, how would construct a model that can advise golfers where to aim?" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": {}, 403 | "source": [ 404 | "Solution\n", 405 | "\n", 406 | " - winning probability for different positions." 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "### 22. You have 100 mathletes and 100 math problems. Each mathlete gets to choose 10 problems to solve. Given data on who got what problem correct, how would you rank the problems in terms of difficulty?" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "Solution\n", 421 | "\n", 422 | " - One way you could do this is by storing a \"skill level\" for each user and a \"difficulty level\" for each problem.  We assume that the probability that a user solves a problem only depends on the skill of the user and the difficulty of the problem.*  Then we maximize the likelihood of the data to find the hidden skill and difficulty levels.\n", 423 | " - The Rasch model for dichotomous data takes the form: \n", 424 | " \n", 425 | "$ {\\displaystyle \\Pr {X_{ni}=1\\\\} = {\\frac {\\exp({\\beta_{n}}-{\\delta_{i}})}{1+\\exp({\\beta_{n}}-{\\delta_{i}})}},} $\n", 426 | "\n", 427 | "where  is the ability of person  and  is the difficulty of item." 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "### 23. You have 5000 people that rank 10 sushis in terms of saltiness. How would you aggregate this data to estimate the true saltiness rank in each sushi?" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "Solution\n", 442 | "\n", 443 | " - Some people would take the mean rank of each sushi.  If I wanted something simple, I would use the median, since ranks are (strictly speaking) ordinal and not interval, so adding them is a bit risque (but people do it all the time and you probably won't be far wrong)." 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "### 24. Given data on congressional bills and which congressional representatives co-sponsored the bills, how would you determine which other representatives are most similar to yours in voting behavior? How would you evaluate who is the most liberal? Most republican? Most bipartisan?" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "Solution\n", 458 | "\n", 459 | " - collaborative filtering. you have your votes and we can calculate the similarity for each representatives and select the most similar representative.\n", 460 | " - for liberal and republican parties, find the mean vector and find the representative closest to the center point." 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "metadata": {}, 466 | "source": [ 467 | "### 25. How would you come up with an algorithm to detect plagiarism in online content?" 468 | ] 469 | }, 470 | { 471 | "cell_type": "markdown", 472 | "metadata": {}, 473 | "source": [ 474 | "Solution\n", 475 | "\n", 476 | " - reduce the text to a more compact form (e.g. fingerprinting, bag of words) then compare those with other texts by calculating the similarity." 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "### 26. You have data on all purchases of customers at a grocery store. Describe to me how you would program an algorithm that would cluster the customers into groups. How would you determine the appropriate number of clusters to include?" 484 | ] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "Solution\n", 491 | "\n", 492 | " - K-means\n", 493 | " - choose a small value of k that still has a low SSE (elbow method)\n", 494 | " - [Elbow method](https://bl.ocks.org/rpgove/0060ff3b656618e9136b)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "### 27. Let’s say you’re building the recommended music engine at Spotify to recommend people music based on past listening history. How would you approach this problem?" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "Solution\n", 509 | "\n", 510 | " - content-based filtering\n", 511 | " - collaborative filtering" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "Solution\n", 524 | "\n" 525 | ] 526 | } 527 | ], 528 | "metadata": { 529 | "hide_input": false, 530 | "kernelspec": { 531 | "display_name": "Python 3", 532 | "language": "python", 533 | "name": "python3" 534 | }, 535 | "language_info": { 536 | "codemirror_mode": { 537 | "name": "ipython", 538 | "version": 3 539 | }, 540 | "file_extension": ".py", 541 | "mimetype": "text/x-python", 542 | "name": "python", 543 | "nbconvert_exporter": "python", 544 | "pygments_lexer": "ipython3", 545 | "version": "3.8.8" 546 | }, 547 | "toc": { 548 | "base_numbering": 1, 549 | "nav_menu": {}, 550 | "number_sections": true, 551 | "sideBar": true, 552 | "skip_h1_title": false, 553 | "title_cell": "Table of Contents", 554 | "title_sidebar": "Contents", 555 | "toc_cell": false, 556 | "toc_position": {}, 557 | "toc_section_display": true, 558 | "toc_window_display": false 559 | }, 560 | "varInspector": { 561 | "cols": { 562 | "lenName": 16, 563 | "lenType": 16, 564 | "lenVar": 40 565 | }, 566 | "kernels_config": { 567 | "python": { 568 | "delete_cmd_postfix": "", 569 | "delete_cmd_prefix": "del ", 570 | "library": "var_list.py", 571 | "varRefreshCmd": "print(var_dic_list())" 572 | }, 573 | "r": { 574 | "delete_cmd_postfix": ") ", 575 | "delete_cmd_prefix": "rm(", 576 | "library": "var_list.r", 577 | "varRefreshCmd": "cat(var_dic_list()) " 578 | } 579 | }, 580 | "types_to_exclude": [ 581 | "module", 582 | "function", 583 | "builtin_function_or_method", 584 | "instance", 585 | "_Feature" 586 | ], 587 | "window_display": false 588 | } 589 | }, 590 | "nbformat": 4, 591 | "nbformat_minor": 2 592 | } 593 | -------------------------------------------------------------------------------- /07_Product_Metrics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Product Metrics ➞ 15 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. What would be good metrics of success for an advertising-driven consumer product? (Buzzfeed, YouTube, Google Search, etc.) A service-driven consumer product? (Uber, Flickr, Venmo, etc.)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | " * advertising-driven: Page-views and daily actives, CTR, CPC (cost per click)\n", 38 | " * click-ads \n", 39 | " * display-ads \n", 40 | " * service-driven: number of purchases, conversion rate" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "### 2. What would be good metrics of success for a productivity tool? (Evernote, Asana, Google Docs, etc.) A MOOC? (edX, Coursera, Udacity, etc.)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Solution\n", 55 | "\n", 56 | " * Productivity tool: same as premium subscriptions\n", 57 | " * MOOC: same as premium subscriptions, completion rate" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### 3. What would be good metrics of success for an e-commerce product? (Etsy, Groupon, Birchbox, etc.) A subscription product? (Net ix, Birchbox, Hulu, etc.) Premium subscriptions? (OKCupid, LinkedIn, Spotify, etc.) " 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "Solution\n", 72 | "\n", 73 | " * e-commerce: number of purchases, conversion rate, Hourly, daily, weekly, monthly, quarterly, and annual sales, Cost of goods sold, Inventory levels, Site traffic, Unique visitors versus returning visitors, Customer service phone call count, Average resolution time\n", 74 | " * subscription\n", 75 | " * churn, CoCA, ARPU, MRR, LTV\n", 76 | " * premium subscriptions: \n", 77 | " * subscription rate" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "### 4. What would be good metrics of success for a consumer product that relies heavily on engagement and interaction? (Snapchat, Pinterest, Facebook, etc.) A messaging product? (GroupMe, Hangouts, Snapchat, etc.)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Solution\n", 92 | "\n", 93 | " * heavily on engagement and interaction: uses AU ratios, email summary by type, and push notification summary by type, resurrection ratio\n", 94 | " * messaging product: \n", 95 | " * daily, monthly active users" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "### 5. What would be good metrics of success for a product that offered in-app purchases? (Zynga, Angry Birds, other gaming apps)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Solution\n", 110 | "\n", 111 | " * Average Revenue Per Paid User\n", 112 | " * Average Revenue Per User" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "### 6. A certain metric is violating your expectations by going down or up more than you expect. How would you try to identify the cause of the change?" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "Solution\n", 127 | "\n", 128 | " * breakdown the KPI’s into what consists them and find where the change is\n", 129 | " * then further breakdown that basic KPI by channel, user cluster, etc. and relate them with any campaigns, changes in user behaviors in that segment" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "### 7. Growth for total number of tweets sent has been slow this month. What data would you look at to determine the cause of the problem?" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Solution\n", 144 | "\n", 145 | " * Historical data, especially historical data at the same month\n", 146 | " * Outer data, such as economic data, political data, data about competitors" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "### 8. You’re a restaurant and are approached by Groupon to run a deal. What data would you ask from them in order to determine whether or not to do the deal?" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Solution\n", 161 | "\n", 162 | " * for similar restaurants (they should define similarity), average increase in revenue gain per coupon, average increase in customers per coupon" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "### 9. You are tasked with improving the efficiency of a subway system. Where would you start?" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Solution\n", 177 | "\n", 178 | " * define efficiency" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "### 10. Say you are working on Facebook News Feed. What would be some metrics that you think are important? How would you make the news each person gets more relevant?" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Solution\n", 193 | "\n", 194 | " * rate for each action, duration users stay, CTR for sponsor feed posts\n", 195 | " * ref. News Feed Optimization\n", 196 | " * Affinity score: how close the content creator and the users are\n", 197 | " * Weight: weight for the edge type (comment, like, tag, etc.). Emphasis on features the company wants to promote\n", 198 | " * Time decay: the older the less important" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "### 11. How would you measure the impact that sponsored stories on Facebook News Feed have on user engagement? How would you determine the optimum balance between sponsored stories and organic content on a user’s News Feed?" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "Solution\n", 213 | "\n", 214 | " * AB test on different balance ratio and see " 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "### 12. You are on the data science team at Uber and you are asked to start thinking about surge pricing. What would be the objectives of such a product and how would you start looking into this?" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "Solution\n", 229 | "\n", 230 | " *  there is a gradual step-function type scaling mechanism until that imbalance of requests-to-drivers is alleviated and then vice versa as too many drivers come online enticed by the surge pricing structure. \n", 231 | " * I would bet the algorithm is custom tailored and calibrated to each location as price elasticities almost certainly vary across different cities depending on a huge multitude of variables: income, distance/sprawl, traffic patterns, car ownership, etc. With the massive troves of user data that Uber probably has collected, they most likely have tweaked the algorithms for each city to adjust for these varying sensitivities to surge pricing. Throw in some machine learning and incredibly rich data and you've got yourself an incredible, constantly-evolving algorithm. " 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "### 13. Say that you are Netflix. How would you determine what original series you should invest in and create?" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "Solution\n", 246 | "\n", 247 | " * Netflix uses data to estimate the potential market size for an original series before giving it the go-ahead." 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### 14. What kind of services would find churn (metric that tracks how many customers leave the service) helpful? How would you calculate churn?" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "Solution\n", 262 | "\n", 263 | " * subscription based services" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "### 15. Let’s say that you’re are scheduling content for a content provider on television. How would you determine the best times to schedule content?" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "Solution\n", 278 | "\n", 279 | " * Based on similar product and the corresponding broadcast popularity" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [] 288 | } 289 | ], 290 | "metadata": { 291 | "hide_input": false, 292 | "kernelspec": { 293 | "display_name": "Python 3", 294 | "language": "python", 295 | "name": "python3" 296 | }, 297 | "language_info": { 298 | "codemirror_mode": { 299 | "name": "ipython", 300 | "version": 3 301 | }, 302 | "file_extension": ".py", 303 | "mimetype": "text/x-python", 304 | "name": "python", 305 | "nbconvert_exporter": "python", 306 | "pygments_lexer": "ipython3", 307 | "version": "3.8.8" 308 | }, 309 | "toc": { 310 | "base_numbering": 1, 311 | "nav_menu": {}, 312 | "number_sections": true, 313 | "sideBar": true, 314 | "skip_h1_title": false, 315 | "title_cell": "Table of Contents", 316 | "title_sidebar": "Contents", 317 | "toc_cell": false, 318 | "toc_position": {}, 319 | "toc_section_display": true, 320 | "toc_window_display": false 321 | }, 322 | "varInspector": { 323 | "cols": { 324 | "lenName": 16, 325 | "lenType": 16, 326 | "lenVar": 40 327 | }, 328 | "kernels_config": { 329 | "python": { 330 | "delete_cmd_postfix": "", 331 | "delete_cmd_prefix": "del ", 332 | "library": "var_list.py", 333 | "varRefreshCmd": "print(var_dic_list())" 334 | }, 335 | "r": { 336 | "delete_cmd_postfix": ") ", 337 | "delete_cmd_prefix": "rm(", 338 | "library": "var_list.r", 339 | "varRefreshCmd": "cat(var_dic_list()) " 340 | } 341 | }, 342 | "types_to_exclude": [ 343 | "module", 344 | "function", 345 | "builtin_function_or_method", 346 | "instance", 347 | "_Feature" 348 | ], 349 | "window_display": false 350 | } 351 | }, 352 | "nbformat": 4, 353 | "nbformat_minor": 2 354 | } 355 | -------------------------------------------------------------------------------- /08_Communication.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Communication ➞ 5 Questions" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### 1. Explain to me a technical concept related to the role that you’re interviewing for." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2021-09-21T13:31:28.708336Z", 31 | "start_time": "2021-09-21T13:31:28.699521Z" 32 | } 33 | }, 34 | "source": [ 35 | "Solution\n", 36 | "\n", 37 | "- AB test, PCA, data science, machine learning, neural networks" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "### 2. Introduce me to something you’re passionate about." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Solution\n", 52 | "\n", 53 | "- Data science" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### 3. How would you explain an A/B test to an engineer with no statistics background? A linear regression?" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "Solution\n", 68 | "\n", 69 | "- A/B testing, or more broadly, multivariate testing, is the testing of different elements of a user's experience to determine which variation helps the business achieve its goal more effectively (i.e. increasing conversions, etc..)  This can be copy on a web site, button colors, different user interfaces, different email subject lines, calls to action, offers, etc. " 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "### 4. How would you explain a confidence interval to an engineer with no statistics background? What does 95% confidence mean?" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "Solution\n", 84 | "\n", 85 | "- [link](https://www.quora.com/What-is-a-confidence-interval-in-laymans-terms)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "### 5. How would you explain to a group of senior executives why data is important?" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "Solution\n", 100 | "\n", 101 | "- Examples" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [] 110 | } 111 | ], 112 | "metadata": { 113 | "hide_input": false, 114 | "kernelspec": { 115 | "display_name": "Python 3", 116 | "language": "python", 117 | "name": "python3" 118 | }, 119 | "language_info": { 120 | "codemirror_mode": { 121 | "name": "ipython", 122 | "version": 3 123 | }, 124 | "file_extension": ".py", 125 | "mimetype": "text/x-python", 126 | "name": "python", 127 | "nbconvert_exporter": "python", 128 | "pygments_lexer": "ipython3", 129 | "version": "3.8.8" 130 | }, 131 | "toc": { 132 | "base_numbering": 1, 133 | "nav_menu": {}, 134 | "number_sections": true, 135 | "sideBar": true, 136 | "skip_h1_title": false, 137 | "title_cell": "Table of Contents", 138 | "title_sidebar": "Contents", 139 | "toc_cell": false, 140 | "toc_position": {}, 141 | "toc_section_display": true, 142 | "toc_window_display": false 143 | }, 144 | "varInspector": { 145 | "cols": { 146 | "lenName": 16, 147 | "lenType": 16, 148 | "lenVar": 40 149 | }, 150 | "kernels_config": { 151 | "python": { 152 | "delete_cmd_postfix": "", 153 | "delete_cmd_prefix": "del ", 154 | "library": "var_list.py", 155 | "varRefreshCmd": "print(var_dic_list())" 156 | }, 157 | "r": { 158 | "delete_cmd_postfix": ") ", 159 | "delete_cmd_prefix": "rm(", 160 | "library": "var_list.r", 161 | "varRefreshCmd": "cat(var_dic_list()) " 162 | } 163 | }, 164 | "types_to_exclude": [ 165 | "module", 166 | "function", 167 | "builtin_function_or_method", 168 | "instance", 169 | "_Feature" 170 | ], 171 | "window_display": false 172 | } 173 | }, 174 | "nbformat": 4, 175 | "nbformat_minor": 2 176 | } 177 | -------------------------------------------------------------------------------- /09_Coding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "All the IPython Notebooks in **Data Science Interview Questions** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/DataScience_Interview_Questions)**\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### 1. Write a function to calculate all possible assignment vectors of `2n` users, where `n` users are assigned to group 0 (control), and `n` users are assigned to group 1 (treatment).\n", 17 | "\n", 18 | "Solution" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 0, 1], [0, 0, 1, 1]]\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "def n_choose_k(n, k):\n", 36 | " \"\"\" function to choose k from n \"\"\"\n", 37 | " if k == 1:\n", 38 | " ans = []\n", 39 | " for i in range(n):\n", 40 | " tmp = [0] * n\n", 41 | " tmp[i] = 1\n", 42 | " ans.append(tmp)\n", 43 | " return ans\n", 44 | " \n", 45 | " if k == n:\n", 46 | " return [[1] * n]\n", 47 | " \n", 48 | " ans = []\n", 49 | " space = n - k + 1\n", 50 | " for i in range(space):\n", 51 | " assignment = [0] * (i + 1)\n", 52 | " assignment[i] = 1\n", 53 | " for c in n_choose_k(n - i - 1, k - 1):\n", 54 | " ans.append(assignment + c)\n", 55 | " return ans\n", 56 | "\n", 57 | "# test: choose 2 from 4\n", 58 | "print(n_choose_k(4, 2))" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [] 67 | } 68 | ], 69 | "metadata": { 70 | "hide_input": false, 71 | "kernelspec": { 72 | "display_name": "Python 3", 73 | "language": "python", 74 | "name": "python3" 75 | }, 76 | "language_info": { 77 | "codemirror_mode": { 78 | "name": "ipython", 79 | "version": 3 80 | }, 81 | "file_extension": ".py", 82 | "mimetype": "text/x-python", 83 | "name": "python", 84 | "nbconvert_exporter": "python", 85 | "pygments_lexer": "ipython3", 86 | "version": "3.8.8" 87 | }, 88 | "toc": { 89 | "base_numbering": 1, 90 | "nav_menu": {}, 91 | "number_sections": true, 92 | "sideBar": true, 93 | "skip_h1_title": false, 94 | "title_cell": "Table of Contents", 95 | "title_sidebar": "Contents", 96 | "toc_cell": false, 97 | "toc_position": {}, 98 | "toc_section_display": true, 99 | "toc_window_display": false 100 | }, 101 | "varInspector": { 102 | "cols": { 103 | "lenName": 16, 104 | "lenType": 16, 105 | "lenVar": 40 106 | }, 107 | "kernels_config": { 108 | "python": { 109 | "delete_cmd_postfix": "", 110 | "delete_cmd_prefix": "del ", 111 | "library": "var_list.py", 112 | "varRefreshCmd": "print(var_dic_list())" 113 | }, 114 | "r": { 115 | "delete_cmd_postfix": ") ", 116 | "delete_cmd_prefix": "rm(", 117 | "library": "var_list.r", 118 | "varRefreshCmd": "cat(var_dic_list()) " 119 | } 120 | }, 121 | "types_to_exclude": [ 122 | "module", 123 | "function", 124 | "builtin_function_or_method", 125 | "instance", 126 | "_Feature" 127 | ], 128 | "window_display": false 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 2 133 | } 134 | -------------------------------------------------------------------------------- /DataScience_Interview_Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/DataScience_Interview_Questions/b515c84b6b42243f45b3621ffe552abbe9219bc7/DataScience_Interview_Questions.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Milaan Parmar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Last Commit 3 | 4 | 5 | 6 | 7 | Stars Badge 8 | Forks Badge 9 | Size 10 | Pull Requests Badge 11 | Issues Badge 12 | Language 13 | MIT License 14 |

15 | 16 |

17 | binder 18 | colab 19 |

20 | 21 | 22 | # Data_Science_Interview_Questions 23 | 24 | ## Introduction 👋 25 | 26 | Here are the answers to [120 Data Science Interview Questions](http://www.datasciencequestions.com/) 27 | 28 | The above answer some is modified based on Kojin's original collection: [kojino/120-Data-Science-Interview-Questions](https://github.com/kojino/120-Data-Science-Interview-Questions) 29 | 30 | Another solution is from: [Nitish-McQueen](https://github.com/Nitish-McQueen): [Data Science Interview Questions](./DataScience_Interview_Questions.pdf) 31 | 32 | Quera has a good list of questions: [https://datascienceinterview.quora.com/Answers-1](https://datascienceinterview.quora.com/Answers-1) 33 | 34 | Feel free to send me a pull request if you find any mistakes or have better answers. 35 | 36 | --- 37 | 38 | ## Table of contents 📋 39 | 40 | | **No.** | **Name** | 41 | | ------- | -------- | 42 | | 01 | **[01_120_Python_Basics_Interview_Questions](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/01_120_Python_Basics_Interview_Questions.ipynb)** | 43 | | 02 | **[02_Predictive_Modeling](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/02_Predictive_Modeling.ipynb)** | 44 | | 03 | **[03_Programming](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/03_Programming.ipynb)** | 45 | | 04 | **[04_Probability](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/04_Probability.ipynb)** | 46 | | 05 | **[05_Statistical_Inference](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/05_Statistical_Inference.ipynb)** | 47 | | 06 | **[06_Data_Analysis](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/06_Data_Analysis.ipynb)** | 48 | | 07 | **[07_Product_Metrics](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/07_Product_Metrics.ipynb)** | 49 | | 08 | **[08_Communication](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/08_Communication.ipynb)** | 50 | | 09 | **[09_Coding](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/09_Coding.ipynb)** | 51 | | 10 | **[10_Linkedin_Skill_Assessment_Python](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/10_Linkedin_Skill_Assessment_Python.ipynb)** | 52 | | 11 | **[DataScience_Interview_Questions](https://github.com/milaan9/DataScience_Interview_Questions/blob/main/DataScience_Interview_Questions.pdf)** | 53 | 54 | These are online **read-only** versions. However you can **`Run ▶`** all the codes **online** by clicking here ➞ binder 55 | 56 | --- 57 | 58 | ## Frequently asked questions ❔ 59 | 60 | ### How can I thank you for writing and sharing this tutorial? 🌷 61 | 62 | You can Star Badge and Fork Badge Starring and Forking is free for you, but it tells me and other people that it was helpful and you like this tutorial. 63 | 64 | Go [**`here`**](https://github.com/milaan9/DataScience_Interview_Questions) if you aren't here already and click ➞ **`✰ Star`** and **`ⵖ Fork`** button in the top right corner. You will be asked to create a GitHub account if you don't already have one. 65 | 66 | --- 67 | 68 | ### How can I read this tutorial without an Internet connection? GIF 69 | 70 | 1. Go [**`here`**](https://github.com/milaan9/DataScience_Interview_Questions) and click the big green ➞ **`Code`** button in the top right of the page, then click ➞ [**`Download ZIP`**](https://github.com/milaan9/DataScience_Interview_Questions/archive/refs/heads/main.zip). 71 | 72 | ![Download ZIP](img/dnld_rep.png) 73 | 74 | 3. Extract the ZIP and open it. Unfortunately I don't have any more specific instructions because how exactly this is done depends on which operating system you run. 75 | 76 | 4. Launch ipython notebook from the folder which contains the notebooks. Open each one of them 77 | 78 | **`Kernel ➞ Restart & Clear Output`** 79 | 80 | This will clear all the outputs and now you can understand each statement and learn interactively. 81 | 82 | If you have git and you know how to use it, you can also clone the repository instead of downloading a zip and extracting it. An advantage with doing it this way is that you don't need to download the whole tutorial again to get the latest version of it, all you need to do is to pull with git and run ipython notebook again. 83 | 84 | --- 85 | 86 | ## Authors ✍️ 87 | 88 | I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏 89 | 90 | See [github's contributors page](https://github.com/milaan9/DataScience_Interview_Questions/graphs/contributors) for details. 91 | 92 | If you have trouble with this tutorial please tell me about it by [Create an issue on GitHub](https://github.com/milaan9/DataScience_Interview_Questions/issues/new). and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one. 93 | 94 | If you like this tutorial, please [give it a ⭐ star](https://github.com/milaan9/DataScience_Interview_Questions). 95 | 96 | --- 97 | 98 | ## Licence 📜 99 | 100 | You may use this tutorial freely at your own risk. See [LICENSE](./LICENSE). 101 | 102 | -------------------------------------------------------------------------------- /img/dnld_rep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/milaan9/DataScience_Interview_Questions/b515c84b6b42243f45b3621ffe552abbe9219bc7/img/dnld_rep.png --------------------------------------------------------------------------------