Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.challenge;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class App {
public static void main(String[] args) {
if (args.length < 1){
System.out.println("Provide json file");
System.exit(0);
}
processJSON(args[0]);
}

private static void processJSON(String filename) {
JSONParser parser = new JSONParser();
Path path = Paths.get(filename);
String content = null;
JSONArray jsonArray = null;
try {
byte[] encoded = Files.readAllBytes(path);
content = new String(encoded);
jsonArray = (JSONArray) parser.parse(content);
}catch (FileNotFoundException e){
System.out.printf("File: \"%s\" Not found.\n", filename);
System.exit(1);
} catch (IOException | ParseException e) {
e.printStackTrace();
System.exit(1);
}
for (Object o : jsonArray) {
JSONObject user = (JSONObject) o;
String userInsert = String.format(
"INSERT INTO users (record_id, name, email, cell_phone, work_phone, address, protection_plan)" +
"VALUES (%d, %s, %s, %s, %s, %s, %b)",
(Long) user.get("Record ID"),
user.get("Name"),
user.get("Email"),
user.get("Cell Phone"),
user.get("Work Phone"),
user.get("Address"),
user.get("Protection Plan")
);
String widgetInsert = String.format("INSERT INTO widgets (advanced_order, basic_order, user_record_id) " +
"VALUES (%d, %d, %d)",
(Long) user.get("Advanced Widget Order"),
(Long) user.get("Basic Widget Order"),
(Long) user.get("Record ID")
);
System.out.println(userInsert);
System.out.println(widgetInsert);
}
}
}
15 changes: 15 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.interview;

import java.util.List;

public class Main {

public static void main(String[] args) {
Solution solution = new Solution();
List<Character> inputs = List.of('t', 't', 't', 't', 'b', 'c', 'c', 'a', 'a', 'd', 'r', 'r', 'r', 'r');
List<Pair<Integer, Character>>outputs = solution.solve(inputs);
for (Pair<Integer, Character> pair : outputs) {
System.out.println(pair);
}
}
}
33 changes: 33 additions & 0 deletions Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.interview;

import java.util.Objects;

public class Pair<K, V> {
private final K key;
private final V value;

public Pair(K key, V value) {
this.key = key;
this.value = value;
}

public K getKey() {
return key;
}

public V getValue() {
return value;
}

public boolean equals(Object o) {
return o instanceof Pair && Objects.equals(key, ((Pair<?, ?>) o).key) && Objects.equals(value, ((Pair<?, ?>) o).value);
}

public int hashCode() {
return 31 * Objects.hashCode(key) + Objects.hashCode(value);
}

public String toString() {
return String.format("(%s, %s)", key, value);
}
}
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
## Headstorm Interview
# Headstorm Programming Challenges

Welcome to the Headstorm interview challenge! This repository is designed for candidates to [fork and create Pull Requests](https://help.github.com/en/articles/creating-a-pull-request-from-a-fork) with their solutions. There are two types of 'take-home' problems here:
## Front End Challenge

### Challenges
These are domain specific problems that can be submitted individually. You can choose from backend, frontend, databases, or data-science. You can submit a PR for one, many, or all the challenges.
The project is in frontend folder. Just open the folder and open the index.html file.

### Interviews
These are language specific interview questions and you can choose the language in which you implement your solution.
## Back End Challenge

Create a REST API using java Spring web framework which performs the following functionality:

- Provides a POST endpoint at `/data` where a user submits a JSON formatted list of 500 random numbers. The list has to be exactly 500 numbers, if there are more or less than 500 an error must be returned. Similarly, if something other than a list of numbers is submitted, an error must be returned.
- Provides a GET endpoint at `/data` which provides the same JSON formatted list of 500 numbers that are sorted from lowest to highest.

> random-rest foolder holds colder to this challenge. A jar file is generated that solves the challenge

```sh
$ java -jar target/random-rest-0.0.1-SNAPSHOT.jar

# use curl, postman or browser to query the api
# using curl to GET
curl http://localhost:8080/data
# to POSt user array in random-rest folder test.json
```

## Database Challenge

### Part 1

Relational data model visualization.
> open db-challenge folder; there is uml, and png file.

### Part 2

A basic program i that read in a JSON file that contains the records from the old database (Nosql), format the data to match your new data model, and print SQL statements to console/standard IO that would insert these records into the new database.

> The jar file is in the folder with the generated json file

```sh
java -jar db-challenge-1.0-SNAPSHOT.jar no-sql-data.json

```
60 changes: 60 additions & 0 deletions RandomNumberController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.challenge.randomrest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/data", produces = "application/json")
public class RandomNumberController {
private List<Integer> userData;

@GetMapping
public ResponseEntity<String> processUserData() {
if (userData == null) {
return handleBadRequest("No list created", HttpStatus.BAD_REQUEST);
}
Collections.sort(userData);
ObjectMapper mapper = new ObjectMapper();
String jsonArray = "";
try {
jsonArray = mapper.writeValueAsString(userData);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return new ResponseEntity<>(jsonArray, new HttpHeaders(), HttpStatus.OK);
}

@PostMapping(consumes = "application/json")
public ResponseEntity<String> postUserData(@RequestBody List<Integer> randomNumList) {
if (randomNumList.size() != 500) {
return handleBadRequest("Invalid List", HttpStatus.BAD_REQUEST);
}
userData = randomNumList;
return handleBadRequest("List created successfully", HttpStatus.CREATED);
}


private ResponseEntity<String> handleBadRequest(String status, HttpStatus statusCode) {
ObjectMapper mapper = new ObjectMapper();
HttpHeaders responseHeaders = new HttpHeaders();
Map<String, String> body = new HashMap<>();
body.put("message", status);
String jsonBody = "";
try {
jsonBody = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(body);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return new ResponseEntity<>(jsonBody, responseHeaders, statusCode);
}
}
13 changes: 13 additions & 0 deletions RandomRestApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.challenge.randomrest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RandomRestApplication {

public static void main(String[] args) {
SpringApplication.run(RandomRestApplication.class, args);
}

}
21 changes: 21 additions & 0 deletions Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.interview;

import java.util.*;

public class Solution {
public List<Pair<Integer, Character>> solve(List<Character> input) {
List<Pair<Integer, Character>> outputs = new ArrayList<>();
for (Map.Entry<Character, Integer> pair : counter(input).entrySet()) {
outputs.add(new Pair<>(pair.getValue(), pair.getKey()));
}
return outputs;
}

private Map<Character, Integer> counter(List<Character> input) {
final Map<Character, Integer> counts = new LinkedHashMap<>();
for (Character word : input) {
counts.merge(word, 1, Integer::sum);
}
return counts;
}
}
50 changes: 50 additions & 0 deletions contact.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
<title>HeadStorm</title>
</head>
<body>
<header>
<nav class="navbar">
<h1><a href="index.html">HeadStorm</a></h1>
<div><a href="contact.html">Contact Us</a></div>
</nav>
</header>
<main>

<div class="container contact">
<div class="jumbotron">
<div class="header">
<h2>Get in Touch!</h2>
</div>
<div class="form-div">
<form class="contact-form">
<div class="form-group">
<label for="username">Name</label>
<input type="text" class="form-control" id="username" placeholder="Enter Name" required>
</div>
<div class="form-group">
<label for="inputEmail">Email address</label>
<input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp"
placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" id="message" cols="30" rows="10"
placeholder="Message" required></textarea>
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</main>
<script src="js/validation.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
<title>HeadStorm</title>
</head>
<body>
<header>
<nav class="navbar">
<h1><a href="index.html">HeadStorm</a></h1>
<div><a href="contact.html">Contact Us</a></div>
</nav>
</header>
<main>

<div class="container">
<div class="jumbotron">
<h1>Welcome to <span class="jum-header">HeadStorm</span></h1>
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus aperiam architecto enim ex, ipsum mollitia,
nostrum obcaecati officia quis, quos rem reprehenderit vel vitae! Aspernatur fugit nihil nobis quis
veritatis?</p>
<hr/>
<p>Contact us to learn more about us.</p>
</div>
</div>
</main>
</body>
</html>
1 change: 1 addition & 0 deletions no-sql-data.json

Large diffs are not rendered by default.

Binary file added schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading