0% found this document useful (0 votes)
23 views

backend question

Uploaded by

rv790562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

backend question

Uploaded by

rv790562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Here are the brief answers to all the questions you listed:

General Backend Questions


1. What is REST, and how does it differ from GraphQL?
REST is an architectural style for designing APIs using resources and HTTP
methods. GraphQL is a query language that allows clients to request
specific data, reducing over-fetching and under-fetching.
2. Explain the difference between HTTP methods like GET, POST, PUT,
PATCH, and DELETE.
o GET: Retrieve data.
o POST: Create data.
o PUT: Update or replace data.
o PATCH: Update part of data.
o DELETE: Remove data.
3. What are middleware functions in Express.js? Provide an example.
Middleware functions are functions executed before or after a route
handler, like authentication or logging.
app.use((req, res, next) => { console.log('Request received'); next(); });
4. How do you ensure security in a MERN stack application?
o Use HTTPS.
o Sanitize inputs.
o Hash passwords.
o Use environment variables.
o Implement CORS correctly.
5. What is the difference between synchronous and asynchronous
programming?
o Synchronous: Tasks are executed sequentially.
o Asynchronous: Tasks can be executed without waiting for the
previous task to finish (using callbacks, promises, or async/await).
Node.js & Express.js
1. How does the event loop work in Node.js?
The event loop handles asynchronous tasks by delegating them to the
system and processing the results once ready, ensuring non-blocking I/O
operations.
2. What are streams in Node.js, and how are they used?
Streams are used for handling continuous data (e.g., reading files).
Types: Readable, Writable, Duplex, and Transform.
3. fs.createReadStream('file.txt').pipe(process.stdout);
4. What is req and res in Express?
o req: Represents the HTTP request.
o res: Represents the HTTP response.
5. How do you handle file uploads in an Express app?
Use libraries like multer to handle file uploads.
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => { res.send('File
uploaded'); });
6. Explain CORS and how to handle it in Express.
CORS (Cross-Origin Resource Sharing) allows resources to be accessed
from another domain. Use the cors package to handle it.
const cors = require('cors');
app.use(cors());

Database (MongoDB)
1. What is the difference between SQL and NoSQL databases?
o SQL: Structured, uses tables.
o NoSQL: Flexible schema, uses collections and documents.
2. What are MongoDB collections and documents?
o Collection: A group of documents (like a table).
o Document: A JSON-like object (like a row).
3. Explain find(), findOne(), and aggregate().
o find(): Retrieves multiple documents.
o findOne(): Retrieves a single document.
o aggregate(): Performs complex queries like joins or grouping.
4. How do you implement relationships in MongoDB?
o Embedding: Storing related data within the same document.
o Referencing: Storing references to related documents.
5. What are indexes in MongoDB?
Indexes improve query performance by creating a data structure to
quickly locate data.
db.collection.createIndex({ field: 1 });

Authentication & Authorization


1. What is the difference between authentication and authorization?
o Authentication: Verifying identity.
o Authorization: Verifying access permissions.
2. How do you implement user authentication in a MERN stack app?
Use JWTs or sessions to authenticate users.
3. What are JSON Web Tokens (JWT), and how do they work?
JWTs are tokens containing encoded user data, used for stateless
authentication.
4. How would you handle token expiration in JWT-based authentication?
Refresh tokens or force re-login when the token expires.
5. How do you securely store passwords in a database?
Use hashing algorithms like bcrypt.
API Design
1. How do you design a RESTful API for a CRUD application?
Use routes:
o GET /items
o POST /items
o PUT /items/:id
o DELETE /items/:id
2. What is versioning in REST APIs, and how do you implement it?
Versioning allows updates without breaking existing APIs, e.g.,
/api/v1/resource.
3. How do you handle error responses in an API?
Use standardized error codes and messages.
{ "error": "Resource not found", "status": 404 }
4. What are the best practices for scalable APIs?
o Use caching.
o Limit payloads.
o Implement pagination.
5. How do you implement pagination in a REST API?
Use query params like ?page=1&limit=10.

Performance Optimization
1. How do you optimize queries in MongoDB?
Use indexes, avoid unnecessary data fetching, and use aggregation.
2. Explain caching in a web application.
Store frequently accessed data temporarily (e.g., using Redis).
3. What are best practices for optimizing Express apps?
o Use gzip compression.
o Optimize middleware.
o Use load balancing.
4. How do you use tools like pm2?
pm2 manages Node.js processes for reliability.
5. pm2 start app.js
6. What is connection pooling in MongoDB?
Reusing connections instead of creating new ones improves
performance.

Real-World Scenarios
1. How would you scale a backend for high traffic?
o Load balancing.
o Horizontal scaling.
o Caching.
2. How do you handle rate limiting?
Use libraries like express-rate-limit.
3. How would you handle large file uploads?
Use streaming and services like S3 for storage.
4. How would you migrate a MongoDB schema?
Use scripts or tools like Mongoose for controlled migration.
5. How do you ensure APIs are fault-tolerant?
Implement retries, fallback mechanisms, and proper error handling.

Debugging & Testing


1. What are debugging techniques for Node.js apps?
Use console.log, debug, or built-in Node.js debugger.
2. How do you handle error logging?
Use logging libraries like winston or morgan.
3. What is unit testing? How do you test an Express route?
Unit testing ensures a single unit of code works as expected, e.g., testing
routes with Jest.
4. How would you write integration tests?
Test multiple modules working together, using libraries like supertest.
5. What tools are used for testing in Node.js?
Jest, Mocha, Chai, and Supertest.

Deployment & DevOps


1. How do you deploy a MERN app to AWS?
Use EC2 for the backend, S3 for the frontend, and MongoDB Atlas for the
database.
2. What is a reverse proxy?
A reverse proxy (e.g., Nginx) forwards client requests to backend servers.
3. How do you configure environment variables?
Use .env files and libraries like dotenv.
4. How do you monitor live applications?
Use tools like New Relic, PM2, or cloud monitoring solutions.
5. What is the role of CI/CD pipelines?
Automates code integration, testing, and deployment.

You might also like