Coding
Interview
Questions
By
Narasimha Karumanchi
Copyright 2017 by CareerMonk.com
All rights reserved.
Designed by Narasimha Karumanchi
Copyright 2017 CareerMonk Publications. All rights reserved.
All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means, including information storage and retrieval systems, without written permission from the publisher or author
Acknowledgements
Mother and Father, it is impossible to thank you adequately for everything you have done, from loving me unconditionally to raising me in a stable household, where your persistent efforts and traditional values taught your children to celebrate and embrace life. I could not have asked for better parents or role-models. You showed me that anything is possible with faith, hard work and determination.
This book would not have been possible without the help of many people. I would like to thank them for their efforts in improving the end result. Before we do so, however, I should mention that I have done my best to correct the mistakes that the reviewers have pointed out and to accurately describe the protocols and mechanisms. I alone am responsible for any remaining errors.
First and foremost, I would like to express my gratitude to many people who saw me through this book, to all those who provided support, talked things over, read, wrote, offered comments, allowed me to quote their remarks and assisted in the editing, proofreading and design. In particular, I would like to thank the following individuals:
Mohan Mullapudi, IIT Bombay, Architect, dataRPM Pvt. Ltd.
Navin Kumar Jaiswal, Senior Consultant, Juniper Networks Inc.
A. Vamshi Krishna, IIT Kanpur, Mentor Graphics Inc.
Ramanaiah, Lecturer, Nagarjuna Institute of Technology and Sciences, MLG
-Narasimha Karumanchi
M-Tech, IIT Bombay
Founder, CareerMonk.com
Preface
Dear Reader,
Please Hold on! I know many people do not read the preface. But I would strongly recommend that you go through the preface of this book at least. The reason for this is that this preface hassomething different to offer.
This book assumes you have some basic knowledge about computer science. The main objective of the book is not to give you the theorems and proofs about Data Structures and Algorithms. I have followed a pattern of improving the problem solutions with different complexities (for each problem, you will find multiple solutions with different, and reduced complexities). Basically, its an enumeration of possible solutions. With this approach, even if you get a new question it will show you a way to think about all possible solutions. This book is very useful for interview preparation, competitive exams preparation, and campus interview preparations.
As a job seeker if you read the complete book with good understanding, I am sure you will challenge the interviewers and that is the objective of this book.
This book is very useful for the students of Engineering Degree and Masters during their academic preparations. In all the chapters you will see that more importance has been given to problems and their analysis instead of theory. For each chapter, first you will read about the basic required theory and this will be followed by a section on problem sets. There are approximately 700 algorithmic problems and all of them are with solutions.
In most the chapters you will see more importance given to problems and analyzing them instead of concentrating more on theory. For each chapter, first you will see the basic required theory and then followed by problems.
For many problems, multiple solutions are provided with different levels of complexities. We start with the brute force solution and slowly move towards the best solution possible for that problem. For each problem we will try to understand how much time algorithm takes and how much memory the algorithm uses.
It is recommended that the reader does at least one complete reading of this book to get full understanding of all the topics that are covered. In subsequent readings you can skip directly to any chapter to refer to a specific topic. Even though, enough readings have been done for the purpose of correcting errors, there could be some minor typos in the book. If any such typos are found, they will be updated at .
Wish you all the best. I am sure that you will find this book useful.
-Narasimha Karumanchi
M-Tech, IIT Bombay
Founder, CareerMonk.com
Table of Contents
Coding Interview Questions
Other titles by Narasimha Karumanchi
IT Interview Questions
Elements of Computer Networking
Data Structures and Algorithms Made Easy (C/C++)
Data Structures and Algorithms Made Easy in Java
Data Structures and Algorithmic Thinking with Python
Data Structures and Algorithms for GATE
Peeling Design Patterns
The objective of this chapter is to explain the basics of programming. In this chapter you will know about data types, pointers, scoping rules, memory layout of program, parameter passing techniques, types of languages and problems related to them.
1.1Variables
Before getting in to the definition of variables, let us relate them to an old mathematical equation. Many of us would have solved many mathematical equations since childhood. As an example, consider the equation below:
x2 + 2y 2 = 1
We dont have to worry about the use of this equation. The important thing that we need to understand is that the equation has names (x and y), which hold values (data). That means the names (x and y) are placeholders for representing data. Similarly, in computer science programming we need something for holding data, and variables is the way to do that.
1.2Data types
In the above-mentioned equation, the variables x and y can take any values such as integral numbers (10, 20), real numbers (0.23, 5.5), or just 0 and 1. To solve the equation, we need to relate them to the kind of values they can take, and data type is the name used in computer science programming for this purpose. A data type in a programming language is a set of data with predefined values. Examples of data types are: integer, floating point, unit number, character, string, etc.