## Swift Coding Challenges Book Details

Title: Swift Coding Challenges
ISBN-10: 942878347
Author: Paul Hudson
Publisher: Hacking with Swift
Language: English
Subject: Swift / Computers & Technology / Programming / Apple Programming
No. of pages: 300
Format: PDF, EPUB, HTML

## Welcome

This is not your average book about Swift coding. This is a book where I expect you to do most of the work. Sure, I’ll be helping you along with hints, and I’ll also be providing my own solutions and explanations along the way, but if you haven’t already put the work in then you’ll be missing out.
You see, this book is called Swift Coding Challenges because I really want to challenge you. There is no learning without struggle, so if you don’t take the time to read each challenge and try it for yourself in Xcode, you’ll never know how you would have fared.

### All Books From Ray Wenderlich

So, please follow these instructions to the letter if you want to reap the full benefit of what this book offers. It’s not a cheatsheet, a guide book, or even a tutorial like I would normally write. Instead, this is a book designed to make you think, to make you work, and to make you learn by doing.

Note: this book is not for Swift beginners, so you should have at least six months of Swift behind you. If you’ve completed Hacking with Swift you ought to be able to handle all the Easy and some of the Tricky problems. If you’ve completed Pro Swift you ought to be able to handle most of the Taxing problems too. If you consistently struggle with challenges, or if you’ve never seen the rethrows keyword before, you should definitely watch my Pro Swift videos.

Where I thought it was useful, I have used Big O notation to describe the computational complexity of an algorithm. The order of a function (the “O”) tells you how fast it grows, and provides a worst-case scenario of how much work must be done to run some code. An O(n) function means “the function takes as correspondingly longer as you add items.” So, if it takes one second with one item, it would take 10 seconds with ten items. In comparison, an O(1) function runs in constant time, which means a function that takes 1 second with one item would take 1 second with 100 items.

## How to use Swift Coding Challenges book

I’ve organized this book into chapters so that it’s roughly grouped by different kinds of problem. There is, inevitably, some crossover between chapters, but I’ve tried to place things where I thought best. Inside each chapter are individual challenges sorted by difficulty, so you ought to be able to flip to a chapter that interests you and just start working your way through from the beginning.
Each challenge is broken down into three parts. First, I state the problem as clearly as possible in one sentence. When necessary I’ll provide some extra clarification. When working with strings, I’ve made it clear whether you should ignore letter case (so “CAT” is equal to “cat”) or whether you should take letter case into account (so “CAT” is not equal to “cat”).

Second, I provide some example input and output so you should be able to write test cases to validate that your code is correct. I’m not saying that you need to use test-driven development, but I would suggest that if you’re going to a job interview TDD is a good skill to be able to show.
After these two steps, I encourage you to fire up Xcode and start coding. How long it takes to solve each challenge depends on your skill level, but I would suggest the following as a rough guide:

1. Novice developers, i.e. under one year of coding experience, Swift or otherwise: 15 minutes for an Easy challenge, 30 minutes for a Tricky challenge, and 1 hour for a Taxing challenge.
2. Intermediate developers, i.e. under three years of coding experience: 10 minutes for an Easy challenge, 20 minutes for a Tricky challenge, and 30 minutes for a Taxing challenge.
3. Senior developers, i.e. five years or more of coding experience: 5 minutes for an Easy challenge, 10 minutes for a Tricky challenge, and 15 minutes for a Taxing challenge.

If you fall somewhere between those groups, I’m sure you’re smart enough to extrapolate a rough goal for yourself. If you’re way beyond five years of experience then I would expect the times might come down further – perhaps as low as five minutes for a taxing challenge if you’re confident.

Obvious warning: the groupings are very broad, so don’t worry if you go over the suggested times. I personally would count writing tests and commenting your code in those times, but that’s down to you.
As you work, you might find you hit problems – and that’s OK. Remember the whole “no learning without struggle” thing? If you hit a brick wall and you’re not sure how to continue, every challenge comes with hints from me to try to point you in the right direction. You should read these only if you’ve tried and failed to complete the solution, and even then read only one hint at a time – you might find just reading the first one is enough to help you advance.

Once you have completed the challenge – which might be when you’ve written a solution that passes your tests, or might be when your self-allotted time target has lapsed – then it’s time for you to read my solution. I’ve tried to make sure every solution is as clear as possible, so sometimes I’ve used three lines of code rather than one to allow me to add more comments or discussion. Remember, interview environments are stressful enough without you striving for the perfect answer to a question – get something that works then improve on it, rather than trying to get it into a magic one liner in your first pass!

Many challenges come with more than one solution. This is sometimes to help you compare performance characteristics, sometimes because I can never resist the opportunity to teach new things, but sometimes also because I want to encourage you to be open-minded – there are lots of ways of making things work, and I hope you can appreciate the variety!

Passing a challenge

I’m afraid there’s no certificate when you complete all the challenges, but if you tweet me @twostraws I’ll high five you over the internet.
Since I’m not on hand to mark your answers, it’s down to you to self-regulate – you’re only cheating yourself, after all. I suggest you write tests for your challenges; something like this ought to work, replacing the “X” with your current challenge number:

```func yourFunctionName(input: String) -> Bool {
return true
```

}

```assert(yourFunctionName(input: "Hacking with Swift") == true,
"Challenge X did not return true")
```

I frequently use challengeX() for the names of my functions and methods, but only because it makes it easier for you to remember where they came from if you copy them into your own playgrounds.

You should be able to complete most of the challenges in a Swift playground. The Files chapter requires you to work with external files on your computer, so for those challenges you should use a macOS Command Line Tool project.
Note: sometimes it’s possible you’ll fly through a challenge graded as taxing, or struggle with a problem that’s graded easy. When this happens, please don’t send me angry emails:

1. Things that are easy for you aren’t always easy for others, and things that are hard for you aren’t always hard for others.
2. The nature of strings make them easier to work with than collections, so you’ll find many more easy problems with strings than with collections.
3. What seems easy over a glass of wine on a Sunday evening can seem insurmountable when faced with three interviewers staring at you as you pick up a whiteboard pen!

Often I refer to a solution as being naïve. This is not an insult! If your solution is the same as my naïve solution, it means you totally solved the problem and deserve a pat on the back. It does, however, mean that there are more efficient or cleaner solutions available, and I hope everyone will learn at least a little bit while reading this book.

A note on algorithms

Some of you reading this won’t have had formal computer science education, and perhaps might not have had the chance to fill any gaps on the job. If this is you, it’s very likely you might find the algorithms chapter particularly difficult – if you can’t tell a bubble sort from an insertion sort, then it’s going to be hard to answer interview questions about them.
Take heart! I do my best to explain algorithms in particular detail, and hope my explanations will help you understand how they work. If you’re able to try these challenges that’s awesome, but don’t feel discouraged if you struggle and end up relying on my solutions particularly heavily. We all have things we don’t know – even me.