General Flashcards
Encapsulation
Encapsulation is the mechanism of binding the data together and hiding it from the outside world. Encapsulation is achieved when each object keeps its state private so that other objects don’t have direct access to its state. Instead, they can access this state only through a set of public functions
Abstraction
Abstraction can be thought of as the natural extension of encapsulation. It means hiding all but the relevant data about an object in order to reduce the complexity of the system. Abstraction helps by hiding internal implementation details of objects and only revealing operations that are relevant to other objects
Polymorphism
Polymorphism is the ability of an object to take different forms and thus, depending upon the context, to respond to the same message in different ways. Take the example of a chess game; a chess piece can take many forms, like bishop, castle, or knight and all these pieces will respond differently to the ‘move’ message.
How do i write clean code?
- The logic should be straightforward to make it hard for bugs to hide
- The dependencies minimal to ease maintenance
- Performance close to optimal so people aren’t tempted to turn it into a mess
- Clean code does one thing well
- Bad code tries to do too much. Clean code is focused
- Clean code is simple, orderly, and can be read easily
- No duplication
The boy scout rule
- Leave the campground cleaner than you found it
- Cleaning things up means it’s much more difficult for the code to rot
Why does good code rot?
- The requirements changed in ways that thwart the original design
- Schedules were too tight to do things right
- Stupid managers and intolerant customers
- At the end of the day, most falls on the programmer. It is unprofessional to bend to the will of managers who don’t understand the risks of making messes
- Would a doctor listen to a patient to stop washing their hands? No, the patient doesn’t know about the risks of diseases and infection
serialization (encoding)
- The translation from the in-memory representation to a byte sequence
- Python object to JSON string
- JSON and XML are standardized serialization/encodings that can be read by many programming languages. They are textual formats
- Big downside is that speed can be quite slow
deserialization (decoding)
- JSON string to python object
Binary serialization (encoding)
- Both JSON and XML use a lot of space compared to binary formats
- As such binary encoding can be much faster but comes at the expense of not being human readable. Though probably OK for internal data
- Examples of binary encodings for JSON: MessagePack, BSON, BJSON, Apache Thrift and Protocol Buffers (protobuf), and Apache Avro
Load Balancers
- Different load balancing methods (algorithms)
- Round robin
- Requests to the application servers are distributed in a round-robin fashion
- Each server is assigned an equal portion of the traffic and in circular order
- Least connected
- next request is assigned to the server with the least number of active connections
- IP-hash
- A hash function is used to determine what server should be selected for the next request based on the client’s IP address’s hash value
- Maintains session persistence
- Round robin
- Overall goal is to distribute incoming requests to ensure high availability, reliability, and performance by avoiding overloading a single server
- Session persistence. Ensure subsequent requests from the same client are directed to the same backend server
- They also support SSL/TLS termination which offloads this burden from the backend servers
- Improve perf: reduces load on any individual server
- Ensure high availability: eliminates single point of failure servers
- Scalability: easily scale infra as demand increases
- Stateful load balancing
- Source IP affinity: Assigns a client to a specific server based on the clients IP address
- Session affinity: Allocates a client to a specific server based on a session identifier such as a cookie or URL parameter
API Gateway
- Design for routing. Receive requests from clients and route them to the appropriate micro service. Clients can access a variety of services through a single entrypoint
- Rate limiting
- Caching
- Authentication and Authorization
- Load balancing
- Transformation. For example convert from XML to JSON
- Can incorporate a Web Application Firewall (WAF)
Linear and non linear data structures
Linear
Direct access: array, matrix
Sequential access: LL, stack, queue
Non linear
Hierarchy: tree, trie, heap
Unorder: Hash table, set, graph
Arrays
Search unordered: O(n) linear
Search ordered: O(log n) can use binary search
Matrix
- The primary array holds all the ROWS while each element in the subarray is a column value
- Multiple list comprehension
[<return> <outer> <inner> <inner> ... <option>]</option></inner></inner></outer></return>
TCP vs UDP
TCP
- Ensures reliable, ordered, and error-checked delivery of bytes between apps
- Retransmits lost or corrupted packets
- Establishes a connection/handshake before sending
- Used when data accuracy is more critical than speed
UDP
- Connectionless
- Sends messages called datagrams without establishing a connection
- Does not guarantee reliability or order
- Low overhead and fast