Question presented to candidate: "Your app needs to decide how many worker processes to spawn based on the machine it is running on, and needs a safe place to write a temporary file. What built-in module gives you that information?"
What a strong answer should cover:
- Node's built-in
osmodule exposes operating-system-level information: CPU count/details, total and free memory, platform identifier, network interfaces, the user's home directory, and the system's temp directory. os.cpus()returns an array with one entry per logical CPU core, and.lengthis the standard way application code decides how many worker processes/threads to spawn for CPU-bound parallelism (feeding directly into theclustermodule or a Worker Thread pool, both covered in their own dedicated questions).os.totalmem()/os.freemem()report memory in bytes, for the whole machine — not the current Node process's own memory usage, which is a separate concern (process.memoryUsage()), a commonly conflated pair.os.tmpdir()gives the correct, platform-appropriate temporary directory — critically, this is not a fixed path; it varies by OS and even by user account, and hardcoding/tmp(a POSIX-only assumption) breaks on Windows.os.platform()returns a specific identifier ("win32","darwin","linux", etc.) — the standard, correct way to branch on operating system, rather than inferring it indirectly from something like a path separator.os.EOLgives the platform's correct line-ending sequence (\non POSIX,\r\non Windows) — relevant when generating text output meant to look correct when opened in a platform-native text editor.
Clarifying questions expected:
- "Is the concern the whole machine's resources, or this specific Node process's own usage?" —
osreports the former;processreports the latter. - "Does the code need to run correctly across multiple operating systems, or only one known target?" — decides how much of
os's cross-platform value actually matters here.
Code / implementation expected: Optional — reading real values directly from os on the actual running machine is a clean, concrete way to ground the answer rather than describing the module abstractly.