Brain Teasers with Coding for Data Scientist
Copyright 2020 Virag R Shah
All rights reserved.
For additional information, contact the author.
Virag R. Shah: viragbook@gmail.com
DISCLAIMER
Every effort has been made to avoid errors or omissions in the publication. In spite of this, some errors might have crept in. The publisher or author shall not be responsible for the same. Any mistake, error or discrepancy noted may be brought to our notice which shall be taken care of in the next edition. It is notified that neither the publisher nor the author will be responsible for any damage or loss of action of any one, of any kind, in any manner, therefore.
ACKNOWLEDGMENTS
First of all, I want to thank you for choosing this book, Brain Teasers with Coding for Data Scientist. I sincerely hope you will enjoy the brain teasers in this book.
The brain teasers are chosen from a collection of problems that I have encountered over the years in interviews, books, websites and blogs. They are represented in a generalized form in this book.
CONTENTS
Title | Page No. |
INTRODUCTION | ii |
Brain Teaser Titles | Page No. |
| HELLO WORLD | |
| BASIC CODING | |
| N ANTS | |
| N STAIRS | |
| DEATH RAT | |
| BULLET CURVE | |
| BIRTHDAY MATCH | |
| FAIR COIN | |
| N COINS FLIP | |
| N BAGS | |
| RAIN IN THE CITY | |
| N PRISONERS & N BOXES | |
| N TOWERS OF HANOI | |
| CLOCK ANGLE | |
| BALLS ON A PLANE | |
SOLUTION | |
ABOUT AUTHOR | |
INTRODUCTION
Brain teasers, kind of puzzles built for lateral thinking, can help to improve cognitive skills. Lateral thinking is a way of solving problems using an indirect and creative approach via reasoning that is not immediately obvious. It involves ideas that may not be obtainable using only traditional step-by-step logic.
Traditionally during software engineering or analytics positions hiring, some companies may ask brain teaser(s) to test how a candidate thinks and approaches a new problem when faced with one. Data Science started with statistics has evolved to include the concepts of software engineering and so does its hiring practices. Whether you are trying to improve your coding skills or preparing for a data science interview, you should at least try on them to test your critical thinking and logic. In data science, statistics is a great supplement to the traditional coding brain teasers.
In this book, you will find interesting brain teasers that are suitable for data scientists as well as software engineers. Solutions also have included coding where possible. So, get ready and jump into the world of fascinating and exciting brain teasers.
1 HELLO WORLD
Lets start with basic coding. Python and R are the most widely used programming languages among others (for example, Java, Scala, Matlab) for data science.
Python code snippet is given below:
if :
print("Hello")
else:
print("World")
Whats the so that the code snippet prints Hello World?
2 BASIC CODING
Print I am 10th in every computer language you know.
3 N ANTS
Imagine there are N ants in N different corners of an equilateral polygon, and each ant randomly picks a direction and starts traversing the edge of the polygon. Whats the probability that none of the ants collide? (Provided condition: N>2)
Write a code.
4 N STAIRS
There are n stairs; a person standing at the bottom wants to reach the top. The person can take any number k steps. How many distinct ways can the person reach the top of the staircase?
Write a code.
5 DEATH RAT
You have X wine bottles, one of which is poisoned. You want to determine which bottle is poisoned by feeding the wine to the rats. The poisoned wine takes Z hour to work. How many rats are necessary to find the poisoned bottle in Y hour? (Provided condition: Y is multiple of Z.)
Write a code.
6 BULLET CURVE
N persons are standing in a circle. They are labeled from 1 to N in clockwise order. Every one of them is holding a gun and can shoot a person on his left. Starting from person 1, they starts shooting in order e.g for N=100, person 1 shoots person 2, then person 3 shoots person 4, then person 5 shoots person 6..then person 99 shoots person 100, then person 1 shoots person 3, then person 5 shoots person 7and it continues till all are dead except one. Whats the index of that last person?
Write a code.
7 BIRTHDAY MATCH
There are X students in the class. What is the possibility that at least 2 students share the same day of birth? (Provided condition: No student is born in a leap year.)
Write a code.
8 FAIR COIN
Make a fair (unbiased) coin from an unfair (biased) coin.
Write a code.
9 N COINS FLIP
You have an N coins laying flat on a table, each with a head side and a tail side. M of them are heads up, rest are tails up. You cant feel, see or in any other way find out which M are heads up. Your goal: split the coins into two piles so there are the same numbers of heads-up coins in each pile.
Write a code.
10 N BAGS
You have N bags full of coins. In each bag are infinite coins. But one bag is full of forgeries, and you cant remember which one. But you do know that genuine coins weigh 1 gram, but forgeries weigh 1.1 grams. You have to identify that bag in minimum readings. You are provided with a digital weighing machine.
11 RAIN IN THE CITY
You are about to get on a plane to Mumbai, you want to know whether you have to bring an umbrella or not. You call N of your random friends and ask each one of them if its raining. The probability that your any friend is telling the truth is 2/3 and the probability that (s)he is playing a prank on you by lying is 1/3. If all N of them tell that it is raining, then what is the probability that it is actually raining in Mumbai.
12 N PRISONERS & N BOXES
The director of a prison offers N death row prisoners, who are numbered from 1 to N, a last chance. A room contains a cupboard with N boxes. The director randomly puts one prisoner's number in each closed boxes. The prisoners enter the room, one after another. Each prisoner may open and look into N/2 boxes in any order. The boxes are closed again afterwards. If, during this search, every prisoner finds his number in one of the boxes, all prisoners are pardoned. If just one prisoner does not find his number, all prisoners die. Before the first prisoner enters the room, the prisoners may discuss strategy but may not communicate once the first prisoner enters to look in the boxes. What is the prisoners' best strategy?
13 N TOWERS OF HANOI
There are N pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are M discs which have radius 1 to M. You are required to do the transformations in minimal number of moves. A move consists of picking the topmost disc of any one of the pegs and placing it on top of any other peg. At any point of time, the decreasing radius property of all the pegs must be maintained.