HomeInterview QuestionsHow would you design a generic function that can h…

How would you design a generic function that can handle such scenarios and be reused across platforms?

🟡 Medium Conceptual Mid level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

I would create a platform‑agnostic abstraction using interfaces or protocols. For example, in TypeScript I’d define an interface DataFetcher with a fetch() method that returns a Promise of a generic type T. Each platform (Node, browser, mobile) would implement this interface with its own HTTP client or SDK. I’d use dependency injection to supply the concrete implementation at runtime, allowing the same business logic to run anywhere. The function would be generic over T, so callers can specify the expected shape of the data. I’d also apply the Strategy pattern to swap out retry or caching strategies, and the Factory pattern to instantiate the correct fetcher based on environment variables. This design keeps the core logic platform‑agnostic, promotes testability, and reduces duplication.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500