Pine Labs Interview Question

Design a caching mechanism using multi threading

Interview Answer

Anonymous

Mar 18, 2026

using System; using System.Collections.Concurrent; public class CacheItem { public T Value { get; set; } public DateTime Expiry { get; set; } } public class MemoryCache { private ConcurrentDictionary> _cache = new ConcurrentDictionary>(); public void Set(string key, T value, int seconds) { var item = new CacheItem { Value = value, Expiry = DateTime.UtcNow.AddSeconds(seconds) }; _cache[key] = item; } public T Get(string key) { if (_cache.TryGetValue(key, out var item)) { if (item.Expiry > DateTime.UtcNow) { return item.Value; } else { _cache.TryRemove(key, out _); } } return default(T); } }