IOS Interview Program(Part 1)

Raghvendra Singh Rajawat
6 min readSep 17, 2023

--

I will give you a list of the programs with answers and explanations asked by the interviewer during the interview

1. Roman Number to Integer

func romanNumToInt(_ s: String) -> Int {
var number = 0
var prev : Character = " "
let romanDic : [Character:Int] = [
"I":1,
"V":5,
"X":10,
"L":50,
"C":100,
"D":500,
"M":1000]
for c in s {
if prev == "I" && (c == "V" || c == "X" ) {
number += romanDic[c]! - 2*romanDic[prev]!
} else if prev == "X" && (c == "L" || c == "C") {
number += romanDic[c]! - 2*romanDic[prev]!
}
else if prev == "C" && (c == "D" || c == "M") {
number += romanDic[c]! - 2*romanDic[prev]!
}
else {
number += romanDic[c]!
}
prev = c
}
return number
}
print("Roman To Int is :",romanNumToInt("IX"))

Output:-

In the above example, I created a Roman character’s dictionary and I have iterated the input string, and created three condition checks
1. For the Previous “I” and the last “V” or “X”.
2. For the Previous “X” and the last “L” or “C”.
3. For the Previous “C” and the last “D or “M”.
and in the else block, I stored the number for the selected character.

Let’s understand from in input I have entered “IX” The very first prev is empty so it will not satisfy any condition and it will go to the else block and it will store “C” in a variable number that is 1 and in the last of the loop I will store character in the prev variable. In the second iteration, the first condition will be satisfied because prev has “I” and c has “X”.

Now come to the condition and do the calculation from the below formula
number += romanDic[c]! — 2*romanDic[prev]!
I already have 1 in number from the first iteration and I will find the number for romanDic[c]! and romanDic[prev]! from the romanDic dictionary. so the final result will be
1 += 10 -2*1 that is 9

2. Intersection of two array

func intersectOffArray( arrayOne: [Int],  arrayTwo: [Int]) -> [Int] {
var result = [Int]()
var dictOffArrrOne = [Int: Int]()

for num in arrayOne {
dictOffArrrOne[num, default: 0] += 1
}

for num in arrayTwo {
guard let count = dictOffArrrOne[num], count > 0 else { continue }
result.append(num)
dictOffArrrOne[num] = 0
}

return result
}
print("Result:",intersectOffArray(arrayOne:[8,9,6], arrayTwo:[8,9,8,5,4,6]))

Output:-

In the above example, I created a dictionary from arrayOne with the count of each elements, that is dictOffArrrOne, and then I iterated arrayTwo and found the result by using iterated item from dictOffArrrOne.

3. Duplicate elements From the Array

func duplicateEleFromArr(_ array: [Int]) -> [Int] {
var dupArr = [Int]()
for i in 0..<array.count {
for j in (i+1)..<array.count {
if array[i] == array[j] {
dupArr.append(array[j])
}
}
}
return dupArr
}
print("Duplicate Element",duplicateEleFromArr([2,1,6,4,2,7,8,2,4]))

Output:-

In the above example, I am using two loops, One loop (parent) for the iteration with the check of the array boundary, And the other (child) loop to find the duplicate element.

4. Frequency of array Element

func duplicateEleFromArr(_ array: [Int]){
var freArr = [Int?](repeating: nil, count: array.count)
let visited = -1
for i in 0..<array.count {
var count = 1
for j in (i+1)..<array.count {
if array[i] == array[j] {
count += 1;
freArr[j] = visited
}
}
if freArr[i] != visited
{
freArr[i] = count
}
}
print("Frequency of Array Element")
for (i,_) in array.enumerated(){
if freArr[i] != visited{
print("\(array[i]) | \(freArr[i] ?? 0)")
}
}
}
duplicateEleFromArr([2,1,6,4,2,7,8,2,4])

In the above example, I have calculated the frequency of the array elements. First, I have created a “freArr” array with nil values and I have created a content variable “visited” with value -1, the length of freArr is the same as the input array.

The “freArr” array looks like [nil,nil,nil,nil,nil,nil,nil,nil,nil]

I have used two loops, one is a parent and the other one is a child. First I checked the boundary condition In the parent loop and then I created a “count” variable with the value of 1.

The child loop starts with i+1 and a counter “j” where “i” is the counter of the parent loop. I checked the duplicate element if the condition was satisfied then I increased the “count” variable by 1 and placed “visited” at the index of “j” in the “freArr” array which was -1

After completion of the child loop I placed “counter” in “freArr” array at the place that was not equal to “visited” which was -1

After completion of the first iteration of the parent loop and all iteration completion of the child loop, it will look like this.

[Optional(3), nil, nil, nil, Optional(-1), nil, nil, Optional(-1), nil]

After completion of all iterations of the parent and child loop, it will look like this

[Optional(3), Optional(1), Optional(1), Optional(2), Optional(-1), Optional(1), Optional(1), Optional(-1), Optional(-1)]

In the end, I have created a loop to print the frequency of the input array. in the loop, I made the condition in which the array element is not equal to “visited“

5. Plus One into the Array

func plusOneToArray(_ input: [Int]) -> [Int] {
var carry = 1
var result = input
for i in stride(from: result.count - 1, through: 0, by: -1) {
let sum = (result[i] + carry)
let digit = sum % 10
carry = sum / 10
result[i] = digit
}
if carry > 0 {
result.insert(carry, at: 0)
}
return result
}
print("Array After Plus One",plusOneToArray([2,2,9]))

Output:-

In The above example, I have declared one array “result” from the input and I have declared one variable carry with value 1, if you want to add more the one then change the value of one.

Next, I implemented a reverse loop, Starting from the last “result.count-1” to “0”.

Next, I calculated the sum by adding the i’th element result[i]of the result array to “visited”. The first iteration started from the last so the sum will look like this. (result[i] + carry = )9 + 1 = 10

sum = 10

Next, I calculated the digit that I would place at i’th location in the result array that will look like this sum % 10 = 10%10 = 0

digit = 0

Next, I calculated “carry” that I would use in the next iteration of the loop which will look like this, sum / 10 = 10/10 = 1

carry = 1

The same process will continue till 0'th location in the loop

After completion of the loop, I will check “carry” is greater than 0 or not, if yes then I will place “carry” at index 0

IOS Interview Program(Part 2)

https://medium.com/@rahulsinghrajawat/ios-interview-program-part2-a7c16875d657

Thank you for reading. Please clap or follow if you liked this!

--

--