Skip to content

Instantly share code, notes, and snippets.

@nirlanka
Created November 19, 2019 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nirlanka/ed10b8cc9806bc192625a33837ba4a3b to your computer and use it in GitHub Desktop.
Save nirlanka/ed10b8cc9806bc192625a33837ba4a3b to your computer and use it in GitHub Desktop.
Answers to some simple C# questions I got for an interview
// Each account on a website has a set of access flags that represent a user's access.
//
// Update and extend the enum so that it contains three new access flags:
//
// [Flags]
// public enum Access
// {
// Delete = 1,
// Publish = 2,
// Submit = 3,
// Comment = 4,
// Modify = 5,
// }
//
// 1. A `Writer` access flag that is made up of the `Submit` and `Modify` flags.
// 2. An `Editor` access flag that is made up of the `Delete`, `Publish` and `Comment` flags.
// 3. An `Owner` access that is made up of the `Writer` and `Editor` flags.
//
// For example, the code below should print "False" as the `Writer` flag does not contain the `Delete` flag.
//
// `Console.WriteLine(Access.Writer.HasFlag(Access.Delete));`
using System;
namespace Simple2
{
[Flags]
public enum Access
{
None = 0,
Delete = 1,
Publish = 2,
Submit = 4,
Comment = 8,
Modify = 16,
Writer = Submit | Modify,
Editor = Delete | Publish | Comment,
Owner = Writer | Editor,
// 000010 = delete
// 001000 = submit
// 100000 = modify
// 101000 = writer
}
public class AccessTest
{
public static void Main(string[] args)
{
Console.WriteLine(Access.Writer.HasFlag(Access.Delete));
// More examples:
Console.WriteLine(Access.Writer.HasFlag(Access.Writer));
Console.WriteLine(Access.Writer.HasFlag(Access.Submit));
}
}
}
// User interface contains to types of controls: `TextInput`, which accepts all characters and `NumericInput`, which accepts only digits.
//
// Implement the class `TextInput` that contains
// - Public method `void Add(char c)` - adds the given character to the current value
// - Public method `string GetValue()` - returns the current value
//
// Implement the class `NumericInput` that:
// - Inherits `TextInput`
// - Overrides the `Add` method so that each non-numeric character is ignored.
//
// For example, the following code should output "10":
// TextInput input = new NumericInput();
// input.Add('1');
// input.Add('a');
// input.Add('0');
// Console.WriteLine(input.GetValue());
using System;
namespace Simple1
{
public class TextInput
{
private string text = @"";
public virtual void Add(char c)
{
text += c;
}
public string GetValue()
{
return text;
}
}
public class NumericInput : TextInput
{
override public void Add(char c)
{
if (char.IsNumber(c))
{
base.Add(c);
}
}
}
public class UserInput
{
public static void Main(string[] args)
{
TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input.GetValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment