Skip to content

Commit 479dcc4

Browse files
committed
web socket
1 parent e85c45e commit 479dcc4

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

Javascript/OOP-Prototypal-Inheritence/Inheritence-OOP-Class-vs-Prototypes-Theory.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#### Class Inheritance:
2+
23
Instances inherit from classes (like a blueprint — a description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class inheritance may or may not use the class keyword from ES6.
34

45
#### Prototypal Inheritance:
6+
57
Instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.
68

79
JavaScript objects use prototype-based inheritance. Its design is logically similar (but different in implementation) from class inheritance in strictly Object Oriented Programming languages.
@@ -14,9 +16,10 @@ This is why in many tutorials you will see **String.prototype.split** written in
1416

1517
Technically, just the light knowledge of the **class** and **extends** keywords is enough to write software. Trying to understand prototype is like venturing into the darker corners of language design. And sometimes that can be insightful.
1618

17-
Just know that at the very top of the Prototype hierarchy there is the Object object. That’s why its own prototype points to null. There’s nothing else above it.
19+
Just know that at the very top of the Prototype hierarchy there is the Object object. That’s why its own prototype points to null. There’s nothing else above it.
1820

1921
#### Further reading
22+
2023
- 1. [https://medium.freecodecamp.org/a-guide-to-prototype-based-class-inheritance-in-javascript-84953db26df0](https://medium.freecodecamp.org/a-guide-to-prototype-based-class-inheritance-in-javascript-84953db26df0)
2124

2225
- 2. [https://tylermcginnis.com/javascript-inheritance-and-the-prototype-chain/](https://tylermcginnis.com/javascript-inheritance-and-the-prototype-chain/)

Javascript/call-apply-bind/bind-why-its-needed.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
1+
The bind() method creates a new function that, when called, has its **this** keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
22

33
The bind() function creates a new bound function, which is an exotic function object (a term from ECMAScript 2015) that wraps the original function object. Calling the bound function generally results in the execution of its wrapped function.
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### What is a WebSocket API?
2+
3+
According to MDN, the WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With the WebSocket API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
4+
5+
At the same time, WebSocket does not compromise with the security system of the web. All the WebSocket handshakes can be scrutinized by the browser using embedded developer tools in them.
6+
7+
WebSocket represents a single TCP socket connection, thus eliminating the problem of connection limitation.
8+
9+
### The differences between a WebSocket API and a FETCH API.
10+
11+
- 1. WebSocket is a low-level protocol, based on the concept of socket and port, which are the underlying transport mechanism whereas REST is based on CRUD operation.
12+
- 2. WebSocket require the use of IP address and Port details, which are lower level details for any application whereas RESTful application needs to design operation based on verbs, and HTTP based.
13+
- 3. WebSocket is bi-directional in nature i.e. both way operation from client to server and vice versa is possible whereas REST follows a uni-directional approach.
14+
- 4. WebSocket approach is ideal for real-time scalable application, whereas REST is better suited for the scenario with lots of getting request.
15+
- 5. WebSocket is a stateful protocol whereas REST is based on stateless protocol i.e. client does not need to know about the server and same hold true for the server.
16+
- 6. WebSocket connection can scale vertically on a single server whereas REST, which is HTTP based can scale horizontally.
17+
- 7. WebSocket is ideal for a scenario where high loads are a part of game i.e. real-time scalable chat application whereas REST is better fitted for occasional communication, in a typical GET request scenario to call RESTful APIs.
18+
- 8. WebSocket works better, where client-server communicates over the same TCP connection for the life of web socket connection whereas, for HTTP request, a new TCP connection is initiated.
19+
- 9. WebSocket communication allows client and server to talk independently of each other whereas with the REST based approach, either client is talking to the client or server is talking to the client at any given time.
20+
- 10. WebSocket communication cost is lower whereas REST-based communication is comparatively higher end on the cost.

system-design/design-url-shortner.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ How to generate a unique ID for each URL?
55
[http://blog.gainlo.co/index.php/2016/03/08/system-design-interview-question-create-tinyurl-system/](http://blog.gainlo.co/index.php/2016/03/08/system-design-interview-question-create-tinyurl-system/)
66

77
At first glance, each long URL and the corresponding alias form a key-value pair.
8-
In a certain sense, a URL shortener is a giant hash table, which maps integer values to full URL strings. The integer values are represented in the "shortened" URL by a short sequence of characters, which may encode this integer in something like base-64 notation. An alphabet of 64 characters is used, each character representing 6 bits of an encoded number that is used as the key to the hash table.
8+
In a certain sense, a URL shortener is a giant hash table, which maps integer values to full URL strings. The integer values are represented in the "shortened" URL by a short sequence of characters, which may encode this integer in something like base-64 notation. An alphabet of 64 characters is used, each character representing 6 bits of an encoded number that is used as the key to the hash table.
99

1010
Therefore, the question can be simplified like this – given a URL, how can we find hash function F that maps URL to a short alias:
1111
F(URL) = alias
1212
and satisfies following condition:s
1313

14-
### Each URL can only be mapped to a unique alias
14+
### Each URL can only be mapped to a unique alias
1515

1616
### Each alias can be mapped back to a unique URL easily
17+
1718
The second condition is the core as in the run time, the system should look up by alias and redirect to the corresponding URL quickly.
1819

1920
## Performance VS flexibility
@@ -30,11 +31,10 @@ On the flip side, using incremental IDs will make the mapping less flexible. For
3031

3132
I can hardly not ask about how to evaluate the cost of the system. For insert/query, we’ve already discussed above. So I’ll focus more on storage cost.
3233

33-
Each entry is stored as <ID, URL> where ID is a 7-character string. Assuming max URL length is 2083 characters, then each entry takes 7 * 4 bytes + 2083 * 4 bytes = 8.4 KB. If we store a million URL mappings, we need around 8.4G storage.
34+
Each entry is stored as <ID, URL> where ID is a 7-character string. Assuming max URL length is 2083 characters, then each entry takes 7 _ 4 bytes + 2083 _ 4 bytes = 8.4 KB. If we store a million URL mappings, we need around 8.4G storage.
3435

3536
If we consider the size of database index and we may also store other information like user ID, date each entry is inserted etc., it definitely requires much more storage.
3637

37-
3838
## How would you generate unique IDs at scale (thousands of URL shortening requests coming every second)?
3939

4040
## How would your service handle redirects?
@@ -46,14 +46,15 @@ If we consider the size of database index and we may also store other informatio
4646
## How to track click stats?
4747

4848
## What characters can we allow in our randomly-generated slugs?
49+
4950
[https://www.interviewcake.com/question/java/url-shortener](https://www.interviewcake.com/question/java/url-shortener)
5051

51-
It turns out the answer is "only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL" (RFC 1738). "Reserved characters" with "reserved purposes" are characters like '?', which marks the beginning of a query string, and '#', which marks the beginning of a fragment/anchor. We definitely shouldn't use any of those. If we allowed '?' in the beginning of our slug, the characters after it would be interpreted as part of the query string and not part of the slug!
52+
It turns out the answer is "only alphanumerics, the special characters "\$-\_.+!\*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL" (RFC 1738). "Reserved characters" with "reserved purposes" are characters like '?', which marks the beginning of a query string, and '#', which marks the beginning of a fragment/anchor. We definitely shouldn't use any of those. If we allowed '?' in the beginning of our slug, the characters after it would be interpreted as part of the query string and not part of the slug!
5253

53-
Okay, so it seems like the set of allowed characters is A-Z, a-z, 0-9, and "$-_.+!*'(),". The apostrophe character seems a little iffy, since sometimes URLs are surrounded by single quotes in HTML documents. So let's pull that one.
54+
Okay, so it seems like the set of allowed characters is A-Z, a-z, 0-9, and "\$-\_.+!\*'(),". The apostrophe character seems a little iffy, since sometimes URLs are surrounded by single quotes in HTML documents. So let's pull that one.
5455

5556
## What about uppercase and lowercase?
5657

5758
[https://www.interviewcake.com/question/java/url-shortener](https://www.interviewcake.com/question/java/url-shortener)
5859

59-
Domains aren't case-sensitive (so google.com and Google.com will always go to the same place), but the path portion of a URL is case-sensitive. If I query parker.com/foo and parker.com/Foo, I'm requesting different documents (although, as a site owner, I may choose to return the same document in response to both requests). So yes, lowercase and capital versions of the same letter can be treated as different characters in our slugs.
60+
Domains aren't case-sensitive (so google.com and Google.com will always go to the same place), but the path portion of a URL is case-sensitive. If I query parker.com/foo and parker.com/Foo, I'm requesting different documents (although, as a site owner, I may choose to return the same document in response to both requests). So yes, lowercase and capital versions of the same letter can be treated as different characters in our slugs.

0 commit comments

Comments
 (0)