---
# System prepended metadata

title: Live Coding

---

# Live Coding 

### **Requirements**

You are tasked to implement a simple HTTP server in Golang that exposes an API endpoint to retrieve the most frequently occurring word in a text document.

Your HTTP server should have the following API endpoint:

```bash
POST /frequent_word
```

The endpoint should accept a JSON payload with the following format:

```json
{
    "text": "string"
}
```

Where **`"text"`** is a string containing the text document.

Your HTTP server should respond with a JSON payload with the following format:

```json
{
    "word": "string",
    "count": int
}
```

Where **`"word"`** is the most frequently occurring word in the text document, and **`"count"`** is the number of occurrences of the word.

**Examples**

```json
{
    "text": "Hello world world world hello"
}
```

```json
{
    "word": "world",
    "count": 3
}
```

### **Instructions**

- The program should parse the request payload, find the most frequently occurring word, and return the result in the response payload.
- You may assume that the text input contains only alphabetical characters, spaces, and punctuation marks.
- The word count should be case-insensitive, and the word returned should be in lowercase.
- If multiple words have the same highest frequency, return any of them.
- Implement any additional logic and error handling that you feel is necessary.
- You may use any additional libraries or packages that you feel is necessary.

### **Please ask no questions - Improvise!**

Another part of the exercise, is to test how well you can think on your own, when you have very limited information.

So if you have any questions, ask yourself what you would have done, and do that.

This is very important, as you will be given a lot of responsibility with this job.

### **Use this code as your starting point**

```go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type InputRequest struct {
	Text string `json:"text"`
}

type Result struct {
	Word  string `json:"word"`
	Count int    `json:"count"`
}

func main() {
	http.HandleFunc("/frequent_word", handler)
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		return
	}

	decoder := json.NewDecoder(r.Body)
	defer r.Body.Close()

	var inputRequest InputRequest
	if err := decoder.Decode(&inputRequest); err != nil {
		http.Error(w, "Invalid request payload", http.StatusBadRequest)
		return
	}

	// prepare variable to store the most frequent word
	var word string
	var maxCount int

	// to do:
	// 1. ...
	// 

	// create a result object
	result := Result{
		Word:  word,
		Count: maxCount,
	}

	// encode the result object as JSON and return it
	w.Header().Set("Content-Type", "application/json")
	err := json.NewEncoder(w).Encode(result)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
```