aboutsummaryrefslogtreecommitdiff
path: root/doc/plot.md
blob: 4f1139eefb04d3dc93ef651ce975341d2b30e1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Capital Match Hiring Coding Challenge

## Introduction

Archaeologists and anthropologists would have a hard time believing that
millenia ago, there was already a healthy and booming democracy. Yet recent
evidence surfaced suggested that in the ancient Kingdom of Malodivo, the
democratic way of life was the norm, even though it is a bit peculiar by
modern standards. Not much was known about this Kingdom; about the only thing
we have fully figured out was its parliamentary system of governance, owing to
the fact that the sole piece of written evidence was the Kingdom's
*Parliamentary Gazette*, a document recording the discussions and resolutions
of the various bills in its Parliament.

Reading through the *Gazette*, we can now piece together a few tidbits of the
workings of the Parliament. The Parliament apparently meets every once in a
while (the exact frequency is unknown because we have not yet figured out
conversions between the Gregorian calendar and the calendar system used in the
Kingdom), and the members, who represent the constituents in districts, submit
bills for discussion and debate. The implementation of a bill requires
funding, naturally. Such funding is provided by the taxes paid by the members
of the parliament which ultimately comes from their constituents. In other
words, instead of each citizen paying taxes individually to a central
authority, each citizen pays the taxes to their elected members of the
Parliament.

Different members of the parliament have different priorities and have
different levels of support for each bill. They signal their varying degrees
of support through funding decisions: they would pledge different amounts of
their constituents' tax dollars to each bill. It would be easy if the sum of
the funds needed by all bills is equal to all the taxes collected; in this
case, the members' wishes could be fulfilled exactly. However, more often than
not, this does not happen. Owing to a booming economy, the total amount of
funding required for all bills is often less than the total tax paid by all
members. In this case, a widely cherished Article in the *Tax Code of the
Kingdom of Malodivo* comes into effect:

> Article XIV. All taxes collected by the Government but not used for the
  implementation of any bill shall be refunded.

Since the Parliament usually cannot pass enough bills to deplete the total
taxes it collected, it will simply reduce the amount of tax paid, and the
members will have a leftover amount. The citizens are happy because their
effective tax rate is reduced. It is important that once the Parliament has
decided on the funding needed for a bill, the bill cannot legally receive
extra funds; it is however possible that a bill will receive less funds than
the Parliament decides.

Your task, is to write a program to determine the best combination of the
funds from each member of the Parliament that go to each bill.

## Scenario

Suppose in the 137th Parliamentary Session of the Kingdom of Malodivo, four
bills were introduced and passed:

| Name of Bill                                           | Category | Funds Needed |
| ------------------------------------------------------ | -------- | ------------ |
| An Act to Construct the Great Wall of Malodivo         | Defense  | ₡200,000     |
| An Act to Construct Shelters for the Homeless          | Welfare  | ₡40,000      |
| An Act to Fund the Development of Longer-Lasting Paper | Science  | ₡14,000      |
| An Act to Increase Retirement Benefits for Veterans    | Welfare  | ₡90,000      |

As one could calculate, the total fund needed to implement these bills is
₡344,000. Although in this example the sum happens to be a multiple of a
thousand, generally the funds needed could be any nonnegative integer. (It's
quite trivial to handle a bill that needs no funding: every member of the
Parliament simply contributes ₡0 to the bill.)

Each member of the Parliament also has a total amount of taxes collected:

| District                      | Total Available Funds |
| ----------------------------- | --------------------- |
| District of Palolene          | ₡200,000              |
| District of Southern Palolene | ₡150,000              |
| District of Lakos             | ₡400,000              |

For each member of the parliament, the member has listened to his/her
constituents in a district and has decided how much of the taxes should go to
each category. There is a bijection between each member and each district, so
these two terms may be used interchangeably. For example, the District of
Palolene has the following table of funds allocation:

| Category | Funding Provided to *Each* Bill In This Category |
| -------- | ------------------------------------------------ |
| Defense  | ₡1,000                                           |
| Welfare  | ₡3,000                                           |
| Science  | ₡5,000                                           |

However, the district could also provide specific funding amount for each bill:

| Name of Bill                                           | Category | Funding Provided         |
| ------------------------------------------------------ | -------- | ------------------------ |
| An Act to Construct the Great Wall of Malodivo         | Defense  | Use default for category |
| An Act to Construct Shelters for the Homeless          | Welfare  | Use default for category |
| An Act to Fund the Development of Longer-Lasting Paper | Science  | ₡7,500                   |
| An Act to Increase Retirement Benefits for Veterans    | Welfare  | ₡500                     |

As you can see, the District of Palolene does not have a large population of
veterans, so the constituents have collectively decided to provide fewer funds
for this bill. In certain cases, the constituents may even decide to provide
zero funding to a bill they vehemently dislike. On the other hand, the
District decides that a durable writing medium is so important that it will
allocate ₡7,500 instead of ₡5,000 to the bill. Indeed a district could support
a bill so enthusiastically that it will decide to provide a higher amount than
the default for the category. However in any case the amount will never be
negative.

Usually the total funding as decided above across all districts for a
particular bill will exceed the amount required for that bill. In this case,
you should reduce the contribution by each district proportionally. In other
words, the proportions of the actual contribution among each district should
be as similar as possible as the amount each district originally decides to
provide. For example if a bill needs ₡10, but there are two districts and they
are willing to provide ₡400 and ₡600 respectively, then these two districts
will actually contribute only ₡4 and ₡6, satisfying the principle of
proportionality. Again, since only the smallest unit of currency in this
Kingdom is ₡1, all amounts are integers, and so suitable rounding needs to be
performed.

It should further be noted that the example above shows a prosperous year,
when the citizens have made good money and have a lot of taxes to pay. It is
quite possible that during a depression, the total available funds could
dwindle to such small amount that they could not fund all the bills the
Parliament has passed. In this case, the same principle of proportionality
applies. For example if there are two bills and a district wishes to provide
₡200 for one bill and ₡500 for another, but it only has ₡7 in total tax dollar
collected, then it should contribute only ₡2 and ₡5 respectively towards these
two bills. The issue with rounding also applies.

Different categories of the bills will generally be implemented by different
Ministries. For example the Ministry of Defense will receive the funds for the
bills in the Defense category, etc. (There is a bijection between the
categories of the bills and the ministries, so they could also be used
interchangeably.) However, the citizens of the Kingdom of Malodivo are
generally suspicious of corruption. They are worried that giving too much
money to a particular Ministry will tempt its Ministers and staff to be
corrupt and misuse the funds. Therefore, each District also has upper limits
on how much money they will provide for each Ministry. The following table
shows the limits for the District of Palolene:

| Ministry | Maximum Funding Provided To All Bills In This Category |
| -------- | ------------------------------------------------------ |
| Defense  | ₡3,000                                                 |
| Welfare  | ₡3,199                                                 |
| Science  | ₡15,000                                                |

For example, the maximum funding for all bills in the Welfare category is
capped at ₡3,199. Yet for the two bills in the Welfare category, the District
of Palolene has decided to fund ₡500 for one of them and ₡3,000 for the other.
The sum, ₡3,500, exceeds the maximum funding limit, ₡3,199. In this case, the
maximum funding limit takes precedence; the actual funding provided will sum
to ₡3,199, and ideally should be ₡457 and ₡2,742 respectively due to the
principle of proportionality. It goes without saying that rounding issues also
apply.

The interaction between the three principles of proportionality may not always
work out so well. It is your choice which proportionality is prioritized, or
you can even devise a better principle of proportionality that takes into
account all three in a balanced way.

In the unlikely event that across all members, the total funding provided to a
certain Ministry is capped so low that bills could not receive the full
funding, the situation should be left as is. The Ministry will simply have
less funds available to implement the bill. For example, suppose that the
suspicion towards the Ministry of Welfare is widespread, and other members
have similarly low caps for Welfare bills, then it is quite possible that the
Ministry of Welfare could not receive enough funds; it'll just have to make do
with that and heed this warning to reduce corruption, or potentially face the
deprivation of funds and the Minister could be forced to resign.

## Requirement

Your program will read the above data in the format of a JSON document,
perform computation, and write the result in a JSON document.

You have the freedom to decide the schema for your input and output JSON
documents.

As an example, you can use the following as a starting point but feel free to
change things as you see fit:

```js
{
    "bills": [{
        "name": "An Act to Construct the Great Wall of Malodivo",
        "category": "Defense",
        "amount": 200000
    },
    {
        "name": "An Act to Construct Shelters for the Homeless",
        "category": "Welfare",
        "amount": 40000
    },
    {
        "name": "An Act to Fund the Development of Longer-Lasting Paper",
        "category": "Science",
        "amount": 14000
    },
    {
        "name": "An Act to Increase Retirement Benefits for Veterans",
        "category": "Welfare",
        "amount": 90000
    }],
    "districts": [{
        "name": "Palolene",
        "availableFunds": 200000,
        "categoryDefaultFunding": [{
            "category": "Defense",
            "amount": 1000
        },
        {
            "category": "Welfare",
            "amount": 3000
        },
        {
            "category": "Science",
            "amount": 5000
        }],
        "billSpecificFunding": [{
            "bill": "An Act to Increase Retirement Benefits for Veterans",
            "amount": 500
        },
        {
            "bill": "An Act to Fund the Development of Longer-Lasting Paper",
            "amount": 7500
        }],
        "caps": [{
            "category": "Defense",
            "amount": 3000
        },
        {
            "category": "Welfare",
            "amount": 3199
        },
        {
            "category": "Science",
            "amount": 15000
        }]
    }]
}
```

The schema for the output is entirely decided by you, but at the very least
you should include the actual amounts that go towards each bill by each
district. You may optionally include other helpful information such as whether
a bill is fully funded, or the remaining balance of each district, or anything
else that you see fit.

### A Simplification

It should also be noted that there are very few *hard requirements*. In fact
there are only two:

1.  reading and writing data must be done in JSON;

2.  the program must be written in Haskell, but feel free to use any GHC
    extensions or open-source Haskell libraries.

As long as you fulfill these two requirements, we are interested in taking a
look at your code and having further discussions.

Besides these two, you should exercise your liberty in trying to follow the
description above. If you deviate from the above (likely), you should document
them either as comments in the program or in a separate writeup.

Efficiency is not a primary concern here. We do not expect any particular
Big-O complexity, but rather ask that your program finishes running in a
reasonable human-scale of time, i.e. at most a minute or two.

For your convenience, we have also provided a simplification of the problem
that could serve as a nice starting point:

*  You may assume there are no maximum funding limits or bill-specific
   amounts;

*  There is only one bill;

*  It just so happens that the amount for the bill (i.e.
   funds needed) is greater than or equal to the sum of the amounts for each
   district (i.e. total funds provided).

You should first get this simplification working, and then gradually add more
features and more complexities. Choose which features to implement according to
your taste. It's perfectly fine even if you do not implement everything. We
just want to see some working code, your design decisions, and your thought
processes.

## Unspecified Behavior

You should have noticed that this document does not specify every detail. For
example it specifies that the actual proportion should be as similar as
possible to the specified proportions, but the precise measure of similarity
is not specified here. You may use a widely used metric such as the L2 norm,
but also feel free to come up with your own. For many places where the
document is vague, it could very well be intentional and please pick anything
that is reasonable.

## Submission

You are given a week to ponder and solve this. If you finish early, feel free
to tell us; if you need a bit of extra time, also feel free to tell us so we
can arrange an extension.

Once you are done, please put your code in a private GitHub/Bitbucket
repository or email us the code. We will look through your code. If we like
what you have written, we will then schedule a chat over Hangouts or Skype and
we will have further discussions about the code; otherwise we will provide
written feedback to let you know why we think you are not a good fit for the
role.

Good luck!