Swift Flashcards

1
Q

When to use a For loop or While loop?

A

For loop: when you know the number of iterations ahead of time.

While loop: when you don’t know the number of iterations ahead of time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

== VS ===

A

== tests for the Equatable protocol. ie does “abc” equal “abc”

=== tests for address matching. Do two instances of an object have the same address?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

break vs return

A

Break: will exit loop or switch

Return: will exit the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the syntax for replacing an element in an array with another element?

A

array[x] = y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the syntax for assigning a value to a dictionary?

A

dictionary[x] = y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you check if a dictionary contains a value?

A
  • dict.keys.contains(key)
  • if let value = dict[key]
  • if dict[key] != nil
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you determine how many times you want to repeat an operation?

A

First ask: What data do i need to see? all of it? some of it?

start from base case and develop from there. Consider writing out options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to iterate both index and value of an array?

A

for (index, value) in array.enumerated()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to iterate elements of a dictionary?

A

for (key, value) in dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to cast from Int to String?

A

Int(string)

returns optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to cast from string to array?

A

Array(string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you instantiate the absolute minimum as an Integer?

A

Int.min

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you instantiate the absolute max as an Integer?

A

Int.max

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to return the greater of two values?

A

max(x,y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to return the lesser of two values?

A

min(x,y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to return a lowercase version of string?

A

string.lowercased()

this returns a new object!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you break a sentence into an array of strings?

A

string.components(seperatedBy: “ “)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to return the prefix of an array?

A

array.prefix(count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to join an array of strings?

A

array.joined(separator: “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to remove and return an element in an array?

A

array.remove(at: index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Are swift parameters passed in as variables or constants?

A

Constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does inout do?

A

Allows you to modify an object in place, instead of returning a new object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What syntax do you use to pass an argument as inout?

A
  • func test(inout string: String)
  • test(string: &string)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How would you insert an element in an array?

A

array.insert(value, at: index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How to clean a string of non alpha characters?
string.filter { $0.isLetter }
26
How to convert a string to an array?
let array = Array(string)
27
What does the type Int32 do?
It stores an integer with a length of 32 bits. This is different than the Int type which stores the default system value, which could be 32 or 64.
28
How to join an array of strings?
let result = array.joined(separator: "")
29
What is the inout syntax for a func signature?
ie func reverseString(string: inout String)
30
What is the syntax for getting the first N elements of an array?
array.prefix(n)
31
How would you sort an array of this type? [[Int: Int]]
array.sort { $0.values.first! < $1.values.first! }
32
What is the diff between Sort, Sorted, and Sorted(by: )
Sort: called in place Sorted: returns a copy Sorted(by: ): returns a copy, accepts `>` or `<` as an argument
33
How to swap 2 values?
(a, b) = (b, a)
34
What does the Continue keyword do?
Prevents a loop from iterating after entering an If block
35
How do you calculate an exponent?
let result = pow(x,y)
36
What type does pow(x,y) return and how do you use it?
It returns Decimal type and it should be converted to Int/Float by returning: Int(NSDecimalNumber(decimal: n))
37
How do you pop the last element of an array?
- .removeLast() to remove it
38
What's the syntax for set?
let set: Set = [1,2,3]
39
What numbers will this iterate over? for index in 1...5
1,2,3,4,5
40
How many times will this run? for i in 0..<4
4
41
How do you get the length of an array?
array.count
42
What is the syntax to perform a swap?
(a,b) = (b,a)
43
How can you combine two strings?
stringA + stringB
44
How would you iterate an array starting at index N?
for i in N...array.count
45
How to create a Set in Swift?
let set: Set = [1,2,3]
46
How can you check if a set contains a value?
set.contains(value)
47
How to add an element to a Set?
set.insert(value)
48
How to declare a function argument as mutable?
write inout before the type func sortColors(_ nums: inout [Int])
49
What is the diff between .reverse() and .reversed() ?
.reveresed() returns new value .reverse() in place
50
What does `continue` do in a loop?
skips an iteration of the loop
51
What does `break` do in a loop?
ends the loop entirely
52
What does `Fallthrough` do in a loop?
allows for a switch case to proceed to the next case
53
How can you create N number of arrays without instantiating them manually?
var arrays: [[Int]] = [] let n = 5 for _ in 0..
54
How can you create N number of arrays without instantiating them manually? Do NOT use a for loop
let n = 5 `var arrays = [[Int]](repeating: [1,2,3], count: n)`
55
How to get absolute number in swift?
abs(n)
56
What is the time complexity of `.count` ?
o(1) on an Array or any object that conforms to `Random Access Collection` o(n) on non RAC conforming objects
57
How would you iterate an array starting at indexN while also having access to the array's element?
for element in array[1...]
58
Whats the diff between readLine() and CommandLine.arguments?
- readLine() prompts the user for input - commandLine pulls in arguments that are passed in at runtime
59
How would you access the last element of an array?
array.last
60
How to access strings by index?
Convert to array let stringArray = string.map{ String($0) }
61
Are strings an instance of Array?
No, they are an instance of Collection. They are a collection of characters
62
What is the syntax to create a Set of Integers?
let set = Set()
63
How to access tuple elements?
let tuple = (a,b) let a = tuple.0 let b = tuple.1
64
Continue vs break
Continue skips an iteration break ends the loop
65
How to slice array?
array[x...y]
66
How to clear whitespace in Swift?
import Foundation string.trimmingCharacters(in: .whitespaces)
67
What is the expected output after trimming whitespace on this string: let originalString = " Remove whitespaces "
Output: "Remove whitespaces"
68
How to filter out non-alphanumeric chars?
let output = string.filter { $0.isLetter || $0.isNumber }
69
How to get Int from pow() ?
let integer = Int(pow(Double(a), Double(b)))
70
How to pop the first element in an array?
array.removeFirst()
71
What will 0...5 print?
0,1,2,3,4,5
72
What will 0..<5 print?
0,1,2,3,4
73
What is the name of this range syntax: 0...5
closed
74
What is the name of this range syntax 0..<5
half open
75
let nums = [1,2,3] what does nums[0..<0]) return?
An empty array [ ]
76
What numbers does this return? let array = [1,2,3,4,5] array[0...3]
1,2,3,4
77
What numbers does this return? let array = [1,2,3,4,5] array[0..<3]
0,1,2,3
78
Is this operator "..." inclusive or exclusive? ie array[0...array.count]
inclusive
79
What will this return? let array = [1,2,3,4,5] array[0..
1,2,3,4,5
80
What will this return? let array = [1,2,3,4,5] array[0...array.count]
Fatal, index out of range