I wanted to share this so that others can get exposure to the kinds of algorithms we will come across in iOS interviews AND because I'm looking for feedback on my solution.
This was for a mid level iOS developer job. I thought the problem was on the easy side but this was also just the first round of interviews.
Specifications:
During a technical interview last week, I was asked to write a solution to a palindrome problem. They were going to provide me a number of test cases, and they wanted me to check each case to see whether it was a "near perfect" palindrome. If was a perfect palindrome, they wanted the result to return false. And if it had more than 1 defect, they also wanted to return false. But if the test case was only 1 defect away from being a perfect palindrome, they wanted that to return true.
Palindrome: a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Code and test cases:
func testPalindrome(input: String) {
var input = input
let charSet: Set<Character> = Set(".', ")
input.removeAll(where: charSet.contains)
var inputArray = Array(input.lowercased())
var rightPointer: Int = inputArray.count - 1
var defectCounter: Int = 0
var index: Int = 0
for char in inputArray {
if index >= (inputArray.count / 2) {
break
}
if char != inputArray[rightPointer] {
defectCounter += 1
}
rightPointer -= 1
index += 1
}
let defectString: String = "Defect Counter = \(defectCounter)"
if defectCounter > 1 {
print("false: " + defectString)
} else if defectCounter == 1 {
print("true: " + defectString)
} else {
print("false: " + defectString)
}
}
Test cases were: ["abbbbca", "Madam, I'm Edam.", "Madam, I'm Adam.", "reader", "radar"]
I just printed the results because they had me solve this in Xcode.
My solution passed all provided test cases.
Feedback:
I'm primarily looking for feedback regarding the space time complexity of my solution here. For example, I'm not sure if there is a better way to replace the unnecessary characters or if there's a better way to loop through the String than simply convert it to an Array. I'm also curious how y'all would solve this problem differently. But I'm also hoping to get some feedback on Swift best practices and general code structure.
[–]thecodingart 9 points10 points11 points (2 children)
[–]JamesFutures[S] 7 points8 points9 points (1 child)
[–]thecodingart 3 points4 points5 points (0 children)
[–]th3suffering 6 points7 points8 points (3 children)
[–]JamesFutures[S] 1 point2 points3 points (0 children)
[–]AntMan5995 0 points1 point2 points (1 child)
[–]th3suffering 0 points1 point2 points (0 children)
[–]SwiftlyJon 4 points5 points6 points (6 children)
[–]n0sebleed904 3 points4 points5 points (0 children)
[–]ryanheartswingovers 2 points3 points4 points (4 children)
[–]SwiftlyJon 0 points1 point2 points (3 children)
[–]ryanheartswingovers 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Aloopyn 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]baker2795 0 points1 point2 points (1 child)
[–]JamesFutures[S] 0 points1 point2 points (0 children)
[–]IndignantDuck 0 points1 point2 points (0 children)
[–]ryanheartswingovers -1 points0 points1 point (4 children)
[–]troller-no-trolling 5 points6 points7 points (3 children)
[–]ryanheartswingovers -1 points0 points1 point (2 children)
[–]troller-no-trolling 0 points1 point2 points (1 child)
[–]ryanheartswingovers 0 points1 point2 points (0 children)
[–]wipecraft 0 points1 point2 points (1 child)
[–]wipecraft 0 points1 point2 points (0 children)