How to reverse a string with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
This is a straightforward and basic task to implement: given a string input to a function we want to return its reverse. There’s a wide range of solutions to this problem and here we’ll go through 3 of them using C#: the built-in Reverse extension function the Aggregate LINQ operator bare-bones solution based on a for-loop Here are the three implementations: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithms { public class StringReverse { public string ReverseString(string input ..read more
Visit website
Find the most frequent character in a string with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
Suppose you need a function that accepts a string and returns the character that occurs the most frequently in it. Examples: Input Result mamaaaa a gaga g agag a 12312312333456 3 Note that we’ll treat numeric values as strings. Also, with multiple values having the same max we’ll return the first one. We’ll build a character map out of the input string, just like the one we saw in this post. Here’s a reminder: What is a character map? It is a map where the key is of type char and the value if of type integer. We collect the unique characters of a string and count how many times e ..read more
Visit website
Determine if two strings are anagrams with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
Two strings are anagrams if they are made up of the same set of characters. Examples: “hello” and “loleh” “123123” and “312312” “qwerty” and “wretqy” The degree of “anagram-ness” can vary: ignore case? ignore non-numeric characters? ignore whitespace? In this post we’ll only consider word-characters only and the comparison will be case-insensitive to make the problem more interesting. We’ll write a function that accepts two integers and returns a boolean: true if the strings are anagrams, otherwise false. We’ll look at two solutions out of many that exist out there: using a character map ..read more
Visit website
How to build a circular matrix with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
I assume you know what a matrix is from your maths classes. They are two-dimensional containers of usually numeric values, but they can hold any type of data. They are widely used in various mathematical and other scientific fields such as machine learning. Here’s an example of a 3×3 matrix: 3 23 44 65 32 43 87 6 7 Matrices have columns and rows and their enumeration starts at 0. The coordinates of the various cells are given as row-column pairs. The top left hand cell is in row 0 and column 0, (0, 0) in short but there are other notations as well, I’ve even seen R0C0 at some point ..read more
Visit website
How to reverse integers with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
Say you need to write a function that accepts and integer and returns its reverse. Here come a couple of examples: Input Output 4 4 28 82 9876 6789 -4 -4 -456 -654 -1928 -8291 So we’re not talking about some mathematical function, but more of a string-reverse. However, note that the negative sign must stay in place, so there’s a little bit of maths in there. If that doesn’t sound like a problem that you would normally solve in a real life project then you’re probably right. I’m not even sure if there’s a legitimate business use-case for such a function. However, job interview ..read more
Visit website
The Fibonacci series, runtime complexity and memoization
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
In this post we’ll look at an often recurring dev interview question: the Fibonacci series. The series is straightforward to build. The starting 0 and 1 are given and from then on each new number is calculated by adding the previous two numbers of the series. So the third element in the series will be 0 + 1 = 1, then 1 + 1 = 2 and so on. Here’s a short example: 0 1 1 2 3 5 8 13 21 34 The problem is most often presented as follows: write a function that returns the nth element of the Fibonacci series. The function will therefore accept an integer that denotes the position in the Fibonacci ..read more
Visit website
Break up a list into batches with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
Here comes another classic interview question: write a function that accepts a collection and an integer. The function must divide the incoming collection up into individual collections of the size specified by the integer parameter. These individual collections can be called batches, groups or chunks and probably other things as well. Let’s take the following collection of integers as an example: [43, 65, 23, 56, 76, 454, 76, 54, 987] If the incoming size integer is 3 then we want to end up with the following chunks: [[43, 65, 23], [56, 76, 454], [76, 54, 987]] …, i.e. a list of list elem ..read more
Visit website
Solving the classic FizzBuzz problem with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
In this post we’ll go through a classic interview question for developers: the FizzBuzz problem. You are asked to write a function that accepts an integer. The function must implement a loop from 1 to the integer that was passed into the function. In the loop the function must print the following to the command window: if the number is divisible by 3 then print “fizz” if the number is divisible by 5 then print “buzz” if the number is divisible by both 5 and 3 then print “fizzbuzz” otherwise just print the number itself We apply only one of these rules in the loop. E.g. 15 is divisible by bot ..read more
Visit website
Determine if a string is a palindrome with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
Here comes another classic coding question that you might come across during an interview. Write a function that accepts a string and returns a boolean. The function must determine if the string is a palindrome. A string is a palindrome if it has the same value in both directions: forward and backward. Examples: abba qwertrewq 1234321 In this post we’ll look at two different solutions, but there are certainly others. Also, we’ll implement case-sensitive solutions, i.e. the following will return false: aBba qWertrewq The first solution is based on looping through the characters of the strin ..read more
Visit website
Roll your own custom list with C# .NET
Exercises in .NET with Andras Nemes
by Andras Nemes
3y ago
The generic list (https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netcore-3.1) is probably the most often used collection type in C#. It’s easy to use, it’s type safe, it offers a lot of manipulation methods and a myriad of LINQ functions for searching, sorting and much, much more. If you need a collection where you can add, remove, insert and set elements at an index then the List is your go-to collection type. However, imagine that you’re sitting in a job interview and your interviewer tells you to build your own list-type collection without using any of th ..read more
Visit website

Follow Exercises in .NET with Andras Nemes on FeedSpot

Continue with Google
Continue with Apple
OR