Problem solving in Swift- Part 1

Varun Tomar
2 min readMay 20, 2020

--

Photo by Olav Ahrens Røtne on Unsplash

Problem solving using different data structures and algorithms is always on top of mind when we get interviewed for big names in technology. I will come with 2 new problems and their optimised solution every day (follow me for fresh article). Let’s start with some problems and their optimised solution using Swift.

Problem 1

Reverse a String without using “reversed()” in-built function in Swift :

Solution 1 : Convert String to an array of characters and then swap characters by iterating array from both side. We are going to write String extension for the same. Refer below code. Time complexity for this code is O(n), it’s linear.

Solution 2 : Using Swift higher order function “reduce”. Refer below code. Complexity for this is again O(n)

Problem 2 :

We have to print factorial of large number.

Solution : As factorial of a large number would exceed integer size limit, thus we need to use some other data structure for it to store and process. Let’s make use of an Array of integer.

Continuing move to Part 2

Thanks for reading this🙏🙏. Any feedback in the comments would be appreciated. If you enjoyed reading this post, please share and give some claps. I will come with 2 new problems and their optimised solution every day.

You can follow me for fresh articles.

--

--

Varun Tomar
Varun Tomar

No responses yet