Coding Interview Questions: Various

Michael Fares
4 min readJul 18, 2021

--

If a user attempts to create a resource that already exists — for example, an email address that’s already registered — what HTTP status code would you return?

In my view, the best status code to return in this case would be a code of 400, which indicates a “bad request” to the server on the part of the client. MDN’s definition of a “bad request” maps nicely onto the case of trying to create a resource that already exists. MDN states: “an (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).”

Consider a responsive site design that requires a full-width image in all responsive states. What would be the correct way to code this to ensure the page loads the smallest image required to fill the space?

The correct way to code this would be using CSS media queries in order to query the width range the user’s device falls into generally (mobile, tablet, laptop or large monitor). In the website’s assets, include copies of the image sized differently for different device screen widths, and serve the appropriate image to the client based on the results of the CSS media query.

When should you npm and when should you yarn?

Npm installs packages sequentially one after the other, while yarn on the other hand, has the ability to install multiple packages at one time, which means much faster workflow. Yarn also has a more robust security feature in that it checks to make sure there are no malicious scripts or files unintentionally being downloaded into your project. Npm has also significantly improved security in recent updates, however in the early days the installation process was less secure than yarn. Another difference perhaps relevant to security is that yarn uses a checksum algorithm to verify the integrity of the packages installed, while npm uses a secure hash algorithm to do the same. These are just a few key differences. Overall however, with both dependency management systems being widely used and supported, generally npm and yarn are “interchangeable” and which one to adopt comes down to preference of an individual or team. More information in this great article.

How can you make sure your dependencies are safe?

  1. The first line of security should be to know what you are installing, and that it is up to date with documentation and community adoption: Check who published the package, how many versions there are, and the number of downloads weekly. If either of the later two numbers are low, this is a signal that the package might be malware and you should inspect the code or think twice.
  2. Run commands like npm audit or yarn audit which check for known security issues with dependencies, and provide a ranked list of vulnerabilities.
  3. Make sure your packages stay up to date: running npm outdated or yarn outdated will provide a list of outdated packages needing updates.
  4. Leverage Github’s feature of providing security alerts for outdated/vulnerable packages, available for several development stacks including npm and yarn.
  5. Again: know your documentation well for package managers and dependencies being used, and keep informed as to the latest updates and security discussions in the community.

What are the differences between CHAR and VARCHAR data types in MySQL?

In my MySQL, a CHAR is a string of a specified fixed length. CHAR is used for data fields whose entries are expected to be equal to or under the fixed string length. For entries whose length is less that the specified length for the CHAR, the each remaining character space is represented as a whitespace, in other words padded with whitespace. A use case example would be if customers can choose from 4 different colors: green, blue, black, or yellow, for an item they are purchasing. The longest color choice is yellow (6 characters) so when setting up a table we could indicate a color data field by writing Color CHAR(6)

VARCHAR on the other hand, is a string of variable length where the max possible length is specified. There is no padded whitespace in the data stored. A use case for VARCHAR is the name field in a database, where a persons name can be anywhere between one and an upper bound of characters, with 50 being a generally good number. Hence when setting up a table we could indicate a name data field by writing Name VARCHAR(50)

How else can the JavaScript code below be written using Node.Js to produce the same output?

console.log("first");     
setTimeout(function() {
console.log("second");
}, 0);
console.log("third");
// Output:
// first
// third
// second

Replacing the setTimeout() function with setImmediate() will produce the same output. As in:

console.log("first");setImmediate(function() {console.log("second");}, 0);console.log("third");// Output:     
// first
// third
// second

--

--

Michael Fares
Michael Fares

Written by Michael Fares

I am a Web Developer, Educator, Lifelong autodidact, 3rd Culture Kid, and Citizen of the World.

No responses yet