- Published on
Nave a la deriva - Parte 2 - Interview challenge solution
- Authors

- Name
- Christian Guevara
- @cgTheDev
TLDR: Create a /phase-change-diagram endpoint that calculates specific volumes for liquid and vapor using linear interpolation based on pressure input.
This challenge belongs to a series of programming challenges published by AltScore. You can see the complete list here. This is the ninth challenge.
Problem Statement
Nave a la deriva Año 2315 - Parte 2: Datos Corruptos, el Cambio de Fase y un Cuaderno Garabateado
Trama:
Un suspiro de alivio escapa de tus labios al ver al robot reparador acoplarse a tu nave. La esperanza se renueva, pero dura poco. Una alarma estridente te saca de tu momentánea tranquilidad. El robot ha detectado una avería crítica: datos corruptos relacionados con la curva de "saturación y cambio de fase P-v" del fluido hidráulico. Sin esta información, la nave no puede calibrar sus actuadores y sigue a la deriva.
Una oleada de frustración te invade. ¡Tú eres un programador, no un ingeniero mecánico! Pero la desesperación da paso a la determinación. Siempre has sido bueno resolviendo problemas, y este no será la excepción.
La documentación del robot te da una pista: realizará 10 peticiones HTTP a la ruta /phase-change-diagram para intentar reconstruir el archivo corrupto. Ahí está tu oportunidad.
Pista:
Mientras buscas frenéticamente entre los manuales de la nave, encuentras el cuaderno de bitácora del ingeniero mecánico. La última entrada termina abruptamente con un "¡Wubba Lubba Dub-Dub!" garabateado y una mancha de lo que sospechas es salsa Sichuan...¡Pero entre diagramas a medio terminar y ecuaciones a medio resolver, encuentras la curva de saturación del fluido hidráulico!

Analysis of the problem
Your spaceship is still adrift! This time, the cooling system is failing and you need to calculate the phase change values of the coolant.
The ship's control system needs an endpoint that, given a pressure, returns:
- The specific volume of the liquid (
specific_volume_liquid) - The specific volume of the vapor (
specific_volume_vapor)
These values must be calculated using linear interpolation based on a phase change diagram.
Diagram data:
- Minimum pressure: 0.05
- Maximum pressure (critical point): 10
- Liquid volume at minimum pressure: 0.00105
- Liquid volume at maximum pressure: 0.0035
- Vapor volume at minimum pressure: 30
- Vapor volume at maximum pressure: 0.0035 (at the critical point, liquid = vapor)
Resources:
- Your server must be publicly accessible
- Endpoint:
GET /phase-change-diagram?pressure=X - Submit your server URL here:
[POST] /v1/s1/e9/solution - Don't forget to check the Documentation!
Solution
Understanding the thermodynamics
This challenge is based on real thermodynamics concepts. A phase change diagram shows how a substance transitions between liquid and vapor states at different pressures.
At the critical point, the liquid and vapor phases become indistinguishable - they have the same specific volume.
The key insight is that we need to use linear interpolation to calculate values between the known points.
Linear interpolation formula
The interpolation formula (also known as "la pendiente" or slope formula):
Where:
- = input pressure
- = minimum pressure (0.05)
- = maximum pressure (10)
- = volume at minimum pressure
- = volume at maximum pressure
The implementation
// app/api/phase-change-diagram/route.ts
import { NextRequest, NextResponse } from 'next/server'
const lowestPressure = 0.05,
// Fluid
fluidVolume1 = 0.00105,
// Gas
gasVolume1 = 30,
// Critical point
highestPressure = 10,
// Fluid
fluidVolume2 = 0.0035,
// Gas
gasVolume2 = 0.0035
// Linear interpolation function
function interpolate(pressure: number, P1: number, P2: number, v1: number, v2: number): number {
return v1 + ((pressure - P1) * (v2 - v1)) / (P2 - P1)
}
function getSpecificVolumes(pressure: number) {
// Limits according to the diagram
if (pressure >= highestPressure) {
return { specific_volume_liquid: fluidVolume2, specific_volume_vapor: gasVolume2 }
}
if (pressure <= lowestPressure) {
return { specific_volume_liquid: fluidVolume1, specific_volume_vapor: gasVolume1 }
}
const volumeF = interpolate(pressure, lowestPressure, highestPressure, fluidVolume1, fluidVolume2)
const volumeG = interpolate(pressure, lowestPressure, highestPressure, gasVolume1, gasVolume2)
return { specific_volume_liquid: volumeF, specific_volume_vapor: volumeG }
}
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url)
const pressureParam = searchParams.get('pressure')
// 400 if missing params or outside the ranges
if (!pressureParam) {
return NextResponse.json({ error: "Missing 'pressure' parameter" }, { status: 400 })
}
const pressure = parseFloat(pressureParam)
if (isNaN(pressure) || pressure < lowestPressure || pressure > highestPressure) {
return NextResponse.json({ error: "Invalid 'pressure' value" }, { status: 400 })
}
const result = getSpecificVolumes(pressure)
return NextResponse.json(result, { status: 200 })
}
Testing the endpoint
# At minimum pressure
curl "http://localhost:3000/api/phase-change-diagram?pressure=0.05"
# {"specific_volume_liquid":0.00105,"specific_volume_vapor":30}
# At critical point (max pressure)
curl "http://localhost:3000/api/phase-change-diagram?pressure=10"
# {"specific_volume_liquid":0.0035,"specific_volume_vapor":0.0035}
# At midpoint pressure
curl "http://localhost:3000/api/phase-change-diagram?pressure=5"
# {"specific_volume_liquid":0.0022739..,"specific_volume_vapor":15.0012...}
Key points
- Boundary conditions: At the limits (0.05 and 10), return exact values
- Linear interpolation: Between limits, interpolate linearly
- Critical point: At maximum pressure, liquid and vapor volumes are equal (0.0035)
- Validation: Return 400 for invalid or missing pressure values
The thermodynamics behind it
In a real phase change diagram:
- Below critical pressure: Liquid and vapor coexist with different densities
- At critical pressure: The distinction between liquid and vapor disappears
- Specific volume: Inverse of density (volume per unit mass)
The "Rick and Morty reference" in my code comments refers to the fact that the values seemed arbitrary at first, like something from a sci-fi show, but they follow real thermodynamic principles!
Submit the solution
curl -X 'POST' \
'https://makers-challenge.altscore.ai/v1/s1/e9/solution' \
-H 'accept: application/json' \
-H 'API-KEY: API-KEY' \
-H 'Content-Type: application/json' \
-d '{
"url": "https://yourdomain.vercel.app"
}'
And the response is:
{
"result": "correct"
}
TADA! 🎉