Recover Binary Tree

Question: You have a binary tree. But only two of its elements have been swapped. This makes it a faulty binary tree. The challenge is to swap those two elements. And fix the tree. Solution I knew I had to traverse it. But then what? With a little help from reading online, I realized I should traverse it, and store the values into an array. Then you just loop the array and find the bad indexes. ...

How to Calculate the Middle Index?

A good number of interview questions require you to constantly split an array/string in half. This is relatively easy to achieve when the array count is odd. However when the count is even, it’s not as easy. let a = [1,3,8,10,22] // middle index is 2 let b = [1,3,8,10] // middle index is 1.5 which is non-existent. So what now? Most important thing to note is: There’s no such thing as “middle index” when the count is even, I mean there’s two middles in that case. I guess the “middle” are those two indices.‌‌ ‌But then you have to pick one. For this reason it’s often better to think of it as a pivot because it’s really the 2nd index. The 2nd index isn’t the middle index of a 4 element array. ...

Unicode Security

This post is the result of some post-discussion on my Swift Strings for iOS interviewing post. Given that characters look like one another or that are invisible code points I thought there might be some room for abuse. Luckily Unicode has great documentation security. At the high level there are two types of security issues: Visual Security Issues Suppose that the user gets an email notification about an apparent problem in their Citibank account. Security-savvy users realize that it might be a spoof; the HTML email might be presenting the URL http://citibank.com/… visually, but might be hiding the real URL. They realize that even what shows up in the status bar might be a lie, because clever Javascript or ActiveX can work around that. (And users are likely to have these turned on, unless they know to turn them off.) They click on the link, and carefully examine the browser’s address box to make sure that it is actually going to http://citibank.com/…. They see that it is, and use their password. However, what they saw was wrong—it is actually going to a spoof site with a fake “citibank.com”, using the Cyrillic letter that looks precisely like a ‘c’. They use the site without suspecting, and the password ends up compromised. ...

Compress String

Question A simple compression algorithm would be to replace repeating characters with their count. Example aaa --> a3 aaabb --> a3b2 aaabbaa --> a3b2a2 Want to stand out? Ask if there’s a difference between a & A and if you need to add uppercase & lowercase together or need to separate them. In my implementation I assumed they’re different. Code // start counting, then upon seeing a diff, write the count. func compress(_ str: String) -> String { let chars = Array(str) guard !chars.isEmpty else { return str } var currentChar = chars.first! var currentCount = 1 var ans: String = "" guard chars.count > 1 else { return "\(currentChar)\(currentCount)"} for i in 1..<chars.count { if chars[i] == currentChar { // increment currentCount += 1 } else { // append subsection ans.append("\(currentChar)\(currentCount)") // reset currentChar = chars[i] currentCount = 1 } } ans.append("\(currentChar)\(currentCount)") return ans } Tests print(compress("aaabbbbccaA")) // a3b4c2a1A1 print(compress("a")) // a1 print(compress("")) //

Check if Two Strings Are One Edit Away

Question You can edit a string in three different ways: insert, remove or replace a character. Write a function to see if two strings are one or zero edits away. Example like, like --> true (zero edits) like, likes --> true (one edit: remove/insert) like, life --> true (one edit, replace) like, lik --> true (one edit: remove/insert) like, pine --> false (two edits) like, lion --> false (two edits) Strategy For most questions that require you to return true or false, you can reduce the scope of the question by removing noise. Often that means exiting early or having conditions that make it impossible to have a true result… Example: If the string lengths differ by 2 or more characters, then you can exit / return an answer. Try not to solve this problem in one go. Break it down into two checks: ...

Swift Strings for iOS interviewing

This is bit of fast paced intro into Swift Strings. ASCII, Unicode and the challenges they introduce. ASCII Ages ago, only characters that existed were just a to z, A to Z, and bunch of other English characters. This was problematic. You were limited to only 7 bits i.e. only 2 ^ 7 - 1 = 127 characters. Also non-English characters where not part of ASCII. Literally the name says ‘American Standard Code for Information Interchange’. So characters like å or ä were not part of it. Let alone ب or ج or characters of other languages. ...

How to Prepare Yourself as a Podcast Guest?

The first time I listened to an iOS podcast was yrs ago. I was struggling to grasp basic iOS concepts and podcasts weren’t really covering basic technical material. It just didn’t seem like the right way of learning about things. Two weeks ago I got invited to Side Project Spotlight Podcast. I was thrilled but also clueless. Had to do some googling, listen to a few episodes and purchase some equipment. Here are the things I learned throughout the process. ...

self-contained rv

What is a Self Contained Build Script?

So I had this need to add a brew package named gh into our build script. I spoke with with the team that handled our agents and asked them to add a new package on the agents. I was told that the package has to be included as part of the build script and that build scripts need to be self-contained. At first I didn’t fully understand what ‘self-contained’ means in this context but as I digged more into our Jenkinsfile I learned what it meant. First let’s figure out: ...

dispose bag

CurrentValueSubject Example

I googled a bit for “CurrentValueSubject Example”. Surprisingly I wasn’t able to find a simple answer. So I created a few examples: Basic import UIKit import Combine class ViewController: UIViewController { var name = CurrentValueSubject<String, Never>("Jason") override func viewDidLoad() { super.viewDidLoad() setup() } func setup() { let _ = name.sink { value in print(value) // Jason, Jason Bourne } name.send("Jason Bourne") } } Basically every time you call send on a publisher, the subscriber gets a callback. ...

git cherry-pick

How to cherry-pick a library fix?

I’ve done this so many times, but every time I do it with hesitance. It’s usually because I’m not certain I’m finding the right commit or if I’m applying it the right way. These steps explain the process and how to validate that you’re doing thins the right way. Find out the library version that your release is using. Check out that version of your library. Create a new branch off that version. Find the SHA of the commits you want to cherry pick. Cherry pick them in and validate you’ve cherr-picked correctly. Make a new release/tag. Go back to your main app. Check out the release branch again. Create a new release version Update your Podfile (or whatever it is that you’re using) to pull the updated version. All the steps together ...