So I was recently asked to complete the following challenge for an interview:
Programming Problem Provide a production ready solution that accepts a new user name, validates a single string input as the password, and stores it locally. The solution should be written in C#/Xamarin with native UI (Android or iOS, your choice, not Xamarin Forms) UI requirements:
- The UI should have at least a page that is a list of users and their information.
- The UI should have the ability to add a new user, which brings up a new screen, and then appends that user to the bottom of the list view.
- Alert the user if their passwords are invalid either on submission or real time.
The following are the string validations:
- String must consist of a mixture of letters and numerical digits only, with at least one of each.
- String must be between 5 and 12 characters in length.
- String must not contain any sequence of characters immediately followed by the same sequence.
My solution is here: https://github.com/rjowell/TestApp
I was recently notified that I did not pass. What could I have done differently, or is the interviewer being too nit-picky?
Here's a sample of my code:
using Foundation;
using Newtonsoft.Json;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using UIKit;
namespace CharterTestApp
{
public class User
{
public string name { get; set; }
public string location { get; set; }
public string username { get; set; }
public string password { get; set; }
public User(string nameInput, string loc, string user, string pass)
{
name = nameInput;
location = loc;
username = user;
password = pass;
}
}
public partial class LoginController : UIViewController
{
public LoginController(IntPtr handle) : base(handle)
{
}
public override bool ShouldPerformSegue(string segueIdentifier, NSObject sender)
{
return ProcLogin(usernameInput.Text, passwordInput.Text);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
bool ProcLogin(string username, string password)
{
string errorText = "";
if(username == "")
{
errorText += "Username is blank\n";
if(password == "")
{
errorText += "Password is blank\n";
}
ErrorLabel.Text = errorText;
ErrorLabel.Hidden = false;
return false;
}
else
{
foreach(User item in Application.userList)
{
if(usernameInput.Text == item.username)
{
if(password == "")
{
errorText += "Password is blank\n";
ErrorLabel.Text = errorText;
return false;
}
else if(passwordInput.Text != item.password)
{
errorText += "Password Incorrect\n";
ErrorLabel.Text = errorText;
return false;
}
else
{
Application.currentName = item.name;
Application.currentUsername = item.username;
return true;
}
}
}
errorText += "Username doesn't exist\n";
ErrorLabel.Text = errorText;
return false;
}
}
}
}
CreateNewUser.cs
using Foundation;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text.RegularExpressions;
using UIKit;
namespace CharterTestApp
{
public partial class CreateUserController : UIViewController
{
public CreateUserController (IntPtr handle) : base (handle)
{
}
partial void ProcessUserInput(UIButton sender)
{
string errorDisplays = "";
if (NameEntry.Text == "")
{
errorDisplays += "Name is blank\n";
}
else
{
foreach (User user in Application.userList)
{
if (user.name == NameEntry.Text)
{
errorDisplays += "User already exists with that name\n";
}
}
}
if(LocationEntry.Text == "")
{
errorDisplays += "Location is blank\n";
}
if(UsernameEntry.Text == "")
{
errorDisplays += "Username is blank\n";
}
else
{
foreach (User user in Application.userList)
{
if (user.username == UsernameEntry.Text)
{
errorDisplays += "User already exists with that name\n";
}
}
}
if(PasswordEntry.Text == "")
{
errorDisplays += "Password is blank\n";
}
else
{
if (Regex.IsMatch(PasswordEntry.Text,@"(..+)\1") || PasswordEntry.Text.Length < 5 || PasswordEntry.Text.Length > 12 || !Regex.IsMatch(PasswordEntry.Text, @"^[a-zA-Z0-9]+$", RegexOptions.IgnoreCase))
{
errorDisplays += "Password is not valid\n";
}
}
if (errorDisplays != "")
{
ErrorLabel.Hidden = false;
ErrorLabel.Text = errorDisplays;
}
else
{
Application.userList.Add(new User(NameEntry.Text,LocationEntry.Text,UsernameEntry.Text,PasswordEntry.Text));
File.WriteAllText("LoginData.json", "[");
foreach(User currentUser in Application.userList)
{
Console.WriteLine(JsonConvert.SerializeObject(currentUser));
File.WriteAllText("LoginData.json", JsonConvert.SerializeObject(currentUser));
}
File.WriteAllText("LoginData.json", "]");
Console.WriteLine(File.ReadAllText("LoginData.json"));
UserSuccessWindow.Hidden = false;
}
}
}
}