Facebook Pixel

Feed API Design Interview Questions and Answers

Common feed API and pagination interview questions with concise answers.

Feed API Design Interview Questions and Answers

Interviewers ask these feed API and pagination questions often. Here are concise answers.

Q1: How do you build a feed API?

GET /feed?skip=&limit=. Exclude the current user and already-swiped users. Build a filter from whitelisted query params. Use .lean() and .select(). Return { data, total, skip, limit }. Run find and countDocuments in parallel with Promise.all. Index the filter and sort fields.

Q2: Offset or cursor pagination?

Offset for small collections or when the client needs page numbers. Cursor for large collections or infinite scroll (faster, stable). Both need an index on the sort or cursor field.

Q3: Why is skip slow for large offsets?

MongoDB scans the skipped documents before returning the ones you want. For offset 100000, it scans 100000 docs. Use cursor-based pagination: find documents where createdAt is less than the last cursor. MongoDB uses an index.

Q4: How do you exclude already-seen items in a feed?

Fetch the seen IDs with a distinct query. Filter the feed with _id: { $nin: seenIds }. For a dating app, exclude users the current user has already swiped on.

Q5: How do you handle ties in cursor pagination?

Add a tiebreaker. .sort({ createdAt: -1, _id: 1 }). Without a tiebreaker, two items with the same createdAt can cause cursor pagination to skip or duplicate items.

Q6: How do you know if there is more data in cursor pagination?

Fetch limit + 1 items. If you get limit + 1, there is more (pop the extra). If you get limit or fewer, there is no more. Avoids a separate count query.

Q7: Why cap the limit?

To prevent abuse. Without a cap, a client can request 10000 items, eating memory and bandwidth. Use Math.min(parseInt(req.query.limit) || 20, 100).

Q8: How do you filter and sort in a feed API?

Build a filter from whitelisted query params. Build a sort from whitelisted sort fields. Combine with pagination: User.find(filter).sort(sort).skip(skip).limit(limit).select(select).lean().

Q9: How do you index for a feed API?

Index the fields you filter and sort by. Use compound indexes for multi-field queries. For a feed filtered by gender and sorted by createdAt, use schema.index({ gender: 1, createdAt: -1 }). Verify with explain().

Q10: How do you test a feed API?

Use Jest and supertest. Test pagination (skip, limit, nextCursor). Test filtering (by gender, age range). Test sorting (asc, desc). Test empty results. Test that the current user is excluded. Test that already-swiped users are excluded.

The Takeaway

Feed API interview answers: GET /feed with skip and limit; exclude current and already-seen; offset vs cursor (cursor for large); skip is slow (use cursor); exclude seen with $nin; tiebreaker for ties; limit + 1 for hasMore; cap the limit; whitelist filter and sort fields; index for queries; test with supertest.

GET /feed?skip=&limit=. Exclude the current user and already-swiped users. Build a filter from whitelisted query params. Use .lean() and .select(). Return { data, total, skip, limit }. Run find and countDocuments in parallel. Index the filter and sort fields.

Offset for small collections or when the client needs page numbers. Cursor for large collections or infinite scroll (faster, stable). Both need an index on the sort or cursor field.

Fetch the seen IDs with a distinct query. Filter the feed with _id: { $nin: seenIds }. For a dating app, exclude users the current user has already swiped on.

Add a tiebreaker. .sort({ createdAt: -1, _id: 1 }). Without a tiebreaker, two items with the same createdAt can cause cursor pagination to skip or duplicate items.

Fetch limit + 1 items. If you get limit + 1, there is more (pop the extra). If you get limit or fewer, there is no more. Avoids a separate count query.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.