Table of Contents Data Structures and Algorithms Made Easy Fifth Edition
Table of Contents Data Structures and Algorithms Made Easy Fifth Edition
And
Algorithms
Made Easy
By
Narasimha Karumanchi
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
𝑀𝑜𝑡ℎ𝑒𝑟 and 𝐹𝑎𝑡ℎ𝑒𝑟, 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 express my
gratitude to all of the people 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:
𝑀𝑜ℎ𝑎𝑛 𝑀𝑢𝑙𝑙𝑎𝑝𝑢𝑑𝑖, IIT Bombay, Architect, dataRPM Pvt. Ltd.
𝑁𝑎𝑣𝑖𝑛 𝐾𝑢𝑚𝑎𝑟 𝐽𝑎𝑖𝑠𝑤𝑎𝑙, Senior Consultant, Juniper Networks Inc.
𝐴. 𝑉𝑎𝑚𝑠ℎ𝑖 𝐾𝑟𝑖𝑠ℎ𝑛𝑎, IIT Kanpur, Mentor Graphics Inc.
Cathy Reed, BA, MA, Copy Editor
−𝑁𝑎𝑟𝑎𝑠𝑖𝑚ℎ𝑎 𝐾𝑎𝑟𝑢𝑚𝑎𝑛𝑐ℎ𝑖
M-Tech, 𝐼𝐼𝑇 𝐵𝑜𝑚𝑏𝑎𝑦
Founder, 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚
Preface
Dear Reader,
Please hold on! I know many people typically do not read the Preface of a book. But I strongly recommend
that you read this particular Preface.
It is not the main objective of this book to present you with the theorems and proofs on 𝑑𝑎𝑡𝑎 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒𝑠 and
𝑎𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠. 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, it’s an
enumeration of possible solutions. With this approach, even if you get a new question, it will show you a way to
think about the possible solutions. You will find this book useful for interview preparation, competitive exams
preparation, and campus interview preparations.
As a 𝑗𝑜𝑏 𝑠𝑒𝑒𝑘𝑒𝑟, if you read the complete book, I am sure you will be able to challenge the interviewers. If you
read it as an 𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟, it will help you to deliver lectures with an approach that is easy to follow, and as a result
your students will appreciate the fact that they have opted for Computer Science / Information Technology as
their degree.
This book is also useful for 𝐸𝑛𝑔𝑖𝑛𝑒𝑒𝑟𝑖𝑛𝑔 𝑑𝑒𝑔𝑟𝑒𝑒 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 and 𝑀𝑎𝑠𝑡𝑒𝑟𝑠 𝑑𝑒𝑔𝑟𝑒𝑒 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 during their academic
preparations. In all the chapters you will see that there is more emphasis on problems and their analysis rather
than on theory. In each chapter, you will first read about the basic required theory, which is then followed by a
section on problem sets. In total, there are approximately 700 algorithmic problems, all with solutions.
If you read the book as a 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 preparing for competitive exams for Computer Science / Information
Technology, the content covers 𝑎𝑙𝑙 the 𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑 topics in full detail. While writing this book, my main focus was to
help students who are preparing for these exams.
In all the chapters you will see more emphasis on problems and analysis rather than on theory. In each chapter,
you will first see the basic required theory followed by various problems.
For many problems, 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 solutions are provided with different levels of complexity. We start with the
𝑏𝑟𝑢𝑡𝑒 𝑓𝑜𝑟𝑐𝑒 solution and slowly move toward the 𝑏𝑒𝑠𝑡 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 possible for that problem. For each problem, we
endeavor to understand how much time the 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 gain a full understanding of
all the topics that are covered. Then, in subsequent readings you can skip directly to any chapter to refer to a
specific topic. Even though many readings have been done for the purpose of correcting errors, there could still
be some minor typos in the book. If any are found, they will be updated at 𝑤𝑤𝑤. 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚. You can
monitor this site for any corrections and also for new problems and solutions. Also, please provide your valuable
suggestions at: 𝐼𝑛𝑓𝑜@𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚.
I wish you all the best and I am confident that you will find this book useful.
−𝑁𝑎𝑟𝑎𝑠𝑖𝑚ℎ𝑎 𝐾𝑎𝑟𝑢𝑚𝑎𝑛𝑐ℎ𝑖
M-Tech, 𝐼𝐼𝑇 𝐵𝑜𝑚𝑏𝑎𝑦
Founder, 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚
Other Books by Narasimha Karumanchi
IT Interview Questions
Data Structures and Algorithms for GATE
Data Structures and Algorithms Made Easy in Java
Coding Interview Questions
Peeling Design Patterns
Elements of Computer Networking
Data Structures and Algorithmic Thinking with Python
Table of Contents
1. Introduction------------------------------------------------------------------------------------ 13
1.1 Variables --------------------------------------------------------------------------------------------------- 13
1.2 Data Types ------------------------------------------------------------------------------------------------- 13
1.3 Data Structures ------------------------------------------------------------------------------------------- 14
1.4 Abstract Data Types (ADTs) ----------------------------------------------------------------------------- 14
1.5 What is an Algorithm? ----------------------------------------------------------------------------------- 14
1.6 Why the Analysis of Algorithms? ----------------------------------------------------------------------- 15
1.7 Goal of the Analysis of Algorithms --------------------------------------------------------------------- 15
1.8 What is Running Time Analysis? ----------------------------------------------------------------------- 15
1.9 How to Compare Algorithms ---------------------------------------------------------------------------- 15
1.10 What is Rate of Growth? ------------------------------------------------------------------------------- 15
1.11 Commonly Used Rates of Growth --------------------------------------------------------------------- 16
1.12 Types of Analysis ---------------------------------------------------------------------------------------- 17
1.13 Asymptotic Notation ------------------------------------------------------------------------------------ 17
1.14 Big-O Notation [Upper Bounding Function] --------------------------------------------------------- 17
1.15 Omega-Ω Notation [Lower Bounding Function] ----------------------------------------------------- 19
1.16 Theta- Notation [Order Function] ------------------------------------------------------------------- 19
1.17 Important Notes ----------------------------------------------------------------------------------------- 20
1.18 Why is it called Asymptotic Analysis? ---------------------------------------------------------------- 20
1.19 Guidelines for Asymptotic Analysis ------------------------------------------------------------------- 20
1.20 Simplyfying properties of asymptotic notations ---------------------------------------------------- 22
1.21 Commonly used Logarithms and Summations ----------------------------------------------------- 22
1.22 Master Theorem for Divide and Conquer Recurrences -------------------------------------------- 22
1.23 Divide and Conquer Master Theorem: Problems & Solutions ------------------------------------ 23
1.24 Master Theorem for Subtract and Conquer Recurrences ----------------------------------------- 23
1.25 Variant of Subtraction and Conquer Master Theorem--------------------------------------------- 24
1.26 Method of Guessing and Confirming ----------------------------------------------------------------- 24
1.27 Amortized Analysis -------------------------------------------------------------------------------------- 25
1.28 Algorithms Analysis: Problems & Solutions --------------------------------------------------------- 25
4. Stacks ------------------------------------------------------------------------------------------ 87
4.1 What is a Stack? ------------------------------------------------------------------------------------------ 87
4.2 How Stacks are used ------------------------------------------------------------------------------------- 87
4.3 Stack ADT -------------------------------------------------------------------------------------------------- 88
4.4 Applications ------------------------------------------------------------------------------------------------ 88
4.5 Implementation-------------------------------------------------------------------------------------------- 88
4.6 Comparison of Implementations ------------------------------------------------------------------------ 93
4.7 Stacks: Problems & Solutions --------------------------------------------------------------------------- 93