🚀 AI-Powered Mock Interviews Launching Soon - Join the Waitlist for Early Access

technicalmedium

Design and implement a RESTful API endpoint for a real estate platform that allows users to search for properties based on multiple criteria (e.g., price range, number of bedrooms, property type) and returns paginated results. Detail the API contract, database query optimization, and caching strategies.

technical screen · 10-15 minutes

How to structure your answer

MECE Framework: 1. API Contract Definition: Specify endpoint URL, HTTP method (GET), request parameters (priceMin, priceMax, beds, type, page, pageSize), and response structure (JSON array of properties, pagination metadata). 2. Database Query Optimization: Implement indexing on frequently searched columns (price, bedrooms, property_type). Utilize prepared statements and ORM query builders for efficient, parameterized queries. Employ database-level query caching for identical, frequent requests. 3. Caching Strategies: Implement a multi-layered caching approach. Use a reverse proxy (e.g., Varnish) for static content. Implement an in-memory cache (e.g., Redis) for frequently accessed property listings, invalidated upon updates. Use ETag headers for conditional GET requests to reduce bandwidth.

Sample answer

For a real estate platform's property search, I'd design a RESTful API endpoint using the MECE framework. The API contract would be a GET request to /api/v1/properties with query parameters like minPrice, maxPrice, beds, type, page, and pageSize. The response would be a JSON array of property objects, including id, address, price, beds, baths, type, and pagination metadata (currentPage, totalPages, totalResults).

Database query optimization would involve creating B-tree indexes on price, beds, property_type, and status columns in the properties table. I'd use an ORM (e.g., SQLAlchemy with PostgreSQL) to construct efficient, parameterized queries, ensuring OFFSET and LIMIT for pagination are optimized. For caching, I'd implement a multi-tier strategy: a CDN for static assets, Redis for frequently accessed property listings (e.g., top 100 most viewed), invalidated via a publish/subscribe pattern on property updates. Additionally, HTTP caching headers (Cache-Control, ETag) would be used for conditional GET requests, reducing server load and bandwidth by 30% for repeat visitors.

Key points to mention

  • • API Contract (URL structure, HTTP methods, request/response formats, error handling)
  • • Database Indexing (B-tree, GIN/GiST for specific data types)
  • • Query Optimization (EXPLAIN ANALYZE, query planner understanding)
  • • Pagination Strategies (OFFSET/LIMIT vs. Keyset Pagination)
  • • Caching Mechanisms (Redis, Memcached)
  • • Cache Invalidation Policies (TTL, Write-Through, Write-Back, Cache-Aside)
  • • Security Considerations (Input validation, Rate Limiting)
  • • Scalability (Horizontal scaling of API, database read replicas)

Common mistakes to avoid

  • ✗ Lack of proper indexing leading to slow query performance.
  • ✗ Inefficient pagination (e.g., large OFFSET values without keyset pagination).
  • ✗ Over-caching or under-caching, leading to stale data or poor performance gains.
  • ✗ Ignoring API versioning or proper error handling in the contract.
  • ✗ Not considering security aspects like SQL injection or excessive data exposure.