Skip to main content
Became Hot Network Question

Have ImplementedI implemented the below URL Shortener class.Used ConcurrentHashMap Used ConcurrentHashMap to handle concurrency. ShortUrl

Short URL building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.


import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class UrlShortener {

    private final Map<String, String> urlMap = new ConcurrentHashMap<>();

    private final int shortUrlLength;

    public UrlShortener(int shortUrlLength) {
        this.shortUrlLength = shortUrlLength;
    }

    public String shortenUrl(String longUrl) {
        if (longUrl.isEmpty()) {
            throw new IllegalArgumentException("long URL is empty");
        }
        String longUrlWithoutHttp = longUrl.replaceFirst("http://", "");
        String longUrlWithoutHttps = longUrlWithoutHttp.replaceFirst("https://", "");
        String longUrlWithoutWWW = longUrlWithoutHttps.replaceFirst("www.", "");
        String actualLongUrl = longUrlWithoutWWW.replaceAll("[^a-zA-Z0-9]", "");

        if (actualLongUrl.length() <= shortUrlLength) {
            return actualLongUrl;
        }
        return urlMap.computeIfAbsent(actualLongUrl, s -> {

            StringBuilder shortUrl = new StringBuilder(shortUrlLength);

            IntStream.range(0, shortUrlLength).forEach(i -> {
                int randomInt = ThreadLocalRandom.current().nextInt(62);
                int numericVal = actualLongUrl.charAt(i);
                char ch = (char) (Math.abs(randomInt - numericVal) % (26 - shortUrlLength) + 'a' + i);
                shortUrl.append(ch);
            });

            return shortUrl.toString();
        });
    }

    public static void main(String[] args) {
        UrlShortener urlShortner = new UrlShortener(6);
        System.out.println(urlShortner.shortenUrl("www.linkedin.com"));
        System.out.println(urlShortner.shortenUrl("http://linkedin.com/"));
        System.out.println(urlShortner.shortenUrl("https://linkedin.com/"));
    }

    @Override
    public boolean equals(Object o)
    {
        if(this==o) return true;
        return o instanceof UrlShortener other &&
                Objects.equals(other.shortUrlLength,this.shortUrlLength);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(this);
    }

}

package com.java.learning;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class UrlShortener {

    private final Map<String, String> urlMap = new ConcurrentHashMap<>();

    private final int shortUrlLength;

    public UrlShortener(int shortUrlLength) {
        this.shortUrlLength = shortUrlLength;
    }

    public String shortenUrl(String longUrl) {
        if (longUrl.isEmpty()) {
            throw new IllegalArgumentException("long URL is empty");
        }
        String longUrlWithoutHttp = longUrl.replaceFirst("http://", "");
        String longUrlWithoutHttps = longUrlWithoutHttp.replaceFirst("https://", "");
        String longUrlWithoutWWW = longUrlWithoutHttps.replaceFirst("www.", "");
        String actualLongUrl = longUrlWithoutWWW.replaceAll("[^a-zA-Z0-9]", "");

        if (actualLongUrl.length() <= shortUrlLength) {
            return actualLongUrl;
        }
        return urlMap.computeIfAbsent(actualLongUrl, s -> {

            StringBuilder shortUrl = new StringBuilder(shortUrlLength);

            IntStream.range(0, shortUrlLength).forEach(i -> {
                int randomInt = ThreadLocalRandom.current().nextInt(62);
                int numericVal = actualLongUrl.charAt(i);
                char ch = (char) (Math.abs(randomInt - numericVal) % (26 - shortUrlLength) + 'a' + i);
                shortUrl.append(ch);
            });

            return shortUrl.toString();
        });
    }

    public static void main(String[] args) {
        UrlShortener urlShortner = new UrlShortener(6);
        System.out.println(urlShortner.shortenUrl("www.linkedin.com"));
        System.out.println(urlShortner.shortenUrl("http://linkedin.com/"));
        System.out.println(urlShortner.shortenUrl("https://linkedin.com/"));
    }

    @Override
    public boolean equals(Object o)
    {
        if(this==o) return true;
        return o instanceof UrlShortener other &&
                Objects.equals(other.shortUrlLength,this.shortUrlLength);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(this);
    }

}

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.


import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class UrlShortener {

    private final Map<String, String> urlMap = new ConcurrentHashMap<>();

    private final int shortUrlLength;

    public UrlShortener(int shortUrlLength) {
        this.shortUrlLength = shortUrlLength;
    }

    public String shortenUrl(String longUrl) {
        if (longUrl.isEmpty()) {
            throw new IllegalArgumentException("long URL is empty");
        }
        String longUrlWithoutHttp = longUrl.replaceFirst("http://", "");
        String longUrlWithoutHttps = longUrlWithoutHttp.replaceFirst("https://", "");
        String longUrlWithoutWWW = longUrlWithoutHttps.replaceFirst("www.", "");
        String actualLongUrl = longUrlWithoutWWW.replaceAll("[^a-zA-Z0-9]", "");

        if (actualLongUrl.length() <= shortUrlLength) {
            return actualLongUrl;
        }
        return urlMap.computeIfAbsent(actualLongUrl, s -> {

            StringBuilder shortUrl = new StringBuilder(shortUrlLength);

            IntStream.range(0, shortUrlLength).forEach(i -> {
                int randomInt = ThreadLocalRandom.current().nextInt(62);
                int numericVal = actualLongUrl.charAt(i);
                char ch = (char) (Math.abs(randomInt - numericVal) % (26 - shortUrlLength) + 'a' + i);
                shortUrl.append(ch);
            });

            return shortUrl.toString();
        });
    }

    public static void main(String[] args) {
        UrlShortener urlShortner = new UrlShortener(6);
        System.out.println(urlShortner.shortenUrl("www.linkedin.com"));
        System.out.println(urlShortner.shortenUrl("http://linkedin.com/"));
        System.out.println(urlShortner.shortenUrl("https://linkedin.com/"));
    }

    @Override
    public boolean equals(Object o)
    {
        if(this==o) return true;
        return o instanceof UrlShortener other &&
                Objects.equals(other.shortUrlLength,this.shortUrlLength);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(this);
    }

}

I implemented the below URL Shortener class. Used ConcurrentHashMap to handle concurrency.

Short URL building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

package com.java.learning;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class UrlShortener {

    private final Map<String, String> urlMap = new ConcurrentHashMap<>();

    private final int shortUrlLength;

    public UrlShortener(int shortUrlLength) {
        this.shortUrlLength = shortUrlLength;
    }

    public String shortenUrl(String longUrl) {
        if (longUrl.isEmpty()) {
            throw new IllegalArgumentException("long URL is empty");
        }
        String longUrlWithoutHttp = longUrl.replaceFirst("http://", "");
        String longUrlWithoutHttps = longUrlWithoutHttp.replaceFirst("https://", "");
        String longUrlWithoutWWW = longUrlWithoutHttps.replaceFirst("www.", "");
        String actualLongUrl = longUrlWithoutWWW.replaceAll("[^a-zA-Z0-9]", "");

        if (actualLongUrl.length() <= shortUrlLength) {
            return actualLongUrl;
        }
        return urlMap.computeIfAbsent(actualLongUrl, s -> {

            StringBuilder shortUrl = new StringBuilder(shortUrlLength);

            IntStream.range(0, shortUrlLength).forEach(i -> {
                int randomInt = ThreadLocalRandom.current().nextInt(62);
                int numericVal = actualLongUrl.charAt(i);
                char ch = (char) (Math.abs(randomInt - numericVal) % (26 - shortUrlLength) + 'a' + i);
                shortUrl.append(ch);
            });

            return shortUrl.toString();
        });
    }

    public static void main(String[] args) {
        UrlShortener urlShortner = new UrlShortener(6);
        System.out.println(urlShortner.shortenUrl("www.linkedin.com"));
        System.out.println(urlShortner.shortenUrl("http://linkedin.com/"));
        System.out.println(urlShortner.shortenUrl("https://linkedin.com/"));
    }

    @Override
    public boolean equals(Object o)
    {
        if(this==o) return true;
        return o instanceof UrlShortener other &&
                Objects.equals(other.shortUrlLength,this.shortUrlLength);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(this);
    }

}

deleted 2 characters in body
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

Am I missing any important concepts here ? Are there any improvements that can be done to improve performance and key builder logic? What could be some few followup questions/additional implementations that can be asked after implementing this in a live coding interview?

Am I missing any important concepts here ? Are there any improvements that can be done to improve performance and key builder logic? What could be some few followup questions/additional implementations that can be asked after implementing this in a live coding interview?

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

Am I missing any important concepts here ? Are there any improvements that can be done to improve performance and key builder logic? What could be some few followup questions/additional implementations that can be asked after implementing this in a live coding interview?

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

Am I missing any important concepts here ? Are there any improvements that can be done to improve performance and key builder logic? What could be some few followup questions/additional implementations that can be asked after implementing this in a live coding interview?

Source Link
Jill
  • 277
  • 1
  • 7

Reduce String Length With Thread Safety & Concurrency

Have Implemented below URL Shortener class.Used ConcurrentHashMap to handle concurrency. ShortUrl building logic may not be optimal, but it ensures that only small letters will be created with short URL length specified by client.

Am I missing any important concepts here ? Are there any improvements that can be done to improve performance and key builder logic? What could be some few followup questions/additional implementations that can be asked after implementing this in a live coding interview?


import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class UrlShortener {

    private final Map<String, String> urlMap = new ConcurrentHashMap<>();

    private final int shortUrlLength;

    public UrlShortener(int shortUrlLength) {
        this.shortUrlLength = shortUrlLength;
    }

    public String shortenUrl(String longUrl) {
        if (longUrl.isEmpty()) {
            throw new IllegalArgumentException("long URL is empty");
        }
        String longUrlWithoutHttp = longUrl.replaceFirst("http://", "");
        String longUrlWithoutHttps = longUrlWithoutHttp.replaceFirst("https://", "");
        String longUrlWithoutWWW = longUrlWithoutHttps.replaceFirst("www.", "");
        String actualLongUrl = longUrlWithoutWWW.replaceAll("[^a-zA-Z0-9]", "");

        if (actualLongUrl.length() <= shortUrlLength) {
            return actualLongUrl;
        }
        return urlMap.computeIfAbsent(actualLongUrl, s -> {

            StringBuilder shortUrl = new StringBuilder(shortUrlLength);

            IntStream.range(0, shortUrlLength).forEach(i -> {
                int randomInt = ThreadLocalRandom.current().nextInt(62);
                int numericVal = actualLongUrl.charAt(i);
                char ch = (char) (Math.abs(randomInt - numericVal) % (26 - shortUrlLength) + 'a' + i);
                shortUrl.append(ch);
            });

            return shortUrl.toString();
        });
    }

    public static void main(String[] args) {
        UrlShortener urlShortner = new UrlShortener(6);
        System.out.println(urlShortner.shortenUrl("www.linkedin.com"));
        System.out.println(urlShortner.shortenUrl("http://linkedin.com/"));
        System.out.println(urlShortner.shortenUrl("https://linkedin.com/"));
    }

    @Override
    public boolean equals(Object o)
    {
        if(this==o) return true;
        return o instanceof UrlShortener other &&
                Objects.equals(other.shortUrlLength,this.shortUrlLength);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(this);
    }

}