Interview Question — Create an API endpoint that filters a list of notes and returns all those containing the word “fake.” The notes are text files in a directory.

Tim Macaulay-Robinson
3 min readJan 6, 2023

Writing an API endpoint to filter a list of notes and return all the ones that contain the keyword “fake” is a common task that can be accomplished using a variety of programming languages and frameworks. In this article, we’ll walk through implementing a solution in Node.js.

To begin, let’s first consider how we should approach this problem. One approach could be to read the list of notes from a directory, filter the list to include only the notes that contain the keyword “fake”, and then return the filtered list to the client. This approach is simple and efficient, as it avoids the need to connect to a database and stream the notes.

With this approach in mind, let’s look at how we can implement a solution using Node.js.

To begin, we’ll need to install the necessary dependencies. We’ll use the Express framework to create the API endpoint and the fs module to read the list of files in the directory.

npm install express

Next, let’s create a simple Express server and define an API endpoint at the path /api/notes/:keyword

const express = require('express');
const fs = require('fs');
const app = express();

app.get('/api/notes/:keyword', (req, res) => {
// code to filter and return notes goes here
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));

Now, let’s read the list of files in the directory and filter the list to include only the ones that contain the keyword. We’ll use the fs.readdirmethod to read the list of files and the Array.prototype.filter method to filter the list.

fs.readdir('./notes', (err, files) => {
if (err) {
return res.status(500).send({ message: err.message });
}
const filteredFiles = files.filter(file => file.includes(keyword));
// code to return filtered list goes here
});

Now that we have a list of the filtered files, we can read the contents of each file and return the results to the client.

const results = {};
filteredFiles.forEach(file => {
const content = fs.readFileSync(`./notes/${file}`, 'utf-8');
results[file] = content;
});
res.send({ results });

Putting it all together, here’s the complete code for the API endpoint:

const express = require('express');
const fs = require('fs');
const app = express();

app.get('/api/notes/:keyword', (req, res) => {
fs.readdir('./notes', (err, files) => {
if (err) {
return res.status(500).send({ message: err.message });
}
const filteredFiles = files.filter(file => file.includes(keyword));
const results = {};
filteredFiles.forEach(file => {
const content = fs.readFileSync(`./notes/${file}`, 'utf-8');
results[file] = content;
});
res.send({ results });
});
});

Ending thoughts

During the interview, breaking down the problem step by step is always good.

  • Creating an API Endpoint
  • Reading a Directory of files
  • Grab the files content, filter for the keyword
  • Return the results

Other questions you might ask:

  • What type of files are the note files?
  • What format are they in?
  • Why a directory of files and not a database? This is 2023. 😉
  • Do we have to follow a schema of what the return looks like?
  • Should this have pagination?
  • Can we write a test that confirms this works?

All of these show you understand the problem, but you are also looking for solutions to future problems.

--

--

No responses yet