Autocoder

PayPal coding round questions with solutions

Last updated 2026-11-19

PayPal engineering assessments are commonly described as medium-difficulty problems on a platform such as HackerRank, with hashing, graphs, DP and sliding windows common. Because PayPal works on payments, domain-flavoured versions, such as currency conversion routes and duplicate payment detection, are natural practice. This set follows that style with eight coding problems, one SQL problem and one MCQ. None is a recalled question.

Each answer has the approach, complexity, the common mistake and runnable Python and Java. For interviews, see PayPal interview questions.

StyleMedium graphs, hashing, windows, DP, SQL
Languages herePython, Java, SQL
Problems8 coding, 1 SQL, 1 MCQ
LevelEasy to hard

How to approach this set

Payment-flavoured problems add edge cases: exact money arithmetic, repeated requests and ordering by time. When you read a problem, write the invariants first, for example 'the timestamp is updated only on success', then code.

Pair this set with the dynamic programming and sliding window guides for the patterns, and review how to crack an online assessment for timing.

PayPal coding round questions with solutions: questions with worked answers

  1. 1

    Currency conversion: given pairs like (USD, INR) = 83 and queries such as (INR, USD), return each query's rate, or -1 if unknown.

    medium

    Model currencies as nodes of a graph, with an edge a to b of weight r and the reverse edge b to a of weight 1/r. Answer each query with a DFS or BFS from the source to the target, multiplying weights along the path. Time O(E + V) per query, space O(V + E). Union-find with weighted edges can answer repeated queries faster. Mistakes: forgetting reverse edges, not handling an unknown currency, and returning 1 for an unknown currency queried against itself.

    python
    from collections import defaultdict
    def convert(pairs, rates, queries):
        g = defaultdict(dict)
        for (a, b), r in zip(pairs, rates):
            g[a][b] = r
            g[b][a] = 1 / r
        def dfs(u, t, seen):
            if u == t:
                return 1.0
            seen.add(u)
            for v, w in g[u].items():
                if v not in seen:
                    r = dfs(v, t, seen)
                    if r != -1:
                        return w * r
            return -1
        return [dfs(a, b, set()) if a in g and b in g else -1 for a, b in queries]
    java
    import java.util.*;
    class Solution {
        static Map<String, Map<String, Double>> g = new HashMap<>();
        static double dfs(String u, String t, Set<String> seen) {
            if (u.equals(t)) return 1.0;
            seen.add(u);
            for (Map.Entry<String, Double> e : g.get(u).entrySet()) {
                if (!seen.contains(e.getKey())) {
                    double r = dfs(e.getKey(), t, seen);
                    if (r != -1) return e.getValue() * r;
                }
            }
            return -1;
        }
        static double[] convert(String[][] pairs, double[] rates, String[][] queries) {
            g.clear();
            for (int i = 0; i < pairs.length; i++) {
                g.computeIfAbsent(pairs[i][0], k -> new HashMap<>()).put(pairs[i][1], rates[i]);
                g.computeIfAbsent(pairs[i][1], k -> new HashMap<>()).put(pairs[i][0], 1 / rates[i]);
            }
            double[] out = new double[queries.length];
            for (int i = 0; i < queries.length; i++)
                out[i] = g.containsKey(queries[i][0]) && g.containsKey(queries[i][1])
                    ? dfs(queries[i][0], queries[i][1], new HashSet<>()) : -1;
            return out;
        }
    }
  2. 2

    Logger rate limiter: print a message only if it has not been printed in the last 10 seconds.

    easy

    Store the last printed timestamp for each message in a hash map. On a request at time t, allow it if the message is new or t minus the stored time is at least 10, and update the timestamp only when allowed. Time O(1) per call, space O(distinct messages). Mistake: updating the timestamp on rejected messages, which keeps extending the block. For unbounded message sets, evict old entries periodically.

    python
    class Logger:
        def __init__(self):
            self.last = {}
        def should_print(self, t, msg):
            if msg not in self.last or t - self.last[msg] >= 10:
                self.last[msg] = t
                return True
            return False
    java
    import java.util.*;
    class Logger {
        private final Map<String, Integer> last = new HashMap<>();
        boolean shouldPrint(int t, String msg) {
            Integer prev = last.get(msg);
            if (prev == null || t - prev >= 10) { last.put(msg, t); return true; }
            return false;
        }
    }
  3. 3

    Cheapest flight (cheapest payment route) from a source to a destination with at most k stops.

    hard

    Run Bellman-Ford for k + 1 rounds. Keep a distance array; in each round relax every edge using a copy of the previous round's distances so a path never grows by more than one edge per round. The answer is dist[dst], or -1 if infinite. Time O(k x E), space O(V). Dijkstra on cost alone fails because the cheapest path may use too many stops. The common bug is relaxing in place, which allows multiple edges per round.

    python
    def cheapest(n, edges, src, dst, k):
        INF = float('inf')
        dist = [INF] * n
        dist[src] = 0
        for _ in range(k + 1):
            nxt = dist[:]
            for u, v, w in edges:
                if dist[u] + w < nxt[v]:
                    nxt[v] = dist[u] + w
            dist = nxt
        return -1 if dist[dst] == INF else dist[dst]
    java
    import java.util.*;
    class Solution {
        static int cheapest(int n, int[][] edges, int src, int dst, int k) {
            int INF = Integer.MAX_VALUE / 2;
            int[] dist = new int[n];
            Arrays.fill(dist, INF);
            dist[src] = 0;
            for (int i = 0; i <= k; i++) {
                int[] nxt = dist.clone();
                for (int[] e : edges)
                    if (dist[e[0]] + e[2] < nxt[e[1]]) nxt[e[1]] = dist[e[0]] + e[2];
                dist = nxt;
            }
            return dist[dst] >= INF ? -1 : dist[dst];
        }
    }
  4. 4

    Top K frequent elements in better than O(n log n).

    medium

    Count frequencies, then use bucket sort: create buckets indexed by frequency (1 to n) and place each value in its bucket, then read the buckets from highest to lowest until you have k values. Time O(n), space O(n). A heap of size k gives O(n log k). Mistake: sorting all distinct values by count, which is O(u log u) and is what the follow-up asks you to beat.

    python
    from collections import Counter
    def top_k(a, k):
        c = Counter(a)
        buckets = [[] for _ in range(len(a) + 1)]
        for v, f in c.items():
            buckets[f].append(v)
        out = []
        for f in range(len(a), 0, -1):
            for v in buckets[f]:
                out.append(v)
                if len(out) == k:
                    return out
        return out
    java
    import java.util.*;
    class Solution {
        static List<Integer> topK(int[] a, int k) {
            Map<Integer, Integer> c = new HashMap<>();
            for (int x : a) c.merge(x, 1, Integer::sum);
            List<Integer>[] buckets = new List[a.length + 1];
            for (Map.Entry<Integer, Integer> e : c.entrySet()) {
                int f = e.getValue();
                if (buckets[f] == null) buckets[f] = new ArrayList<>();
                buckets[f].add(e.getKey());
            }
            List<Integer> out = new ArrayList<>();
            for (int f = a.length; f > 0 && out.size() < k; f--)
                if (buckets[f] != null) out.addAll(buckets[f]);
            return out.subList(0, Math.min(k, out.size()));
        }
    }
  5. 5

    Longest repeating character replacement: with at most k replacements, find the longest substring of one repeated letter.

    medium

    Sliding window with counts. Track the highest frequency of any letter in the window. If window length minus that frequency exceeds k, more than k replacements would be needed, so shrink from the left. The answer is the largest window seen. Time O(n), space O(26). You do not need to decrease the max frequency when shrinking, because only a larger frequency can improve the answer. Mistake: recomputing the max for every step, which makes it O(26n) (still fine) or, worse, O(n^2).

    python
    def char_replacement(s, k):
        cnt, left, maxf, best = {}, 0, 0, 0
        for r, ch in enumerate(s):
            cnt[ch] = cnt.get(ch, 0) + 1
            maxf = max(maxf, cnt[ch])
            while (r - left + 1) - maxf > k:
                cnt[s[left]] -= 1
                left += 1
            best = max(best, r - left + 1)
        return best
    java
    class Solution {
        static int charReplacement(String s, int k) {
            int[] cnt = new int[26];
            int left = 0, maxf = 0, best = 0;
            for (int r = 0; r < s.length(); r++) {
                maxf = Math.max(maxf, ++cnt[s.charAt(r) - 'A']);
                while ((r - left + 1) - maxf > k) cnt[s.charAt(left++) - 'A']--;
                best = Math.max(best, r - left + 1);
            }
            return best;
        }
    }
  6. 6

    Unique paths with obstacles: count the paths from the top-left to the bottom-right moving right or down, avoiding cells marked 1.

    medium

    dp[i][j] is the number of ways to reach cell (i, j): zero if it is an obstacle, otherwise the sum of the ways from above and from the left. A single rolling row suffices. Time O(mn), space O(n). Mistake: seeding dp[0] as 1 without checking whether the start itself is blocked, in which case the answer is 0. Use 64-bit integers if the problem allows large grids.

    python
    def unique_paths(g):
        n = len(g[0])
        dp = [0] * n
        dp[0] = 1 if g[0][0] == 0 else 0
        for row in g:
            for j in range(n):
                if row[j] == 1:
                    dp[j] = 0
                elif j > 0:
                    dp[j] += dp[j - 1]
        return dp[-1]
    java
    class Solution {
        static long uniquePaths(int[][] g) {
            int n = g[0].length;
            long[] dp = new long[n];
            dp[0] = g[0][0] == 0 ? 1 : 0;
            for (int[] row : g)
                for (int j = 0; j < n; j++) {
                    if (row[j] == 1) dp[j] = 0;
                    else if (j > 0) dp[j] += dp[j - 1];
                }
            return dp[n - 1];
        }
    }
  7. 7

    Insert a new interval into a sorted list of non-overlapping intervals and merge as needed.

    medium

    Three phases in one pass: copy all intervals that end before the new one starts; merge all intervals that overlap it by taking the minimum start and maximum end; copy the rest. Time O(n), space O(n) for the output. Mistake: using strict comparisons that treat touching intervals such as [1,3] and [3,5] as separate when they should merge.

    python
    def insert(iv, new):
        out, i, n = [], 0, len(iv)
        while i < n and iv[i][1] < new[0]:
            out.append(iv[i]); i += 1
        while i < n and iv[i][0] <= new[1]:
            new = [min(new[0], iv[i][0]), max(new[1], iv[i][1])]
            i += 1
        out.append(new)
        out.extend(iv[i:])
        return out
    java
    import java.util.*;
    class Solution {
        static int[][] insert(int[][] iv, int[] nw) {
            List<int[]> out = new ArrayList<>();
            int i = 0, n = iv.length;
            while (i < n && iv[i][1] < nw[0]) out.add(iv[i++]);
            while (i < n && iv[i][0] <= nw[1]) {
                nw = new int[]{Math.min(nw[0], iv[i][0]), Math.max(nw[1], iv[i][1])};
                i++;
            }
            out.add(nw);
            while (i < n) out.add(iv[i++]);
            return out.toArray(new int[0][]);
        }
    }
  8. 8

    Valid palindrome II: can a string become a palindrome by deleting at most one character?

    easy

    Use two pointers from both ends. At the first mismatch, the answer is true if either the substring skipping the left character or the one skipping the right character is a palindrome; check both with a helper. Time O(n), space O(1). Mistake: trying every possible deletion, which is O(n^2), or skipping on later mismatches too, which allows more than one deletion.

    python
    def valid_palindrome(s):
        def ok(i, j):
            while i < j:
                if s[i] != s[j]:
                    return False
                i += 1; j -= 1
            return True
        i, j = 0, len(s) - 1
        while i < j:
            if s[i] != s[j]:
                return ok(i + 1, j) or ok(i, j - 1)
            i += 1; j -= 1
        return True
    java
    class Solution {
        static boolean ok(String s, int i, int j) {
            while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false;
            return true;
        }
        static boolean validPalindrome(String s) {
            int i = 0, j = s.length() - 1;
            while (i < j) {
                if (s.charAt(i) != s.charAt(j)) return ok(s, i + 1, j) || ok(s, i, j - 1);
                i++; j--;
            }
            return true;
        }
    }
  9. 9

    SQL: flag possible duplicate payments, where the same user pays the same merchant the same amount within 10 minutes.

    medium

    Self join the payments table on user, merchant and amount, require the second payment to have a larger id (so each pair appears once) and a timestamp within 10 minutes of the first. The id condition avoids matching a row with itself and listing pairs twice. Index on (user_id, merchant_id, amount, created_at) to keep it fast. Interval syntax varies by database.

    sql
    SELECT a.id AS first_id, b.id AS second_id, a.user_id, a.merchant_id, a.amount
    FROM Payments a
    JOIN Payments b
      ON a.user_id = b.user_id AND a.merchant_id = b.merchant_id AND a.amount = b.amount
     AND b.id > a.id
     AND b.created_at <= a.created_at + INTERVAL '10 minutes'
     AND b.created_at >= a.created_at;
  10. 10

    MCQ: in Python, what does 0.1 + 0.2 == 0.3 evaluate to, and why?

    easy

    False. 0.1 and 0.2 cannot be represented exactly in binary floating point, and their sum is 0.30000000000000004, which differs from the nearest double to 0.3. Compare floats with math.isclose or an absolute tolerance, and use decimal.Decimal or integer minor units for money. The same holds in Java, JavaScript and C++.

Frequently asked questions

How hard is the PayPal coding test?

It is commonly described as medium difficulty with graph, hashing, DP and sliding-window problems.

Does the test include SQL?

Backend and data roles are sometimes reported to include SQL. Practise self joins and window functions.

Are there payment-specific questions?

Not as a known question type. The domain-flavoured problems here are practice for the underlying patterns.

Are these the real questions?

No. They are original practice problems in the style of the topics the round commonly covers.

Related guides

More in Company Interview Questions

See all in this section