Basics | 青训营笔记

128 阅读9分钟

1/ Difference between linear and non linear data structures

S.NOLinear Data StructureNon-linear Data Structure
1.In a linear data structure, data elements are arranged in a linear order where each and every element is attached to its previous and next adjacent.In a non-linear data structure, data elements are attached in hierarchically manner.
2.In linear data structure, single level is involved.Whereas in non-linear data structure, multiple levels are involved.
3.Its implementation is easy in comparison to non-linear data structure.While its implementation is complex in comparison to linear data structure.
4.In linear data structure, data elements can be traversed in a single run only.While in non-linear data structure, data elements can’t be traversed in a single run only.
5.In a linear data structure, memory is not utilized in an efficient way.While in a non-linear data structure, memory is utilized in an efficient way.
6.Its examples are: array, stack, queue, linked list, etc.While its examples are: trees and graphs.
7.Applications of linear data structures are mainly in application software development.Applications of non-linear data structures are in Artificial Intelligence and image processing.
8.Linear data structures are useful for simple data storage and manipulation.Non-linear data structures are useful for representing complex relationships and data hierarchies, such as in social networks, file systems, or computer networks.
9.Performance is usually good for simple operations like adding or removing at the ends, but slower for operations like searching or removing elements in the middle.Performance can vary depending on the structure and the operation, but can be optimized for specific operations.

HTTP

Append: multithreading

Random-access memory(RAM)

multi-threading enables us to do parallel computing efficiently let's start our explanation of multi-threading by first discussing how a process works. When you start a program it is first loaded and given an address space in main memory from your hard disk after being loaded in memory it is then scheduled onto an available CPU when a process is scheduled on to a CPU the process instructions are executed sequentially

Execution Context

it will keep track of the current state of

a running process which includes data

like what instruction the CPU is

currently executing(PC) if you have multiple

CPUs and even in the case of a single

CPU you could have multiple execution

contexts within a single process we

refer to these execution contexts in an

abstract data structure called a thread

Threads

a process can have one or more threads

these threads can execute simultaneously

on multiprocessor and multi-core systems

and execute different sections of the

program

Address Space

in contrast to a thread a process is

allocated in address space in the case

of a thread a thread shares its parent

processes address space along with any

other threads that the parent process

has created in this way threads are

considered a lightweight option to a

process because they will almost always

consume less memory than a new process

would

in this diagram we see that process two

has several threads there is also a

variable X that process to created all

three of the threads and process to have

the ability to read and write to the

memory location where variable X is

located if variable X were to be

incremented every time we completed a

for loop iteration within process two

there's a possibility that each of these

threads may increment the variable as

Variable X

the programmer we have no control over

which thread accesses variable X and

when it accesses variable X the reason

for this is that the operating system

scheduler schedules processes and

threads according to its scheduling

algorithm whatever scheduling algorithm

is on our operating system that's what

we have to use this can cause really

nasty issues in multi-threaded programs

Synchronization

in order to implement multi-threaded

programs we almost certainly have to use

what's known as a synchronization

construct there are many but some of the

most common are mutexes and semaphores

these mechanisms enforce synchronization

between threads so if thread 1 in

process 2 is currently updating variable

X with a new value no other threads will

be allowed to update variable X while

thread 1 is updating it I said earlier

that multi-threading allows us to

perform parallel computing efficiently

we could very well have instead of

threads multiple processes running on

multiple CPUs in doing so we can avoid

the problem of synchronization between

threads and a multi-threaded process

because each process has its own address

space that other processes are not

allowed to write to and the operating

system actively prevents them from doing

this however address spaces can be very

large and we may only need to run a

portion of the program rather than all

of it each time we run it if we were to

create many processes to perform

parallel computing we would quickly use

up the memory on most any machine

this is why threads are useful because

Benefits

they utilize the address space of the

parent process making better use of the

system's memory in consuming less space

the benefits of multi-threading still

outweigh the disadvantage of having to

implement synchronization between

threads

one of the most common examples of

Examples

multi-threading is in the case of a web

server if we have a web server that is

serving a static website in other words

some HTML files when the web server

receives an HTTP GET requests it will

provide those files to the requesting

client the web server like any program

will execute its instructions

sequentially when it receives a request

however the web server may receive

multiple requests for those HTML files

at the same time if it does those

requests are queued and have to wait for

the server to complete requests that it

has already received thankfully most web

servers today are multi-threaded and the

server will either create a pool of

threads or dynamically create new

threads when an HTTP GET request is

received that's why when you visit sites

like Facebook and Amazon you typically

receive a response almost immediately

otherwise if those servers weren't

multi-threaded you would literally have

to wait in a queue until the server

handled everyone's requests who visited

the site before you

Outro

I hope this explanation of

multi-threading helped give you a better

understanding of the concept and then it

made you eager to learn more if so

please throw a like on the video and let

me know in the comments if I've missed

anything or if there is any particular

topic you would like me to cover thanks

for watching


2、Disk Scheduling Algorithms-I was asked to explain the difference between the algorithms

  • Seek Time: Seek time is the time taken to locate the disk arm to a specified track where the data is to be read or write. So the disk scheduling algorithm that gives minimum average seek time is better.

A:

FCFS(First come first served): FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed in the order they arrive in the disk queue.

SSTF: In SSTF (Shortest Seek Time First), requests having shortest seek time are executed first. So, the seek time of every request is calculated in advance in the queue and then they are scheduled according to their calculated seek time. As a result, the request near the disk arm will get executed first. SSTF is certainly an improvement over FCFS as it decreases the average response time and increases the throughput of system.Let us understand this with the help of an example.

C(circular)SCAN: In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its direction. So, it may be possible that too many requests are waiting at the other end or there may be zero or few requests pending at the scanned area.

LOOK: It is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm in spite of going to the end of the disk goes only to the last request to be serviced in front of the head and then reverses its direction from there only. Thus it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.

CLOOK: As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to CSCAN disk scheduling algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last request to be serviced in front of the head and then from there goes to the other end’s last request. Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.


3、LRU Page replacement and the different scenarios of using different page replacement algorithms.

Page Fault: A page fault happens when a running program accesses a memory page that is mapped into the virtual address space but not loaded in physical memory. Since actual physical memory is much smaller than virtual memory, page faults happen. In case of a page fault, Operating System might have to replace one of the existing pages with the newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce the number of page faults.

(miss/hit)

First In First Out (FIFO): This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be replaced page in the front of the queue is selected for removal.

The Clock algorithm( Second Chance) algorithm are modifications of the FIFO algorithm that by keeping track of recently used pages.

The Clock Algorithm is based on the idea of using a circular buffer or linked list to keep track of the pages in memory. Each page is associated with a reference bit that is set to 1 whenever the page is accessed. When a page fault occurs, the operating system looks for a page with a reference bit of 0. If it finds one, that page is replaced. If all the pages have a reference bit of 1, the algorithm clears the reference bits and starts again.

[Belady’s anomaly]hat it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10-page faults.

Optimal Page replacement: In this algorithm, pages are replaced which would not be used for the longest duration of time in the future. Optimal page replacement is perfect, but not possible in practice as the operating system cannot know future requests.

Least Recently Used: In this algorithm, page will be replaced which is least recently used.

Most Recently Used (MRU): In this algorithm, page will be replaced which has been used recently. Belady’s anomaly can occur in this algorithm.

4、What is polymorphism(多态)

OOP concepts include abstraction, encapsulation, inheritance and polymorphism

It is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types

// Polymorphism at work #1: a Rectangle, Triangle and Circle
// can all be used wherever a Shape is expected. No cast is
// required because an implicit conversion exists from a derived
// class to its base class.
var shapes = new List<Shape>
{
    new Rectangle(),
    new Triangle(),
    new Circle()
};
​
// Polymorphism at work #2: the virtual method Draw is
// invoked on each of the derived classes, not the base class.
foreach (var shape in shapes)
{
    shape.Draw();
}
/* Output:
    Drawing a rectangle
    Performing base class drawing tasks
    Drawing a triangle
    Performing base class drawing tasks
    Drawing a circle
    Performing base class drawing tasks
*/

5、difference between override and overload

overloading: same method name but different parameters in the same class【compile time polymophism】

overriding: same method signature but (different method body) in both superclass and child class【runtime polymophism】

6、internal implementation of a hashmap

HashMap contains an array of Node and Node can represent a class having the following objects :

  1. int hash
  2. K key
  3. V value
  4. Node next

first step is hashing, it's a process to convert an object into an integer form.

7、How to count the number of line in a file in a linux terminal

# wc -l [filename]
$ grep -c ".*" [filename]

8、What does a network infrastructure consist of?

  • Hardware infrastructure typically includes routers, switches, hubs, repeaters, gateways, bridges, and modems.
  • Software infrastructure includes monitoring and management tools and operating systems.
  • Network services include networking protocols such as TCP, UDP, and IP addressing.

9、pass by ref versus pass by value

Pass by value means that a copy of the argument is passed to the function, so any changes made to the argument within the function do not affect the original value. This is the default behavior in most programming languages.

On the other hand, pass by reference means that the actual memory address of the argument is passed to the function, so any changes made to the argument within the function affect the original value.

The pros of pass by value are that it is simpler and easier to understand. It also prevents unintended changes to the original value. The cons of pass by value are that it can be less efficient, especially when working with large objects, as a copy of the object needs to be made.

The pros of pass by reference are that it is more efficient, especially when working with large objects, as no copy of the object needs to be made. It also allows for direct modification of the original object. The cons of pass by reference are that it can be more complex and harder to understand, and it can potentially lead to unintended changes to the original object.

The most common usage depends on the programming language and the specific use case. In general, pass by value is the default behavior in most programming languages, while pass by reference is often used with mutable objects in languages that support it, such as C++ and Python.

To clarify, while Java does pass arguments by value, when an object reference is passed as an argument, the value of the reference (which is essentially a memory address) is copied, but both the original reference and the copied reference still refer to the same object in memory. This means that changes made to the object within the called method will be reflected in the original object as well

11、CO

When building a CPU, there are several ways to make it work more efficiently:

  1. Improve the architecture: The architecture of a CPU has a significant impact on its performance. By designing a CPU with a more efficient architecture, you can improve its performance. For example, you can use a pipelined architecture, which allows multiple instructions to be executed simultaneously, or a superscalar architecture, which allows multiple instructions to be issued and executed in a single clock cycle.
  2. Increase clock speed: The clock speed of a CPU determines how many instructions it can execute per second. By increasing the clock speed, you can increase the CPU's performance. However, increasing the clock speed also increases power consumption and heat generation, so there are limits to how much you can increase it.
  3. Optimize the instruction set: The instruction set of a CPU determines what operations it can perform. By optimizing the instruction set, you can reduce the number of instructions required to perform a given task, which can improve performance. For example, you can add specialized instructions for commonly used operations.
  4. Use caching: Caches are small, fast memory units that store frequently accessed data. By using caching, you can reduce the amount of time the CPU spends waiting for data to be loaded from main memory, which can improve performance.
  5. Use parallelism: Parallelism allows multiple instructions to be executed simultaneously. By designing the CPU to take advantage of parallelism, you can improve its performance. For example, you can use multiple cores or threads to execute instructions in parallel.

12、What are you learning that will help you in your work with us?