General Flashcards
1
Q
Stack vs. Heap
A
Stack:
- linear data structure
- high-speed access
- OS manages space efficiently (memory will never become fragmented)
- memory allocated in continuous block
- issue with memory shortage
- can be implemented with array, dynamic memory and linked list
- variables are allocated on the stack in function call stack
- do not need to worry about memory allocation and deallocation of stack variables
Heap:
- hierarchical data structure
- slower than stack
- space is not used efficiently; memory can become fragmented
- memory is allocated in any random order
- memory is allocated and deallocated manually by the programmer
- can be implemented with arrays and trees
2
Q
Reference Counting vs. Garbage Collection
A
Reference Counting:
- keeps track of object reference counts
- releases objects automatically as necessary
- no background processing
- cannot cope with retain cycles
Garbage Collection:
- runtime detects unused objects and releases them at indeterminate time intevals
- can clean up retain cycles
- happens in the background but might cause other threads to be put on hold temporarily
3
Q
Stack vs. Queue
A
Stack:
- LIFO (last in first out)
- linear data structure
- item is inserted at the top with push and deleted from the top with pop
- top points to last element in the list
Queue:
- FIFO (first in fist out)
- linear data structure
- items can be inserted only at the rear and can be deleted only from the front
- insert item at the rear with enqueue, and delete item from the front with dequeue
- front and rear pointers
4
Q
Recursion vs. Iteration
A
Recursion:
- calls itself
- small amount of code
- time complexity can become exponential
- large overhead due to repeated function calls
- better choice if time complexity is not an issue and shortness of code is
- infinite repetition can lead to CPU crash
Iteration:
- loops
- larger amount of code due to repetition of a block of code
- time complexity is generally lesser
- no overhead as in recursion
- infinite iteration will lead to infinite loops which may or may not lead to system errors
5
Q
Necessity of multithreading
A
- can execute multiple parts of a program at the same time
- enables resource sharing
- increases program responsiveness
- utilizes multiprocess architecture
6
Q
How to fix a memory leak
A
-use XCode debug memory graph to detect simple retain cycles. It displays all objects currently alive. Purple exclamation mark indicate related leaked objects.
- in the box at top projectName > device, click Edit scheme
- select Malloc Scribble which will fill freed memory with a predefined value to make it more obvious when memory is leaked
- select Logging>Malloc Stack>Live Allocations Only which allows Xcode to build an allocation backtrace
- make the debug console visible
- run the app
- click the Memory Debugger button that looks like three joined circles in a >.
- this will display the heap contents with purple exclamation marks beside the items in the debug navigator
- ———————————————————————————-
- use Allocations instrument which tracks all objects the app allocates over the course of its run
- ———————————————————————————-
- use Leaks instrument to check all memory and figure out the leaked objects
7
Q
How to fix a laggy list
A
- fetch data asynchronously
- update table or collection view right after data is retrieved
- load images asynchronously and cache them
8
Q
Tail Recursion
A
- a special form of recursion where last operation of a function is a recursive call
- considered better because it can be optimized by the compiler
function tailrecsum(x, running_total = 0) { if (x === 0) { return running_total; } else { return tailrecsum(x - 1, running_total + x); } }
9
Q
software licensing
A
- a document that provides legally binding guidelines for the use and distribution of software
- main types are proprietary, free or open source.