What Is An Algorithm
What Is An Algorithm
1. Finiteness:
- An algorithm must have a finite number of steps or instructions. It should eventually come to an
end after executing a defined sequence of operations.
2. Definiteness:
- Each step of the algorithm must be precisely and unambiguously defined. There should be no
ambiguity or confusion in interpreting the instructions.
3. Input:
- An algorithm takes input, processes it, and produces the desired output. The input is the
information provided to the algorithm at the beginning to perform the required task.
4. Output:
- After executing the steps, the algorithm produces a result or output. The output should relate to
the problem being solved and should be clearly defined.
5. Effectiveness:
- An algorithm should be effective, meaning that it should solve the problem for any valid input
data. It should be able to produce the correct output within a reasonable amount of time and
resources.
- Encapsulation is the bundling of data and methods that operate on the data into a single unit,
often a class, to control access and protect the data.
2. Polymorphism:
- A class is a blueprint for creating objects in object-oriented programming, defining attributes and
behaviors that the objects will have.
4. Structure:
- In programming, a structure is a composite data type that groups together variables of different
data types under a single name for organizational purposes.
What is the difference between Stack and Queue. Explain with a coding
example.
A stack and a queue are both an abstract data structure that represent collections of elements with
two main operations:
1. Stack:
- A stack is a Last In, First Out (LIFO) data structure, meaning the last element added is the first one
to be removed.
- The operations on a stack are typically called push (to add an item to the top of the stack) and
pop (to remove the item from the top).
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
# stack [1,2,3]
2. Queue:
- A queue is a First In, First Out (FIFO) data structure, meaning the first element added is the first
one to be removed.
- The operations on a queue are typically called enqueue (to add an item to the back of the queue)
and dequeue (to remove the item from the front).
# Example usage:
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
# Queue [1,2,3]
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
function createSLLFromArray(array) {
return null;
current = current.next;
return head;
function createSLLFromValue(val) {
if (!head) {
return newNode;
current = current.next;
}
current.next = newNode;
return head;
function displaySLL(head) {
current = current.next;
console.log();
displaySLL(headFromArray);
displaySLL(headSingleValue);
displaySLL(headSingleValue);
The latest version of Swift is "Swift 5.9.1", which was released on October 19, 2023
2. Native Error Handling: Swift provides support for throwing, catching, propagating, and
manipulating errors at runtime.
3. Structs and Classes: Swift allows you to define a structure or class in a single file, and the
external interface is made available for other code to use.
4. Protocol Extensions: Swift allows you to define behavior on protocols themselves, rather than
in global functions or individual conformances.
5. Memory Safety: Swift automatically manages memory and prevents unsafe behavior from
happening in your code.
6. Memory Management: With Automatic Reference Counting (ARC), Swift tracks and manages
your app’s memory usage.
7. Flexible Enumerations: Swift enums support pattern matching and can have payloads.
8. Package Manager: The Swift package manager is a cross-platform tool you can use to build,
run, test, and package Swift libraries and executables.
9. Debugging: Swift uses the LLDB debugger, which provides you with a REPL and debugger to
enable integrated debugging, consistent formatting, failure recovery, and expression evaluation.
10. Modern Syntax: Swift has a modern syntax that is easy to learn and use.
12. Open Source: Swift is an open-source language, which means its source code is freely
available on GitHub.