IOS Interview Program (Part 3)

Raghvendra Singh Rajawat
5 min readOct 6, 2023

--

List of some other program

1. Move zero at the end of the array

Some of the interviewers will give you an array and tell you “Please move all 0 or some numbers at the end of the array”.

Let’s see in the example

func moveZeroesAtEnd(_ array: inout [Int]) {
for index in array.indices.reversed() where array[index] == 0 {
array.remove(at: index)
array.append(0)

}
}
var arr = [2,0,9,0,8,4]
print("Array Before Move:",arr)
moveZeroesAtEnd(&arr)
print("Array After Move:",arr)

In the above example I have created a function moveZeroesAtEnd with input param means I will pass the array by reference. I also used the “where clause” in the for loop. on where clause I wrote condition The value should be equal to 0, so the loop only executes for those values equal to 0, The loop iteration is in reversed order.

I removed 0 from his position inside the loop and placed it at the end.

Output:-

2. Some interviewers will ask you to create a stack with all operations in Swift language

Let’s create

struct Stack<T> {
private var stackData:[T] = []
private var top : Int = -1
mutating func push(_ element:T) {
stackData.append(element)
top += 1
}
mutating func pop() {
if !isEmpty() {
stackData.remove(at: top)
top -= 1
}else{
top = -1
print("Stack is Empty")
}
}
func peek() -> T? {
return stackData.last
}
func isEmpty() -> Bool {
if(top == -1){
return true
}else{
return false
}
}
func prinStack(_ str:String) {
print("\(str)",stackData)
}
}
var stackObj = Stack<Int>()
print(stackObj.peek() == nil ? "Stack is Empty" : "Peek Element: \(stackObj.peek()!)")
stackObj.push(10)
stackObj.push(20)
stackObj.prinStack("Stack Without pop:")
stackObj.pop()
stackObj.prinStack("Stack With one pop:")
print(stackObj.peek() == nil ? "Stack is Empty" : "Peek Element: \(stackObj.peek()!)")
print(stackObj.isEmpty() ? "Stack is Empty":"Stack Has data")
stackObj.pop()
print(stackObj.peek() == nil ? "Stack is Empty" : "Peek Element: \(stackObj.peek()!)")
print(stackObj.isEmpty() ? "Stack is Empty":"Stack Has data")

Output:-

No need to explain anything everything is described clearly. I created a stack as a generic type that works with all types like Int, and String. Also created methods for the stack operations like push, pop, to check peek, and to check whether the stack is empty or not

3. Same They can ask to create a linklist

Let’s Create Linklist


class Node<T> {
var value: T
var next: Node<T>?

init(value: T) {
self.value = value
self.next = nil
}
}

class LinkedList<T:Equatable> {
var head: Node<T>?

func isEmpty() -> Bool {
return head == nil
}

func append(value: T) {
let newNode = Node(value: value)
if head == nil {
head = newNode
} else {
var current = head
while current?.next != nil {
current = current?.next
}
current?.next = newNode
}
}

func delete(value: T) {
if head == nil {
return // List is empty, nothing to delete
}

if head!.value == value {
head = head?.next // If the head node contains the value to be deleted, update the head
return
}

var previous: Node<T>? = nil
var current = head

while current != nil {
if current!.value == value {
previous?.next = current?.next // Skip the current node
return
}
previous = current
current = current?.next
}
}

func printList() {
var current = head
while current != nil {
print(current!.value, terminator: " -> ")
current = current?.next
}
print("nil")
}
}


let linkedList = LinkedList<Int>()
linkedList.append(value: 1)
linkedList.append(value: 2)
linkedList.append(value: 3)
linkedList.append(value: 4)

print("Original list:")
linkedList.printList()

linkedList.delete(value: 3)

print("List after deletion:")
linkedList.printList()

Output:

In the above example, I created a Node. The Node class represents a single node in the linked list. It contains a value of generic type T and an optional reference to the Next node.

Next, I created the class LinkList whose type is generic and conforming to Equatable.

Next, I declared a Node variable Head. And I declared a function “isEmpty” to check whether the head is null or not.

Next, I declared a function append() to add a new node to the Linklist. In the append function, I created a node with an input value and checked whether the head was nill or not if the head was nil then I set the new node as a head of the link list

On the else block, I found the last node so first I assigned the node to the current variable and in the next line I used the while loop to find the end of the link list, After getting nill in the current node I assigned the new node to the next of current node.

Next, I declared a function delete() to delete a node from the link list. In the delete() function I will check if the head is nill or not if the head is nill then I will return without deletion, Next, I will compare the head value with the value that I want to delete, If the condition matches then I will delete the head value and set the next of head as the head, and return from the function.

Next, I will declare a variable “previous” as Node type and I will declare a variable current that stores the head value, Next I will create a while loop that will execute until I get head as a nill. Inside the loop, I will compare the current.value with the value that I want to delete, if match found then I will assign the current value to the next of the previous node, which means I will skip the matching node that want to delete and return from the function. If the match is not found then I will assign the current node to the previous and in the current node I will assign the next of the current node.

In the end, I will create a last function “printList” to print the link list. On that function, I will create a variable current that initially holds the head after that I will execute the loop till the end of the link list, Inside the loop I will print the value of the current node and assign the next of current node to the current node and at the end i will print nil that denotes the end of the Link list.

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

IOS Interview Program(Part 1)

IOS Interview Program(Part 2)

--

--