✅ 【WPFバックエンドエンジニア向け】英語Live Coding模擬問題集(出題+模範解答)

▼ Problem 1: Thread-Safe Singleton (Concurrency)

Question (English):
“Implement a thread-safe Singleton pattern in C#. Make sure the implementation is lazy-loaded and supports multiple threads.”


Sample Solution:

public sealed class Singleton
{
private static readonly Lazy<Singleton> _instance = new Lazy<Singleton>(() => new Singleton());

public static Singleton Instance => _instance.Value;

private Singleton()
{
// Initialization code here
}
}

▼ Problem 2: Reverse a Linked List (Algorithm)

Question:
“Given the head of a singly linked list, write a method to reverse the list.”


Sample Solution:

public class ListNode
{
public int Val;
public ListNode Next;
public ListNode(int val) => Val = val;
}

public ListNode ReverseList(ListNode head)
{
ListNode prev = null;
ListNode current = head;

while (current != null)
{
ListNode nextTemp = current.Next;
current.Next = prev;
prev = current;
current = nextTemp;
}

return prev;
}

▼ Problem 3: First Non-Repeated Character (Hashing)

Question:
“Write a method that returns the first non-repeating character from a string.”


Sample Solution:

public char? FirstNonRepeatedChar(string input)
{
var charCount = new Dictionary<char, int>();

foreach (var c in input)
{
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}

foreach (var c in input)
{
if (charCount[c] == 1)
return c;
}

return null; // No non-repeated character
}

▼ Problem 4: WPF MVVM Basic Design (Architecture / Design Pattern)

Question:
“Design a simple WPF MVVM structure for a ToDo list application. Briefly explain your approach.”


Sample Answer (Verbal explanation format):

“I would create three main components:

  1. Model: Represents the ToDo item (e.g., ID, Title, IsCompleted).
  2. ViewModel: Contains an ObservableCollection of ToDo items and ICommand implementations for Add and Remove operations.
  3. View: Binds UI elements like ListBox and Buttons to the ViewModel properties and commands.

I’d also use INotifyPropertyChanged for data binding and RelayCommand or DelegateCommand for command handling.”*


▼ Problem 5: Cancel an Async Task (CancellationToken)

Question:
“Write a C# method that performs a long-running task and supports cancellation using CancellationToken.”


Sample Solution:

public async Task LongRunningOperationAsync(CancellationToken cancellationToken)
{
for (int i = 0; i < 10; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"Processing item {i}");
await Task.Delay(500, cancellationToken);
}
}

▼ Problem 6: LINQ – Filter and Project

Question:
“Given a list of integers, write a LINQ query to return all even numbers multiplied by 2.”


Sample Solution:

var result = numbers.Where(n => n % 2 == 0).Select(n => n * 2).ToList();

▼ Problem 7: JSON Parsing

Question:
“Parse the following JSON string into a C# object using Newtonsoft.Json.”

JSON Sample:

{
"Id": 1,
"Name": "Sample Item",
"IsActive": true
}

Sample Solution:

public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}

string json = @"{ ""Id"":1, ""Name"":""Sample Item"", ""IsActive"":true }";
var item = JsonConvert.DeserializeObject<Item>(json);

▼ Problem 8: Permutation Check (String)

Question:
“Write a method to check if two strings are permutations of each other.”


Sample Solution:

public bool ArePermutations(string str1, string str2)
{
if (str1.Length != str2.Length)
return false;

var count = new int[256];

foreach (var c in str1)
count[c]++;

foreach (var c in str2)
{
count[c]--;
if (count[c] < 0)
return false;
}

return true;
}

▼ Problem 9: REST API Consumption

Question:
“Write C# code to make an HTTP GET request and parse JSON response.”


Sample Solution:

public async Task<Item> GetItemAsync(string url)
{
using var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync(url);
return JsonConvert.DeserializeObject<Item>(response);
}

▼ Problem 10: Explain: Task.Run vs Task.Factory.StartNew

Question:
“Explain the difference between Task.Run and Task.Factory.StartNew.”


Sample Answer (Verbal explanation format):

“Task.Run is a simpler API that targets the thread pool and is optimized for short-running, compute-bound tasks.
Task.Factory.StartNew offers more control, such as specifying TaskScheduler or TaskCreationOptions.
For most scenarios, especially starting with .NET 4.5 and later, Task.Run is recommended.”


✅ 【補足Tips】

  • 本番ではこれに似た問題が「白板 or CoderPad or HackerRank」で出ます。
  • 解く前に必ず「Let me explain my approach first」などと考え方を英語で先に説明する癖をつける
  • コード書いたあとに**「Now let me walk you through the code step by step」**と言って、口頭でロジック説明すること

コメント

タイトルとURLをコピーしました