Problem statement
Write function solution(documents, query, limit). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. documents:{id:string,vector:integer[]}[], query:integer[], limit:nonnegative integer. Vectors share dimension 1–32 with values -100…100. Cosine=dot/(product of magnitudes); use zero if either vector is zero. score=Math.round(cosine*1_000_000). Sort by rounded score descending, preserving input order on ties. Return top limit {id,score}; limit may exceed document count. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.
Example input and output
Example 1
Function arguments
[
[
{
"id": "a",
"vector": [
1,
0
]
},
{
"id": "b",
"vector": [
0,
1
]
}
],
[
1,
0
],
2
]Return value
[
{
"id": "a",
"score": 1000000
},
{
"id": "b",
"score": 0
}
]Example 2
Function arguments
[
[
{
"id": "a",
"vector": [
1,
1
]
},
{
"id": "b",
"vector": [
2,
2
]
}
],
[
1,
1
],
1
]Return value
[
{
"id": "a",
"score": 1000000
}
]Example 3
Function arguments
[
[
{
"id": "a",
"vector": [
1,
0
]
},
{
"id": "b",
"vector": [
0,
1
]
},
{
"id": "c",
"vector": [
1,
0
]
}
],
[
1,
0
],
2
]Return value
[
{
"id": "a",
"score": 1000000
},
{
"id": "c",
"score": 1000000
}
]