Fullstack Flashcards

(30 cards)

1
Q

What is the difference between block and inline-block in CSS?

A

block elements (e.g., <div>) take full width and start on a new line; inline-block allows inline flow with block properties like width/height. Example: display: inline-block;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you center a div both horizontally and vertically using CSS?

A

```css
div {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* Explanation: Flexbox aligns content in both axes (O(1) layout). */
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is event delegation in JavaScript?

A

Event delegation uses event bubbling to handle events on a parent element for its children. Example: Add click listener to <ul> to handle <li> clicks (reduces listeners).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you debounce a function in JavaScript for frontend performance?

A

```javascript
function debounce(func, wait) {
let timeout;
return (…args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(…args), wait);
};
}
// Example: debounce(() => search(), 300) limits rapid calls (O(1)).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a React component, and how do you create one?

A

A React component is a reusable UI piece returning JSX. Example: function MyComponent() { return <div>Hello</div>; } or class MyComponent extends React.Component { render() { return <div>Hello</div>; } }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the useEffect hook in React.

A

useEffect handles side effects (e.g., API calls) after render. Example: useEffect(() => { fetchData(); }, [id]); runs when id changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you handle forms in React with multiple inputs?

A

```javascript
function Form() {
const [form, setForm] = useState({ name: ‘’, email: ‘’ });
const handleChange = (e) => setForm({ …form, [e.target.name]: e.target.value });
return (
<form>
<input name=’name’ value={form.name} onChange={handleChange} />
<input name=’email’ value={form.email} onChange={handleChange} />
</form>
);
}
// Explanation: Uses a single state object for multiple inputs (O(1) updates).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is React.memo, and when is it used?

A

React.memo memoizes a component to prevent re-renders unless props change. Example: const Memoized = React.memo(MyComponent);, used for performance optimization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you implement lazy loading in React?

A

```javascript
const LazyComponent = React.lazy(() => import(‘./Component’));
function App() {
return (
<Suspense fallback={<div>Loading…</div>}>
<LazyComponent></LazyComponent>
</Suspense>
);
}
// Explanation: React.lazy and Suspense defer component loading (O(1) setup).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of ARIA attributes in HTML?

A

ARIA attributes enhance accessibility. Example: role='button' aria-label='Close' describes a custom button for screen readers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a REST API, and what are its key principles?

A

A REST API uses HTTP methods (GET, POST, PUT, DELETE) for stateless, resource-based communication. Principles: statelessness, client-server, uniform interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Node.js handle asynchronous operations?

A

Node.js uses an event loop for non-blocking I/O, processing callbacks, Promises, or async/await. Example: fs.promises.readFile('file.txt') reads files asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a Node.js Express route to handle a GET request.

A

```javascript
const express = require(‘express’);
const app = express();
app.get(‘/users/:id’, (req, res) => {
res.json({ id: req.params.id, name: ‘User’ });
});
// Example: GET /users/1 → { id: ‘1’, name: ‘User’ }
// Explanation: Returns JSON for a user by ID (O(1)).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is JWT, and how is it used in a full-stack app?

A

JWT (JSON Web Token) is a token for secure authentication. Frontend sends Authorization: Bearer <token>; backend verifies it. Example: jwt.verify(token, 'secret').

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you implement JWT authentication in Node.js?

A

```javascript
const jwt = require(‘jsonwebtoken’);
function authenticateToken(req, res, next) {
const token = req.headers[‘authorization’]?.split(‘ ‘)[1];
if (!token) return res.status(401).send(‘Unauthorized’);
jwt.verify(token, ‘secret’, (err, user) => {
if (err) return res.status(403).send(‘Forbidden’);
req.user = user;
next();
});
}
// Explanation: Verifies JWT, attaches user to request (O(1)).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between SQL and NoSQL databases?

A

SQL databases (e.g., MySQL) are relational, use tables, and support SQL; NoSQL databases (e.g., MongoDB) are non-relational, flexible for unstructured data.

17
Q

Write a SQL query to join two tables for employee and department data.

A

```sql
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
– Explanation: Matches employees to departments via department_id (O(n)).
~~~

18
Q

How do you optimize a slow SQL query?

A

Use EXPLAIN to analyze, add indexes on filtered/joined columns, reduce selected columns, and replace subqueries with joins.

19
Q

What is caching, and how is it used in a full-stack app?

A

Caching stores data (e.g., in Redis) to reduce database load. Example: Cache API responses in backend, serve from cache in frontend to improve speed (O(1) lookup).

20
Q

How does the fs module in Node.js handle file operations?

A

```javascript
const fs = require(‘fs’).promises;
async function readFile() {
const data = await fs.readFile(‘file.txt’, ‘utf8’);
return data;
}
// Explanation: Reads file asynchronously, returning content (O(n)).
~~~

21
Q

What is CORS, and how do you handle it in a full-stack app?

A

CORS controls cross-origin requests. In Express: app.use(cors({ origin: 'http://frontend.com' })); allows frontend domain to access backend API.

22
Q

How do you handle errors in a Node.js backend?

A

```javascript
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: ‘Internal Server Error’ });
});
// Explanation: Global error middleware catches unhandled errors, returns 500 response.
~~~

23
Q

What is the difference between localStorage and sessionStorage in the browser?

A

localStorage persists until cleared; sessionStorage lasts until tab closes. Example: localStorage.setItem('key', 'value'); stores persistently.

24
Q

How do you fetch data from an API in React?

A

```javascript
function DataComponent() {
const [data, setData] = useState(null);
useEffect(() => {
fetch(‘/api/data’)
.then(res => res.json())
.then(setData);
}, []);
return <div>{data ? data.name : ‘Loading…’}</div>;
}
// Explanation: Fetches data on mount, updates state (O(1) render).
~~~

25
What is a service worker, and how does it enhance a frontend?
A service worker is a script for caching and offline support. Example: `navigator.serviceWorker.register('/sw.js');` caches assets for offline access.
26
How do you implement a rate limiter in a Node.js backend?
```javascript const rateLimit = (limit, windowMs) => { const requests = new Map(); return (req, res, next) => { const key = req.ip; const now = Date.now(); if (!requests.has(key)) requests.set(key, []); requests.set(key, requests.get(key).filter(t => now - t < windowMs)); if (requests.get(key).length >= limit) return res.status(429).send('Too Many Requests'); requests.get(key).push(now); next(); }; } // Explanation: Limits requests per IP in a time window (O(1)). ```
27
What is the CAP theorem, and how does it apply to full-stack?
CAP theorem states a distributed system can only guarantee two of: Consistency, Availability, Partition Tolerance. Full-stack apps choose based on needs (e.g., NoSQL prioritizes availability).
28
How do you secure a full-stack application?
Use HTTPS, JWT for authentication, validate/sanitize inputs, implement rate limiting, and use CORS. Example: Sanitize form inputs to prevent SQL injection.
29
What is the purpose of middleware in Express?
Middleware processes requests in Express, handling tasks like logging or authentication. Example: `app.use((req, res, next) => { console.log(req.url); next(); });`.
30
How do you handle cross-browser compatibility in the frontend?
Use feature detection, polyfills, and tools like Autoprefixer. Example: Add `-webkit-` prefixes for CSS `transform` to support older browsers.