MERN Stack Interview Questions
MERN stack interviews typically assess a candidate's understanding of React, Node.js, Express, and MongoDB, along with problem-solving and system design skills.
Frontend Questions
Interviewers often ask about React concepts like component lifecycle, hooks, state management, and performance optimization techniques.
Backend Questions
Backend-focused questions cover topics like REST API design, middleware in Express, and how Node.js handles asynchronous operations.
Practical Preparation
Practicing coding challenges, reviewing past projects, and being ready to explain design decisions all help candidates perform well in MERN stack interviews.
React Questions With Model Answers
Why does React need a key on list items?
Keys let React match each item between renders. With stable keys such as database IDs, React can move, add or remove only the changed elements. Using the array index as a key causes bugs when items are reordered or deleted, because state can attach to the wrong row.
What does the dependency array in useEffect do?
It tells React when to re-run the effect. An empty array runs it once after the first render, a list of values runs it whenever one of them changes, and no array runs it after every render. Effects that subscribe to something should return a cleanup function.
Controlled or uncontrolled inputs?
A controlled input stores its value in React state and updates through onChange, which makes validation easy. An uncontrolled input keeps its own value in the DOM and is read with a ref, which is simpler for very basic forms.
Node.js, Express and MongoDB Questions
How does Node.js handle many requests on one thread?
Node runs JavaScript on a single thread but hands slow work such as file and network operations to the system. When that work finishes, its callback is queued and the event loop runs it. This is why CPU-heavy code blocks every user, and why it belongs in worker threads or a separate service.
How does error-handling middleware differ from normal middleware?
Express recognises error middleware by its four parameters. It must be registered after the routes.
app.get('/api/users/:id', async (req, res, next) => {
try {
const user = await User.findById(req.params.id);
if (!user) return res.status(404).json({ message: 'User not found' });
res.json(user);
} catch (err) {
next(err); // pass to the error handler
}
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ message: 'Something went wrong' });
});
Embed or reference in MongoDB?
Embed data that is always read together and stays reasonably small, such as an address inside a user. Reference data that grows without limit or is shared, such as orders belonging to a customer. Interviewers like to hear that the choice follows the read pattern of the application.
Why add an index?
Without an index, MongoDB scans every document to answer a query. An index on fields used in filters and sorts makes reads faster, at the cost of extra storage and slightly slower writes. Use explain() to confirm a query actually uses it.
Full-Stack and Design Questions
- How does JWT login work? The server checks credentials, signs a token and returns it. The client sends it on each request, and middleware verifies the signature before allowing access. Mention secure storage, short expiry and refresh tokens.
- What is CORS? A browser rule that blocks requests to another origin unless the server allows it with the right headers. In Express this is usually configured with the
corspackage, listing only trusted origins. - How would you paginate a large list? Use
limitwith either a skip value or, better for large data, a cursor based on the last seen ID. - Where do secrets live? In environment variables, never in the React bundle, because everything shipped to the browser is public.
Preparation Checklist
- Build one small app end to end: login, CRUD, search and deployment.
- Practise explaining one bug you fixed and why it happened.
- Revise array methods, promises and
async/awaitin plain JavaScript. - Prepare a short system diagram of your project to sketch on a whiteboard.