My Ruby and Javascript Solution for a Login Apocalypse

This is my solution to a potential coding challenge question that you can get during an interview.

PART 1:

Within each state, each citizen is eligible for the following:

  • to sign up on the machine for an account
  • to send a maximum of 5 emails per week
  • to receive an unlimited number of messages
  • to send a maximum of 200 characters per week (doesn’t matter if they’ve yet reached their five message limit. whichever comes first is the cutoff)

Exercise: Come up with a basic RDBMS ERD for such a system. We only need to see entities, associations, and attributes (don’t go too overboard, just the necessities).

Diagrams:
Citizens table:
first name
last name
username
address
created_at - creation timestamp
updated_at - update timestamp
Emails table:
content
citizen_id - citizen that sends the email
receiver_id - citizen that receives the email
citizen_active - can be set to false when deleted weekly
receiver_active - can be set to false when deleted weekly
created_at - creation timestamp
updated_at - update timestamp
Rails Schemas:create_table "citizens", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "emails", force: :cascade do |t|
t.string "content"
t.integer "citizen_id"
t.integer "sender_id"
t.boolean "citizen_active"
t.boolean "sender_active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
t.datetime "updated_at", null: falseend

BONUS: Based on your ERD, write a method that takes in a user as an argument and runs a SQL query to determine whether that user is eligible to keep using the computer at that moment (i.e. they haven’t reached any limits).

def isEligible?(citizenID)
sentEmails = Email.connection.select_all("
SELECT COUNT(id) AS number_of_emails, SUM(LEN(content)) AS char_count
FROM emails
WHERE sender_id = #{citizenID}
AND created_at BETWEEN DATEADD(DAY, -7, GETDATE()) AND DATEADD(DAY, 1, GETDATE())
ORDER BY created_at DESC
").to_hash
if sentEmails.number_of_emails > 5
return false
end
if sentEmails.char_count > 200
return false
end
return true
end

PART 2:

It’s very important that these citizens sign up correctly with their information the first time. They don’t get a second chance (time constraints / bureaucracy) and there’s no backend validation for the information they submit. They just need to put in a username, a password, and their address and then click a Submit button.

These citizens have never seen a computer before, but let’s assume they understand the following:

  • how to read English
  • how using a mouse and clicking to initiate action on the screen works
  • how to type and what inputs/textareas are for

Exercise: What are five things you’d make sure the website included, did, or checked (and how) to ensure that each citizen has the best chance of submitting everything correctly the first time?

Here are some examples to get started. Finish these sentences and then create three from scratch:

  • Citizens won’t know to click the Submit button when they’re done to submit the information, so we’ll…
  • Citizens might make a typo in their address and click submit, so we’ll…
Citizens won’t know to click the Submit button when they’re done to submit the information, so we’ll display a message telling them to click the submit button.Citizens might make a typo in their address and click submit, so we’ll have an address validation check against a map API as they type that will display if it is a valid address or not.Citizens won’t know if the username they picked is a valid format or if it was taken, so we’ll display error messages and explain why their username is invalid.Citizens won’t know if the password they picked is a valid format, so we’ll display error messages and explain why their password is invalid.Citizens might make a typo in their password when they sign up, so we’ll give them 2 input text areas for a password that must match.

BONUS: Write a Javascript validation (jQuery allowed) that takes in a password as an argument and will show a very clear error message if the password is not at least 8 characters and does not contain at least one letter, one number, and the % symbol.

function validate(password){
const errorDisplay = document.querySelector(".errorDisplay") //is an unordered list
let errors = []
const tests = [
{regex: /.{8,}/, message: "The password is not at least 8 characters."},
{regex: /[A-Za-z]/, message: "The password does not contain at least one letter."},
{regex: /[0-9]/, message: "The password does not contain at least one number."},
{regex: /[%]/, message: "The password does not contain the % symbol."},
]
tests.forEach(test => {
if (test.regex.test(password) === false)
errors.push(test.message)
})
if (errors.length > 0) {
for (let i = 0; i < errors.length; i++) {
errorDisplay.innerHTML += `<li> ${errors[i]} </li>`
}
errorDisplay.innerHTML += "<li> Please enter a new password. </li>"
}
}

This was a fun exercise to go through. I hope it will be useful for you too!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store