laitimes

Github Developer God teaches you to play with database programming

Github Developer God teaches you to play with database programming

Reporting by XinZhiyuan

EDIT: Time

Everyone has their favorite programming problem in mind. This time, let's follow the big guy to the database world for a walk.

On Github, a user named Arthur O'Dwyer regularly posts about the C++ programming language.

Recently, he shared an interview for software engineering that took place in 2013.

Now, nine years later, he still remembers it vividly.

Dwyer says the problem is a microcosm of most real-world programming.

For example, when you maintain a huge code base, there are always some code paths that you don't fully understand, some styles that don't feel necessary, and a lot of code that is difficult to stand on.

Give you 3 hours, can you make it?

First, let's take a look at what this "programming challenge" looks like.

With the incr and decr commands, memcached can add k to a certain number. However, memcached does not provide other arithmetic operations, especially without multiplying by k.

Now you're going to add a mult command to memcached. After completion, the client that can use telnet to memcached runs the following command:

Memcached get started

In this question, the interviewer asks for programming using a memcached database.

memcached is a high-performance distributed cache server used to centrally cache database query results and reduce the number of database accesses to improve the response speed of dynamic web applications.

In order to successfully build memcached, you may need to brew install libevent and a few other things.

You can assume that all interviewees have been granted access to the Linux device, and it has all the right dependencies.

Let's bypass GitHub Repo and unzip a contemporary source code distribution:

Now that you've built the memcached executable, you can start running:

Communicate with the server via the default memcached port 11211, which can be dialoged using a normal telnet:

If there is no telnet, it can also be replaced with nc-c.

Play memcached

memcached is a key-value store, which means it can be told to remember something like the association between a key and a value.

In memcached, the key refers to the ASCII string and the value refers to the arbitrary byte stream.

For example, in a telnet session, enter:

This is tantamount to telling memcached to remember: the association between the string key fullname and the 10-byte value John Smith.

The other numbers on this line of code are a "flags" value of 0, to be remembered along with the byte stream value, and the expiration timeout is 3600 seconds.

After that, memcached will forget about this association.

In any case, after you enter these two lines, memcached will respond:

Now you can retrieve the fullname value and enter it in the same telnet session:

Memcached will return:

Memcached can override the value associated with fullname, and by issuing another set fullname command, you can ask memcached to modify the value in a specific way.

For example, there are specialized commands for append and prepend.

If you want to attach -Jones to fullname in the client program, you can do this:

If you have multiple clients connecting to the same memcached server, they will all be added to the same key at the same time.

This get/set version may cause some updated information to be lost, and append is guaranteed to find them.

Another dedicated command executed is incr:

memcached responds with an incremented value:

This response is useful because there are many clients. If you issue a separate get age command, you may not see the new value until several other clients have completed their respective updates.

If you're going to use that value as a sequence number, SQL primary key, or something like that, there's a way to see the increased value, which is great.

Of course, memcached also remembers the increased value:

Note that 37 and 38 are still stored and returned as byte strings.

They are decoded as integers by ASCII and then returned as part of an atomization operation.

If you try incr a non-integer value, you will get an error:

Finally, remember any positive value for incr or decr, not just 1.

By the way, when you have finished communicating with memcached and want to terminate the connection, you can type the memcached command quit, or you can use nc-c, Ctrl+D to work as well.

The best interview questions

"This is the best question I've ever had in an engineering interview!" Nine years later, Dwyer still thinks so.

Because, it very clearly divides the candidates into three categories:

The first type is the person who is directly confused after reading the question. However, they may not be able to get to this point in the interview process.

The second type might think, "I know how to do it, multiplication is a repetition of addition, and we already have a ready-made addition subroutine, in the form of incr." So I just need to base itself on that basis, well, add the value of x to itself... Oh yes, the whole process still needs to be yes, and then let's see how this lock should be used..."

As a result, they sink deeper and deeper into various pits. In the end, 3 hours passed and nothing useful was written. Obviously, candidates of this type will not be hired either.

The third type says, "I know how to do it, multiplication is the same as addition, except that addition is done +, multiplication I should do *."

So, they copied and pasted all the "+s" to "*s" and completed it within the specified time.

This type of person is likely to be hired.

Of course, better candidates will notice that it will take some time to polish their code before they can be formally committed.

Dwyer says this challenge is particularly suitable for interviews because there is only one correct answer, which is to change "bool incr" to "int opcode" (or any answer that is synonymous with it). At the same time, the code and statements provided by the question together provide a very clear hint that there are currently two arithmetic instructions, and your job is to extend it to three arithmetic instructions.

By the way, memcached database challenges require proficiency in the C++ language, and if your code base is python and Go, you probably won't use memcached.

Interestingly, the author of the question also saw the post and replied that he had come up with it after a few months of work.

According to the authors, the code for the database is so large and complex that for the very beginning of the year, almost every maintenance seems to be a problem.

Obviously, the skill of "quickly figuring out an unfamiliar block of code" can be said to be quite important

Resources:

https://quuxplusone.github.io/blog/2022/01/06/memcached-interview/

Read on