Autocoder

Micron Coding Round Questions with Solutions

Last updated 2026-11-19

Coding questions for a memory company lean on bits: extract a field, flip the bits that differ, find the first free block in a bitmap, encode data with a Hamming code. This page gives twelve practice problems in that style with complete C or Python solutions, their complexity and the common mistake.

They are practice problems for topics such rounds are commonly reported to include, not real papers. See Micron interview questions for the concept side and bit manipulation interview questions for more drill.

Habits for bit problems

Use unsigned types of fixed width, write masks as 1u << n or 1ULL << n, and parenthesise every bit expression. Test the boundaries: n = 0, n equal to the type width and the all-ones value.

Say aloud which bits you are manipulating. Interviewers can follow a mask they can see on the page far more easily than an expression they have to decode.

Hiring status

No sourced hiring facts for Micron are held on this site, so no dates or openings are quoted. Use the company's own careers site.

Micron Coding Round Questions with Solutions: questions with worked answers

  1. 1

    Count the bits you must flip to convert integer A into integer B.

    easy

    XOR gives 1 in every position where A and B differ, and the popcount of the XOR is the answer. For 29 (11101) and 15 (01111) the XOR is 10010, which has 2 set bits. Time O(k) with Kernighan's loop, space O(1). Common mistake: comparing the numbers digit by digit in decimal.

    c
    #include <stdio.h>
    #include <stdint.h>
    int flips(uint32_t a, uint32_t b) {
        uint32_t x = a ^ b;
        int n = 0;
        while (x) { x &= x - 1; n++; }
        return n;
    }
    int main(void) {
        printf("%d\n", flips(29, 15)); /* 2 */
        return 0;
    }
  2. 2

    Extract n bits starting at position p (counting from the least significant bit).

    medium

    Shift right by p, then mask with (1u << n) - 1. For n equal to 32 that expression is undefined, so handle it separately. Extracting 4 bits at position 4 from 0xABCD gives 0xC. Time O(1). Common mistake: confusing the position convention, and shifting by the type width.

    c
    #include <stdio.h>
    #include <stdint.h>
    uint32_t extract(uint32_t x, int p, int n) {
        uint32_t mask = (n >= 32) ? 0xFFFFFFFFu : ((1u << n) - 1u);
        return (x >> p) & mask;
    }
    int main(void) {
        printf("0x%X\n", extract(0xABCDu, 4, 4)); /* 0xC */
        return 0;
    }
  3. 3

    Insert the n low bits of v into x at position p, replacing what was there.

    medium

    Build a mask of n ones at position p, clear those bits in x, then OR in the masked, shifted value. Inserting 0x5 (3 bits) at position 4 into 0xFF gives 0xDF, since bits 4 to 6 become 101. Time O(1). Common mistake: not masking v first, so extra high bits of v corrupt neighbouring fields.

    c
    #include <stdio.h>
    #include <stdint.h>
    uint32_t insert(uint32_t x, uint32_t v, int p, int n) {
        uint32_t mask = ((1u << n) - 1u) << p;
        return (x & ~mask) | ((v << p) & mask);
    }
    int main(void) {
        printf("0x%X\n", insert(0xFFu, 0x5u, 4, 3)); /* 0xDF */
        return 0;
    }
  4. 4

    Encode four data bits with a Hamming(7,4) code and correct a single bit error.

    hard

    Place parity bits at positions 1, 2 and 4 and data bits at 3, 5, 6 and 7. Each parity bit is the XOR of the positions whose index has that bit set. To decode, recompute the three checks; they form a syndrome that equals the position of the flipped bit, or 0 if there is no error. Time O(1). Common mistake: numbering positions from 0, which breaks the syndrome-equals-position property.

    python
    def encode(d):
        d1, d2, d3, d4 = d
        p1 = d1 ^ d2 ^ d4
        p2 = d1 ^ d3 ^ d4
        p3 = d2 ^ d3 ^ d4
        return [p1, p2, d1, p3, d2, d3, d4]       # positions 1..7
    
    def correct(w):
        w = w[:]
        s = (w[0] ^ w[2] ^ w[4] ^ w[6]) \
          | (w[1] ^ w[2] ^ w[5] ^ w[6]) << 1 \
          | (w[3] ^ w[4] ^ w[5] ^ w[6]) << 2
        if s:
            w[s - 1] ^= 1
        return [w[2], w[4], w[5], w[6]], s
    
    w = encode([1, 0, 1, 1])
    w[4] ^= 1                                       # flip position 5
    print(correct(w))  # ([1, 0, 1, 1], 5)
  5. 5

    Find and claim the first free block in a bitmap.

    medium

    Scan 64-bit words; the first word that is not all ones contains the answer. The index of the first zero bit is the count of trailing zeros of the inverted word. Set the bit and return word index times 64 plus the bit index. Time O(n / 64) worst case, space O(1). Common mistake: calling __builtin_ctz on zero when the word is full, and mixing 32-bit and 64-bit shifts, since 1 << 40 on an int is undefined.

    c
    #include <stdio.h>
    #include <stdint.h>
    int claim(uint64_t *map, int words) {
        for (int w = 0; w < words; w++) {
            if (map[w] != ~0ULL) {
                int b = __builtin_ctzll(~map[w]);
                map[w] |= 1ULL << b;
                return w * 64 + b;
            }
        }
        return -1;
    }
    int main(void) {
        uint64_t map[2] = {0x7ULL, 0};
        int a = claim(map, 2);
        int b = claim(map, 2);
        printf("%d %d\n", a, b); /* 3 4 */
        return 0;
    }
  6. 6

    Predict the output: right shift of a negative number versus an unsigned one.

    medium

    Right shifting a negative signed integer is implementation-defined; common compilers use an arithmetic shift that copies the sign bit, so -1 >> 1 is -1. For the unsigned value 0xFFFFFFFF, a logical shift gives 0x7FFFFFFF, which is 2147483647. Output on typical platforms: -1 2147483647. Common mistake: using signed shifts on data that is really a bit pattern, which makes results differ between compilers.

    c
    #include <stdio.h>
    int main(void) {
        int a = -1;
        unsigned b = 0xFFFFFFFFu;
        printf("%d %u\n", a >> 1, b >> 1);
        return 0;
    }
  7. 7

    Swap the odd and even bits of a 32-bit integer.

    medium

    Mask the even-position bits (0x55555555) and shift them left by one, mask the odd-position bits (0xAAAAAAAA) and shift them right by one, then OR. 0x1 becomes 0x2 and 0xA becomes 0x5. Time O(1). Common mistake: using a signed type, where shifting 0xAAAAAAAA right could replicate the sign bit.

    c
    #include <stdio.h>
    #include <stdint.h>
    uint32_t swap_bits(uint32_t x) {
        return ((x & 0x55555555u) << 1) | ((x & 0xAAAAAAAAu) >> 1);
    }
    int main(void) {
        printf("0x%X 0x%X\n", swap_bits(0x1u), swap_bits(0xAu)); /* 0x2 0x5 */
        return 0;
    }
  8. 8

    Predict the output: char to unsigned char conversion.

    easy

    a holds -1. Converting to unsigned char takes the value modulo 256, which is 255. Printing a as an int shows -1 on platforms where plain char is signed. Output: -1 255. Common mistake: expecting the stored bits to change; the same eight bits 0xFF are interpreted differently.

    c
    #include <stdio.h>
    int main(void) {
        signed char a = -1;
        unsigned char b = (unsigned char)a;
        printf("%d %d\n", a, b);
        return 0;
    }
  9. 9

    Check whether a number has alternating bits (101010 style).

    medium

    XOR the number with itself shifted right by one. If the bits alternate, the result is all ones, and a number of the form 2^k - 1 satisfies y & (y + 1) == 0. 5 (101) and 10 (1010) pass; 7 (111) fails. Time O(1). Common mistake: not handling y + 1 overflow for the largest unsigned value; use a wider type or compare with care.

    c
    #include <stdio.h>
    #include <stdint.h>
    int alternating(uint32_t x) {
        uint64_t y = x ^ (x >> 1);
        return (y & (y + 1)) == 0;
    }
    int main(void) {
        printf("%d %d %d\n", alternating(5), alternating(10), alternating(7)); /* 1 1 0 */
        return 0;
    }
  10. 10

    Find the duplicate in an array of n + 1 numbers drawn from 1..n without modifying it and in O(1) space.

    medium

    Treat the array as a linked list where index i points to a[i]. Because of the duplicate, there is a cycle, and the duplicate is the cycle entrance. Use Floyd's two pointers to find a meeting point, then restart one pointer from the start and move both one step at a time; they meet at the entrance. Time O(n), space O(1). Common mistake: sorting, which changes the array and costs O(n log n).

    python
    def find_duplicate(a):
        slow = fast = a[0]
        while True:
            slow = a[slow]
            fast = a[a[fast]]
            if slow == fast:
                break
        slow = a[0]
        while slow != fast:
            slow = a[slow]
            fast = a[fast]
        return slow
    
    print(find_duplicate([1, 3, 4, 2, 2]), find_duplicate([3, 1, 3, 4, 2]))  # 2 3
  11. 11

    Sort integers by the number of set bits, then by value.

    easy

    Use a sort key of (popcount, value). For [0, 1, 2, 3, 4, 5, 6, 7, 8] the order is [0, 1, 2, 4, 8, 3, 5, 6, 7]. Time O(n log n), space O(n). Python's int.bit_count() needs 3.10 or later, so bin(x).count("1") is the portable choice. Common mistake: forgetting the tie-break, which leaves equal-popcount values in input order.

    python
    def by_bits(a):
        return sorted(a, key=lambda x: (bin(x).count("1"), x))
    
    print(by_bits(list(range(9))))  # [0, 1, 2, 4, 8, 3, 5, 6, 7]
  12. 12

    Run-length encode and decode a string, a basic storage compression idea.

    medium

    Walk the string and count runs of equal characters, emitting count and character. Decode by repeating each character count times. For "aaabccdd" the encoding is [(3, a), (1, b), (2, c), (2, d)]. Time O(n), space O(n). Common mistake: dropping the last run because the loop flushes a run only when the next character differs.

    python
    def rle(s):
        out, i = [], 0
        while i < len(s):
            j = i
            while j < len(s) and s[j] == s[i]:
                j += 1
            out.append((j - i, s[i]))
            i = j
        return out
    
    def unrle(pairs):
        return "".join(ch * n for n, ch in pairs)
    
    e = rle("aaabccdd")
    print(e, unrle(e) == "aaabccdd")  # [(3, 'a'), (1, 'b'), (2, 'c'), (2, 'd')] True

Frequently asked questions

What topics do memory-company coding rounds emphasise?

Commonly reported are bit manipulation, C output prediction, arrays and strings, linked lists and bitmaps or buffers. The difficulty is easy to medium but the details of types and shifts matter.

Should I code in C or Python for bit problems?

C is natural for firmware roles and shows you know the types. Python is acceptable where allowed, but remember that its integers are unbounded, so mask explicitly when simulating a fixed width.

Are these real Micron problems?

No. They are practice problems in the style of topics such rounds are commonly reported to cover. Micron does not publish its papers.

How do I check bit tricks quickly?

Test with small values by hand, then with 0, 1, the all-ones value and the highest bit. Print results in hex while debugging.

Related guides

More in Company Interview Questions

See all in this section